directory search
Compose About versions and upgrading (Compose) ASP.NET Core + SQL Server on Linux (Compose) CLI environment variables (Compose) Command-line completion (Compose) Compose(组成) Compose command-line reference(组合命令行参考) Control startup order (Compose) Django and PostgreSQL (Compose) Docker stacks and distributed application bundles (Compose) docker-compose build(docker-compose构建) docker-compose bundle docker-compose config docker-compose create docker-compose down docker-compose events docker-compose exec docker-compose help docker-compose images docker-compose kill docker-compose logs docker-compose pause docker-compose port docker-compose ps docker-compose pull docker-compose push docker-compose restart docker-compose rm docker-compose run docker-compose scale docker-compose start docker-compose stop docker-compose top docker-compose unpause docker-compose up Environment file (Compose) Environment variables in Compose Extend services in Compose Frequently asked questions (Compose) Getting started (Compose) Install Compose Link environment variables (deprecated) (Compose) Networking in Compose Overview of Docker Compose Overview of docker-compose CLI Quickstart: Compose and WordPress Rails and PostgreSQL (Compose) Sample apps with Compose Using Compose in production Using Compose with Swarm Engine .NET Core application (Engine) About images, containers, and storage drivers (Engine) Add nodes to the swarm (Engine) Apply custom metadata (Engine) Apply rolling updates (Engine) apt-cacher-ng Best practices for writing Dockerfiles (Engine) Binaries (Engine) Bind container ports to the host (Engine) Breaking changes (Engine) Build your own bridge (Engine) Configure container DNS (Engine) Configure container DNS in user-defined networks (Engine) CouchDB (Engine) Create a base image (Engine) Create a swarm (Engine) Customize the docker0 bridge (Engine) Debian (Engine) Default bridge network Delete the service (Engine) Deploy a service (Engine) Deploy services to a swarm (Engine) Deprecated Engine features Docker container networking (Engine) Docker overview (Engine) Docker run reference (Engine) Dockerfile reference (Engine) Dockerize an application Drain a node (Engine) Engine FAQ (Engine) Fedora (Engine) Get started (Engine) Get started with macvlan network driver (Engine) Get started with multi-host networking (Engine) How nodes work (Engine) How services work (Engine) Image management (Engine) Inspect the service (Engine) Install Docker (Engine) IPv6 with Docker (Engine) Join nodes to a swarm (Engine) Legacy container links (Engine) Lock your swarm (Engine) Manage nodes in a swarm (Engine) Manage sensitive data with Docker secrets (Engine) Manage swarm security with PKI (Engine) Manage swarm service networks (Engine) Migrate to Engine 1.10 Optional Linux post-installation steps (Engine) Overview (Engine) PostgreSQL (Engine) Raft consensus in swarm mode (Engine) Riak (Engine) Run Docker Engine in swarm mode Scale the service (Engine) SDKs (Engine) Select a storage driver (Engine) Set up for the tutorial (Engine) SSHd (Engine) Storage driver overview (Engine) Store service configuration data (Engine) Swarm administration guide (Engine) Swarm mode key concepts (Engine) Swarm mode overlay network security model (Engine) Swarm mode overview (Engine) Understand container communication (Engine) Use multi-stage builds (Engine) Use swarm mode routing mesh (Engine) Use the AUFS storage driver (Engine) Use the Btrfs storage driver (Engine) Use the Device mapper storage driver (Engine) Use the OverlayFS storage driver (Engine) Use the VFS storage driver (Engine) Use the ZFS storage driver (Engine) Engine: Admin Guide Amazon CloudWatch logs logging driver (Engine) Bind mounts (Engine) Collect Docker metrics with Prometheus (Engine) Configuring and running Docker (Engine) Configuring logging drivers (Engine) Control and configure Docker with systemd (Engine) ETW logging driver (Engine) Fluentd logging driver (Engine) Format command and log output (Engine) Google Cloud logging driver (Engine) Graylog Extended Format (GELF) logging driver (Engine) Journald logging driver (Engine) JSON File logging driver (Engine) Keep containers alive during daemon downtime (Engine) Limit a container's resources (Engine) Link via an ambassador container (Engine) Log tags for logging driver (Engine) Logentries logging driver (Engine) PowerShell DSC usage (Engine) Prune unused Docker objects (Engine) Run multiple services in a container (Engine) Runtime metrics (Engine) Splunk logging driver (Engine) Start containers automatically (Engine) Storage overview (Engine) Syslog logging driver (Engine) tmpfs mounts Troubleshoot volume problems (Engine) Use a logging driver plugin (Engine) Using Ansible (Engine) Using Chef (Engine) Using Puppet (Engine) View a container's logs (Engine) Volumes (Engine) Engine: CLI Daemon CLI reference (dockerd) (Engine) docker docker attach docker build docker checkpoint docker checkpoint create docker checkpoint ls docker checkpoint rm docker commit docker config docker config create docker config inspect docker config ls docker config rm docker container docker container attach docker container commit docker container cp docker container create docker container diff docker container exec docker container export docker container inspect docker container kill docker container logs docker container ls docker container pause docker container port docker container prune docker container rename docker container restart docker container rm docker container run docker container start docker container stats docker container stop docker container top docker container unpause docker container update docker container wait docker cp docker create docker deploy docker diff docker events docker exec docker export docker history docker image docker image build docker image history docker image import docker image inspect docker image load docker image ls docker image prune docker image pull docker image push docker image rm docker image save docker image tag docker images docker import docker info docker inspect docker kill docker load docker login docker logout docker logs docker network docker network connect docker network create docker network disconnect docker network inspect docker network ls docker network prune docker network rm docker node docker node demote docker node inspect docker node ls docker node promote docker node ps docker node rm docker node update docker pause docker plugin docker plugin create docker plugin disable docker plugin enable docker plugin inspect docker plugin install docker plugin ls docker plugin push docker plugin rm docker plugin set docker plugin upgrade docker port docker ps docker pull docker push docker rename docker restart docker rm docker rmi docker run docker save docker search docker secret docker secret create docker secret inspect docker secret ls docker secret rm docker service docker service create docker service inspect docker service logs docker service ls docker service ps docker service rm docker service scale docker service update docker stack docker stack deploy docker stack ls docker stack ps docker stack rm docker stack services docker start docker stats docker stop docker swarm docker swarm ca docker swarm init docker swarm join docker swarm join-token docker swarm leave docker swarm unlock docker swarm unlock-key docker swarm update docker system docker system df docker system events docker system info docker system prune docker tag docker top docker unpause docker update docker version docker volume docker volume create docker volume inspect docker volume ls docker volume prune docker volume rm docker wait Use the Docker command line (Engine) Engine: Extend Access authorization plugin (Engine) Docker log driver plugins Docker network driver plugins (Engine) Extending Engine with plugins Managed plugin system (Engine) Plugin configuration (Engine) Plugins API (Engine) Volume plugins (Engine) Engine: Security AppArmor security profiles for Docker (Engine) Automation with content trust (Engine) Content trust in Docker (Engine) Delegations for content trust (Engine) Deploying Notary (Engine) Docker security (Engine) Docker security non-events (Engine) Isolate containers with a user namespace (Engine) Manage keys for content trust (Engine) Play in a content trust sandbox (Engine) Protect the Docker daemon socket (Engine) Seccomp security profiles for Docker (Engine) Secure Engine Use trusted images Using certificates for repository client verification (Engine) Engine: Tutorials Engine tutorials Network containers (Engine) Get Started Part 1: Orientation Part 2: Containers Part 3: Services Part 4: Swarms Part 5: Stacks Part 6: Deploy your app Machine Amazon Web Services (Machine) Digital Ocean (Machine) docker-machine active docker-machine config docker-machine create docker-machine env docker-machine help docker-machine inspect docker-machine ip docker-machine kill docker-machine ls docker-machine provision docker-machine regenerate-certs docker-machine restart docker-machine rm docker-machine scp docker-machine ssh docker-machine start docker-machine status docker-machine stop docker-machine upgrade docker-machine url Driver options and operating system defaults (Machine) Drivers overview (Machine) Exoscale (Machine) Generic (Machine) Get started with a local VM (Machine) Google Compute Engine (Machine) IBM Softlayer (Machine) Install Machine Machine Machine CLI overview Machine command-line completion Machine concepts and help Machine overview Microsoft Azure (Machine) Microsoft Hyper-V (Machine) Migrate from Boot2Docker to Machine OpenStack (Machine) Oracle VirtualBox (Machine) Provision AWS EC2 instances (Machine) Provision Digital Ocean Droplets (Machine) Provision hosts in the cloud (Machine) Rackspace (Machine) VMware Fusion (Machine) VMware vCloud Air (Machine) VMware vSphere (Machine) Notary Client configuration (Notary) Common Server and signer configurations (Notary) Getting started with Notary Notary changelog Notary configuration files Running a Notary service Server configuration (Notary) Signer configuration (Notary) Understand the service architecture (Notary) Use the Notary client
characters

Docker群体会生成两种不同的流量:

  • 控制和管理平面流量:这包括群管理消息,例如加入或离开群体的请求。此流量始终加密。

  • 应用程序数据平面流量:这包括集装箱流量和往返于外部客户端的流量。

本主题讨论如何管理群集服务的应用程序数据。有关通用群集网络的更多详细信息,请参阅Docker网络参考架构。

以下三种网络概念对群集服务非常重要:

  • 覆盖网络管理参与群集的Docker守护进程之间的通信。您可以创建覆盖网络,与独立容器的用户定义网络相同。您也可以将服务附加到一个或多个现有覆盖网络,以启用服务到服务的通信。覆盖网络是使用overlay网络驱动程序的Docker网络。

  • 入口网络是一个特殊的覆盖网络,便于服务的节点之间的负载均衡。当任何swarm节点在已发布的端口上收到请求时,它将该请求交给一个名为的模块IPVSIPVS跟踪参与该服务的所有IP地址,选择其中的一个,并通过ingress网络将请求路由到它。该ingress网络时自动创建初始化或加入一个群。大多数用户不需要定制其配置,但Docker 17.05和更高版本允许您这样做。

  • docker_gwbridge是一个桥接网络连接覆盖网(包括ingress网络)到单个码头工人守护进程的物理网络。默认情况下,服务运行的每个容器都连接到其本地Docker守护程序主机的docker_gwbridge网络。该docker_gwbridge网络时自动创建初始化或加入一个群。大多数用户不需要定制其配置,但是Docker允许您这样做。

防火墙考虑

参与swarm的Docker守护进程需要通过以下端口相互通信的能力:

  • 端口7946TCP / UDP用于容器网络发现。

  • 4789容器覆盖网络的端口UDP。

创建一个覆盖网络

要创建覆盖网络,请overlay在使用该docker network create命令时指定驱动程序:

$ docker network create \  --driver overlay \
  my-network

上述命令没有指定任何自定义选项,所以Docker分配一个子网并使用默认选项。您可以看到有关使用网络的信息docker network inspect

当没有容器连接到覆盖网络时,其配置并不令人兴奋:

$ docker network inspect my-network[    {        "Name": "my-network",        "Id": "fsf1dmx3i9q75an49z36jycxd",        "Created": "0001-01-01T00:00:00Z",        "Scope": "swarm",        "Driver": "overlay",        "EnableIPv6": false,        "IPAM": {            "Driver": "default",            "Options": null,            "Config": []        },        "Internal": false,        "Attachable": false,        "Ingress": false,        "Containers": null,        "Options": {            "com.docker.network.driver.overlay.vxlanid_list": "4097"        },        "Labels": null    }]

在上面的输出中,请注意,驱动程序是覆盖的,并且范围是swarm,而不是您可能在其他类型的Docker网络中看到的本地,主机或全局范围。 该范围表示只有参与群体的主机才能访问此网络。

网络的子网和网关是在服务首次连接到网络时动态配置的。以下示例显示了与上述相同的网络,但有三个容器的redis服务连接到它。

$ docker network inspect my-network[    {        "Name": "my-network",        "Id": "fsf1dmx3i9q75an49z36jycxd",        "Created": "2017-05-31T18:35:58.877628262Z",        "Scope": "swarm",        "Driver": "overlay",        "EnableIPv6": false,        "IPAM": {            "Driver": "default",            "Options": null,            "Config": [                {                    "Subnet": "10.0.0.0/24",                    "Gateway": "10.0.0.1"                }            ]        },        "Internal": false,        "Attachable": false,        "Ingress": false,        "Containers": {            "0e08442918814c2275c31321f877a47569ba3447498db10e25d234e47773756d": {                "Name": "my-redis.1.ka6oo5cfmxbe6mq8qat2djgyj",                "EndpointID": "950ce63a3ace13fe7ef40724afbdb297a50642b6d47f83a5ca8636d44039e1dd",                "MacAddress": "02:42:0a:00:00:03",                "IPv4Address": "10.0.0.3/24",                "IPv6Address": ""            },            "88d55505c2a02632c1e0e42930bcde7e2fa6e3cce074507908dc4b827016b833": {                "Name": "my-redis.2.s7vlybipal9xlmjfqnt6qwz5e",                "EndpointID": "dd822cb68bcd4ae172e29c321ced70b731b9994eee5a4ad1d807d9ae80ecc365",                "MacAddress": "02:42:0a:00:00:05",                "IPv4Address": "10.0.0.5/24",                "IPv6Address": ""            },            "9ed165407384f1276e5cfb0e065e7914adbf2658794fd861cfb9b991eddca754": {                "Name": "my-redis.3.hbz3uk3hi5gb61xhxol27hl7d",                "EndpointID": "f62c686a34c9f4d70a47b869576c37dffe5200732e1dd6609b488581634cf5d2",                "MacAddress": "02:42:0a:00:00:04",                "IPv4Address": "10.0.0.4/24",                "IPv6Address": ""            }        },        "Options": {            "com.docker.network.driver.overlay.vxlanid_list": "4097"        },        "Labels": {},        "Peers": [            {                "Name": "moby-e57c567e25e2",                "IP": "192.168.65.2"            }        ]    }]

自定义覆盖网络

有些情况下,您不想使用覆盖网络的默认配置。有关可配置选项的完整列表,请运行该命令docker network create --help。以下是一些最常见的更改选项。

配置子网和网关

默认情况下,网络的子网和网关在第一个服务连接到网络时自动配置。使用--subnet--gateway标志创建网络时,您可以配置这些设置。以下示例通过配置子网和网关扩展了前一个示例。

$ docker network create \  --driver overlay \  --subnet 10.0.9.0/24 \  --gateway 10.0.9.99 \
  my-network

配置应用程序数据的加密

与群体有关的管理和控制平面数据始终是加密的。有关加密机制的更多详细信息,请参阅Docker群集模式覆盖网络安全模型。

swarm节点之间的应用程序数据默认不加密。要在给定的覆盖网络上加密此流量,请使用此--opt encrypted标志docker network create。这使得vxlan级别的IPSEC加密成为可能。这种加密会带来不可忽视的性能损失,所以您应该在生产中使用它之前对其进行测试。

将服务附加到覆盖网络

要将服务附加到现有的重叠网络,请将--network标志传递给docker service create或将--network-add标志传递给docker service update

$ docker service create \  --replicas 3 \  --name my-web \  --network my-network \
  nginx

连接到覆盖网络的服务容器可以通过它相互通信。

要查看服务连接到哪个网络,请使用docker服务ls查找服务的名称,然后使用docker service ps <service-name>列出网络。 或者,要查看哪些服务的容器连接到网络,请使用docker网络检查<network-name>。 您可以从任何加入到swarm并处于运行状态的swarm节点运行这些命令。

配置服务发现

服务发现是Docker用来将来自服务的外部客户端的请求路由到单个群集节点的机制,无需客户端需要知道有多少节点参与服务或其IP地址或端口。您不需要发布在同一网络上的服务之间使用的端口。例如,如果您有一个将其数据存储在MySQL服务中的WordPress服务,并且它们连接到相同的覆盖网络,则不需要将MySQL端口发布到客户端,只需发布WordPress HTTP端口。

使用虚拟IP(VIP)或DNS循环(DNSRR),服务发现可以以两种不同的方式工作。您可以配置此服务。

  • 默认情况下,当您将服务附加到网络并且该服务发布一个或多个端口时,Docker会为该服务分配一个虚拟IP(VIP),这是客户端到达该服务的“前端”。Docker保存服务中所有工作节点的列表,并在客户端和其中一个节点之间路由请求。来自客户端的每个请求可能会路由到不同的节点。

  • 如果您将服务配置为使用DNS轮询(DNSRR)服务发现,则不会有单个虚拟IP。相反,Docker会为服务设置DNS条目,以便服务名称的DNS查询返回一个IP地址列表,并且客户端直接连接到其中的一个。在您想使用自己的负载均衡器的情况下,DNS循环法很有用。要配置服务以使用DNSRR,请--endpoint-mode dnsrr在创建新服务或更新现有服务时使用该标志。

自定义入口网络

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

定制ingress网络涉及到删除和重新创建网络。这通常在群体中创建任何服务之前完成。如果您有现有的发布端口的服务,则需要先删除这些服务,然后才能删除ingress网络。

在没有ingress网络存在的时间内,不发布端口的现有服务将继续运行,但没有负载平衡。这会影响发布端口的服务,例如发布端口80的WordPress服务。

  1. 使用检查ingress网络docker network inspect ingress,并删除任何容器连接到它的服务。这些是发布端口的服务,例如发布端口80的WordPress服务。如果所有这些服务都未停止,则下一步将失败。

  2. 删除现有的ingress网络:$ docker network rm ingress警告!在删除路由网格网络之前,请确保群集中的所有节点运行相同的码头引擎版本。否则,删除可能无效,新建入口网络的功能将受到影响。你确定你要继续吗?Y / N

  3. 使用该--ingress标志创建一个新的覆盖网络,以及要设置的自定义选项。本示例将MTU设置为1200,将子网设置为10.11.0.0/16,并将网关设置为10.11.0.2。$ docker network create \ -d overlay \ --ingress \ --subnet = 10.11.0.0 / 16 \ --gateway = 10.11.0.2 \ --opt com.docker.network.mtu = 1200 \ my-ingress   注意:您可以为你的ingress网络命名除了ingress,但你只能有一个。尝试创建第二个将失败。

  4. 重新启动您在第一步中停止的服务。

自定义docker_gwbridge

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

您需要brctl在您的操作系统上安装该应用程序才能删除现有的网桥。包名是bridge-utils

  1. 停止Docker。

  2. 使用该brctl show docker_gwbridge命令检查是否存在调用的桥接设备docker_gwbridge。如果是这样,请使用它brctl delbr docker_gwbridge

  3. 启动Docker。不要加入或初始化群体。

  4. docker_gwbridge使用您的自定义设置创建或重新创建桥梁。本例使用子网10.11.0.0/16。有关可自定义选项的完整列表,请参阅桥接驱动程序选项。$ 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 \ docker_gwbridge

  5. 初始化或加入群。

使用单独的界面进行控制和数据通信

默认情况下,所有swarm流量都通过相同的接口发送,包括控制和管理流量,以维护swarm本身以及数据流量进出服务容器。

在Docker 17.06及更高版本中,可以--datapath-addr在初始化或加入群集时通过传递该标志来分隔此流量。如果有多个接口,则--advertise-addr必须明确指定,如果未指定--datapath-addr--advertise-addr则默认为。关于加入,离开和管理群的--advertise-addr流量将通过接口发送,并且服务容器之间的流量将通过--datapath-addr接口发送。这些标志可以采用IP地址或网络设备名称,例如eth0

这个例子初始化一个独立的swarm --datapath-addr。它假定您的Docker主机有两个不同的网络接口:10.0.0.1应该用于控制和管理流量,192.168.0.1应该用于与服务相关的流量。

$ docker swarm init --advertise-addr 10.0.0.1 --datapath-addr 192.168.0.1

这个例子加入了由主机管理的swarm 192.168.99.100:2377,并将--advertise-addr标志设置为eth0,并将--datapath-addr标志设置为eth1

$ docker swarm join \  --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2d7c \  --advertise-addr eth0 \  --datapath-addr eth1 \  192.168.99.100:2377

扩展内容

  • 将服务部署到群集

  • 群体管理指南

  • Docker CLI参考

  • Swarm模式教程

  • Docker网络参考架构

Previous article: Next article: