Docker网络 overlay模式

本文翻译自docker官网:https://docs.docker.com/network/overlay/

Use overlay networks

The overlay network driver creates a distributed network among multiple
Docker daemon hosts. This network sits on top of (overlays) the host-specific
networks, allowing containers connected to it (including swarm service
containers) to communicate securely when encryption is enabled. Docker
transparently handles routing of each packet to and from the correct Docker
daemon host and the correct destination container.

overlay网络驱动程序在多个Docker守护程序主机之间创建一个分布式网络。
这个网络位于(覆盖)主机特定网络之上,允许连接到它的容器(包括swarm服务容器)在启用加密时安全地通信。
Docker透明地处理每个数据包往返于正确的Docker守护程序主机和正确的目标容器的路由。

When you initialize a swarm or join a Docker host to an existing swarm, two
new networks are created on that Docker host:

当你初始化群或将Docker主机加入现有群时,将在该Docker主机上创建两个新网络:

  • an overlay network called ingress, which handles control and data traffic
    related to swarm services. When you create a swarm service and do not
    connect it to a user-defined overlay network, it connects to the ingress
    network by default.

    一种称为ingress的overlay网络,处理与swarm服务相关的控制和数据通信。
    当您创建swarm服务而不将其连接到用户定义的覆盖网络时,默认情况下,它将连接到ingress网络。

  • a bridge network called docker_gwbridge, which connects the individual
    Docker daemon to the other daemons participating in the swarm.

    一个称为docker_gwbridge的网桥网络,它将单个docker守护进程连接到参与swarm的其他守护进程。

You can create user-defined overlay networks using docker network create,
in the same way that you can create user-defined bridge networks. Services
or containers can be connected to more than one network at a time. Services or
containers can only communicate across networks they are each connected to.

可以使用docker network create创建用户定义的overlay网络,方法与创建用户定义的bridge网络相同。
服务或容器一次可以连接到多个网络。服务或容器只能通过各自连接的网络进行通信。

Although you can connect both swarm services and standalone containers to an
overlay network, the default behaviors and configuration concerns are different.
For that reason, the rest of this topic is divided into operations that apply to
all overlay networks, those that apply to swarm service networks, and those that
apply to overlay networks used by standalone containers.

尽管您可以将swarm服务和独立容器连接到overlay网络,但默认行为和配置关注点是不同的。
因此,本主题的其余部分分为适用于所有overlay网络的操作、适用于swarm服务网络的操作和适用于独立容器使用的overlay网络的操作。

Operations for all overlay networks

overlay网络的所有操作

Create an overlay network

创建一个overlay网络

Prerequisites:先决条件

  • Firewall rules for Docker daemons using overlay networks(使用覆盖网络的Docker守护程序的防火墙规则)

    You need the following ports open to traffic to and from each Docker host participating on an overlay network:
    你需要开放以下端口,以便和参与overlay网络的每个Docker主机进行通信:

    • TCP port 2377 for cluster management communications(用于群集管理通信的TCP端口2377)
    • TCP and UDP port 7946 for communication among nodes(用于节点间通信的TCP和UDP端口7946)
    • UDP port 4789 for overlay network traffic(overlay网络流量的UDP端口4789)
  • Before you can create an overlay network, you need to either initialize your
    Docker daemon as a swarm manager using docker swarm init or join it to an
    existing swarm using docker swarm join. Either of these creates the default
    ingress overlay network which is used by swarm services by default. You need
    to do this even if you never plan to use swarm services. Afterward, you can
    create additional user-defined overlay networks.
    在创建覆盖网络之前,您需要使用docker swarm init将Docker守护程序初始化为swarm manager,
    或者使用docker swarm join将其连接到现有的swarm。其中任何一个都会创建默认的ingress overlay网络,
    swarm服务默认使用该网络。即使你从未打算使用swarm服务,你也需要这么做。之后,可以创建其他用户定义的覆盖网络。

To create an overlay network for use with swarm services, use a command like
the following:

要创建用于swarm服务的overlay网络,请使用如下命令:

$ docker network create -d overlay my-overlay

To create an overlay network which can be used by swarm services or
standalone containers to communicate with other standalone containers running on
other Docker daemons, add the --attachable flag:

要创建swarm服务独立容器可用于与其他Docker守护进程上运行的其他独立容器通信的overlay网络,请添加--attachable标志:

$ docker network create -d overlay --attachable my-attachable-overlay

You can specify the IP address range, subnet, gateway, and other options. See
docker network create --help for details.

您可以指定IP地址范围、子网、网关和其他选项。有关详细信息,请参见docker network create --help

Encrypt traffic on an overlay network

加密overlay网络上的流量

All swarm service management traffic is encrypted by default, using the
AES algorithm in
GCM mode. Manager nodes in the swarm rotate the key used to encrypt gossip data
every 12 hours.

默认情况下,所有swarm服务管理流量都是在GCM模式下使用AES算法加密的。
群中的管理节点每12小时轮换一次用于加密八卦数据的密钥。

To encrypt application data as well, add --opt encrypted when creating the
overlay network. This enables IPSEC encryption at the level of the vxlan. This
encryption imposes a non-negligible performance penalty, so you should test this
option before using it in production.

要同时加密应用程序数据,请在创建覆盖网络时添加--opt encrypted。这将在vxlan级别启用IPSEC加密。
这种加密会带来不可忽略的性能损失,因此您应该在生产中使用此选项之前测试它。

When you enable overlay encryption, Docker creates IPSEC tunnels between all the
nodes where tasks are scheduled for services attached to the overlay network.
These tunnels also use the AES algorithm in GCM mode and manager nodes
automatically rotate the keys every 12 hours.

启用覆盖加密时,Docker将在为连接到overlay网络的服务计划任务的所有节点之间创建IPSEC隧道。
这些隧道还在GCM模式下使用AES算法,管理器节点每12小时自动旋转一次密钥。

Do not attach Windows nodes to encrypted overlay networks.
不要将Windows节点附加到加密的overlay网络。

Overlay network encryption is not supported on Windows. If a Windows node
attempts to connect to an encrypted overlay network, no error is detected but
the node cannot communicate.
Windows不支持overlay网络加密。如果Windows节点尝试连接到加密的overlay网络,则不会检测到错误,但该节点无法通信。

Swarm mode overlay networks and standalone containers

Swarm模式overlay网络和独立容器

You can use the overlay network feature with both --opt encrypted --attachable
and attach unmanaged containers to that network:

您可以将overlay网络功能与--opt encrypted --attachable和将非托管容器附加到该网络一起使用:

$ docker network create --opt encrypted --driver overlay --attachable my-attachable-multi-host-network

Customize the default ingress network

自定义默认入口网络

Most users never need to configure the ingress network, but Docker allows you
to do so. This can be useful if the automatically-chosen subnet conflicts with
one that already exists on your network, or you need to customize other low-level
network settings such as the MTU.

大多数用户不需要配置ingress网络,但Docker允许您这样做。如果自动选择的子网与网络上已存在的子网冲突,
或者需要自定义其他低级网络设置(如MTU),则此功能非常有用。

Customizing the ingress network involves removing and recreating it. This is
usually done before you create any services in the swarm. If you have existing
services which publish ports, those services need to be removed before you can
remove the ingress network.

自定义ingress网络包括删除和重新创建它。这通常是在swarm中创建任何服务之前完成的。
如果您有发布端口的现有服务,则需要先删除这些服务,然后才能删除ingress网络。

During the time that no ingress network exists, existing services which do not
publish ports continue to function but are not load-balanced. This affects
services which publish ports, such as a WordPress service which publishes port

在不存在ingress网络的期间,不发布端口的现有服务继续工作,但负载不平衡。
这会影响发布端口的服务,例如发布端口80的WordPress服务。

  1. Inspect the ingress network using docker network inspect ingress, and
    remove any services whose containers are connected to it. These are services
    that publish ports, such as a WordPress service which publishes port 80. If
    all such services are not stopped, the next step fails.

    使用docker network inspect ingress检查ingress网络,并删除与其容器连接的所有服务。
    这些是发布端口的服务,例如发布端口80的WordPress服务。如果没有停止所有这些服务,下一步就会失败。

  2. Remove the existing ingress network:

    删除现有的ingress网络:

    $ docker network rm ingress
    
    WARNING! Before removing the routing-mesh network, make sure all the nodes
    in your swarm run the same docker engine version. Otherwise, removal may not
    be effective and functionality of newly created ingress networks will be
    impaired.
    Are you sure you want to continue? [y/N]
    
  3. Create a new overlay network using the --ingress flag, along with the
    custom options you want to set. This example sets the MTU to 1200, sets
    the subnet to 10.11.0.0/16, and sets the gateway to 10.11.0.2.

    使用--ingress标志以及要设置的自定义选项创建一个新的overlay网络。
    本例将MTU设置为1200,将子网设置为10.11.0.0/16,并将网关设置为10.11.0.2

    $ docker network create \
      --driver overlay \
      --ingress \
      --subnet=10.11.0.0/16 \
      --gateway=10.11.0.2 \
      --opt com.docker.network.driver.mtu=1200 \
      my-ingress
    

    Note: You can name your ingress network something other than
    ingress, but you can only have one. An attempt to create a second one
    fails.
    您可以将ingress网络命名为ingress以外的名称,但您只能有一个。尝试创建第二个失败。

  4. Restart the services that you stopped in the first step.

重新启动第一步中停止的服务。

Customize the docker_gwbridge interface

自定义docker_gwbridge接口

The docker_gwbridge is a virtual bridge that connects the overlay networks
(including the ingress network) to an individual Docker daemon's physical
network. Docker creates it automatically when you initialize a swarm or join a
Docker host to a swarm, but it is not a Docker device. It exists in the kernel
of the Docker host. If you need to customize its settings, you must do so before
joining the Docker host to the swarm, or after temporarily removing the host
from the swarm.

docker_gwbridge是一个虚拟网桥,它将覆盖网络(包括ingress网络)连接到单个docker守护程序的物理网络。
Docker在初始化swarm或将Docker主机加入swarm时自动创建它,但它不是Docker设备。它存在于Docker主机的内核中。
如果需要自定义其设置,则必须在将Docker主机加入swarm之前或从swarm中临时移除主机之后进行自定义。

  1. Stop Docker.

停止Docker。

  1. Delete the existing docker_gwbridge interface.

删除已存在的docker_gwbridge接口。

```bash
$ sudo ip link set docker_gwbridge down

$ sudo ip link del dev docker_gwbridge
```
  1. Start Docker. Do not join or initialize the swarm.

启动Docker。不要加入或初始化swarm。

  1. Create or re-create the docker_gwbridge bridge manually with your custom
    settings, using the docker network create command.
    This example uses the subnet 10.11.0.0/16. For a full list of customizable
    options, see Bridge driver options.

    使用docker network create命令,使用自定义设置手动创建或重新创建docker_gwbridge
    本例使用子网10.11.0.0/16。有关可自定义选项的完整列表,请参见Bridge driver options

    $ docker network create \
    --subnet 10.11.0.0/16 \
    --opt com.docker.network.bridge.name=docker_gwbridge \
    --opt com.docker.network.bridge.enable_icc=false \
    --opt com.docker.network.bridge.enable_ip_masquerade=true \
    docker_gwbridge
    
  2. Initialize or join the swarm. Since the bridge already exists, Docker does
    not create it with automatic settings.

    初始化或加入swarm。由于网桥已经存在,Docker不会使用自动设置来创建它。

Operations for swarm services

Publish ports on an overlay network

在overlay网络上发布端口

Swarm services connected to the same overlay network effectively expose all
ports to each other. For a port to be accessible outside of the service, that
port must be published using the -p or --publish flag on docker service create or docker service update. Both the legacy colon-separated syntax and
the newer comma-separated value syntax are supported. The longer syntax is
preferred because it is somewhat self-documenting.

连接到同一覆盖网络的swarm服务有效地将所有端口相互公开。要在服务外部访问端口,
必须在docker service createdocker service update上使用-p--publish`标志发布该端口。
支持传统的冒号分隔语法和较新的逗号分隔值语法。最好使用较长的语法,因为它有点自我记录(self-documenting)。

<table>
<thead>
<tr>
<th>Flag value</th>
<th>Description</th>
</tr>
</thead>
<tr>
<td><tt>-p 8080:80</tt> or<br /><tt>-p published=8080,target=80</tt></td>
<td>Map TCP port 80 on the service to port 8080 on the routing mesh.</td>
</tr>
<tr>
<td><tt>-p 8080:80/udp</tt> or<br /><tt>-p published=8080,target=80,protocol=udp</tt></td>
<td>Map UDP port 80 on the service to port 8080 on the routing mesh.</td>
</tr>
<tr>
<td><tt>-p 8080:80/tcp -p 8080:80/udp</tt> or <br /><tt>-p published=8080,target=80,protocol=tcp -p published=8080,target=80,protocol=udp</tt></td>
<td>Map TCP port 80 on the service to TCP port 8080 on the routing mesh, and map UDP port 80 on the service to UDP port 8080 on the routing mesh.</td>
</tr>
</table>

Bypass the routing mesh for a swarm service

绕过swarm服务的路由网

By default, swarm services which publish ports do so using the routing mesh.
When you connect to a published port on any swarm node (whether it is running a
given service or not), you are redirected to a worker which is running that
service, transparently. Effectively, Docker acts as a load balancer for your
swarm services. Services using the routing mesh are running in virtual IP (VIP)
mode
. Even a service running on each node (by means of the --mode global
flag) uses the routing mesh. When using the routing mesh, there is no guarantee
about which Docker node services client requests.

默认情况下,swarm服务使用路由网格进行发布端口。
当您连接到任何swarm节点上的已发布端口(无论它是否运行给定的服务)时,您都会被重定向到运行该服务的worker,这是透明的。
Docker实际上是swarm服务的负载均衡器。使用路由网格的服务以 虚拟IP(VIP)模式 运行。
甚至在每个节点上运行的服务(通过--mode global标志)也使用路由网格。
使用路由网格时,无法保证哪个Docker节点为客户端请求提供服务。

To bypass the routing mesh, you can start a service using DNS Round Robin
(DNSRR) mode
, by setting the --endpoint-mode flag to dnsrr. You must run
your own load balancer in front of the service. A DNS query for the service name
on the Docker host returns a list of IP addresses for the nodes running the
service. Configure your load balancer to consume this list and balance the
traffic across the nodes.

要绕过路由网格,可以通过--endpoint-mode标志设置为dnsrr,使用 DNS Round Robin (DNSRR)模式 启动服务。
您必须在服务前面运行自己的负载平衡器。Docker主机上服务名称的DNS查询返回运行该服务的节点的IP地址列表。
配置负载平衡器以使用此列表并平衡节点间的通信量。

Separate control and data traffic

独立的控制和数据通信

By default, control traffic relating to swarm management and traffic to and from
your applications runs over the same network, though the swarm control traffic
is encrypted. You can configure Docker to use separate network interfaces for
handling the two different types of traffic. When you initialize or join the
swarm, specify --advertise-addr and --datapath-addr separately. You must do
this for each node joining the swarm.

默认情况下,尽管swarm控制流量是加密的,与swarm管理相关的控制流量以及应用程序之间的流量是在同一网络上运行的。
您可以将Docker配置为使用单独的网络接口来处理两种不同类型的流量。
初始化或加入swarm时,分别指定--advertise-addr--datapath-addr。对于加入swarm的每个节点,必须这样做。

Operations for standalone containers on overlay networks

overlay网络上独立容器的操作

Attach a standalone container to an overlay network

将独立容器附加到overlay网络

The ingress network is created without the --attachable flag, which means
that only swarm services can use it, and not standalone containers. You can
connect standalone containers to user-defined overlay networks which are created
with the --attachable flag. This gives standalone containers running on
different Docker daemons the ability to communicate without the need to set up
routing on the individual Docker daemon hosts.

不适用--attachable标志创建ingress网络,意味着只有swarm服务可以使用它,而不是独立的容器。
您可以将独立容器连接到使用--attachable标志创建的用户定义的覆盖网络。
这使运行在不同Docker守护进程上的独立容器能够进行通信,而无需在各个Docker守护程序主机上设置路由。

Publish ports

Flag value Description
-p 8080:80 Map TCP port 80 in the container to port 8080 on the overlay network.
-p 8080:80/udp Map UDP port 80 in the container to port 8080 on the overlay network.
-p 8080:80/sctp Map SCTP port 80 in the container to port 8080 on the overlay network.
-p 8080:80/tcp -p 8080:80/udp Map TCP port 80 in the container to TCP port 8080 on the overlay network, and map UDP port 80 in the container to UDP port 8080 on the overlay network.

Container discovery

For most situations, you should connect to the service name, which is load-balanced and handled by all containers ("tasks") backing the service. To get a list of all tasks backing the service, do a DNS lookup for tasks.<service-name>.

对于大多数情况,你应该连接到服务名称,该名称是负载平衡的,由支持该服务的所有容器("tasks")处理。要获取支持服务的所有任务的列表,请执行DNS查找tasks.<service-name>

Next steps

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容