如何为 Docker Swarm Manager 配置 DNS(and/or 负载平衡)

How to configure DNS (and/or loadbalancing) for Docker Swarm Manager

场景:

我正在构建和配置一个 Docker Swarm 集群。我的想法是在 2 个独立的主机(HostAHostB)中部署 2 组 3 个虚拟机,使用 Consul 作为发现后端:

HostA: manager0, node0, node1

HostB: manager1, node2, node3

通过这种方式,我在 Swarm 管理器和节点中都获得了所需的冗余。然而,我目前的情况处于更早且更简单的阶段:我刚刚旋转了一个 Docker Swarm 管理器,如下所示:

docker \
  run -d \
  -p 3376:3376 \
  swarm manage \
  -H :3376 --advertise manager0:3376 \
  --replication consul://consul0:8500/

(注意:上面的命令已针对此问题进行了修改,因为一些不相关的参数(例如 TLS 验证已被清除。)

Swarm 集群的第一个种子按预期工作:

docker -H tcp://manager0:3376 version
Client:
Version:      1.11.2
 API version:  1.23
 Go version:   go1.5.4
 Git commit:   b9f10c9
 Built:        Wed Jun  1 21:47:50 2016
 OS/Arch:      linux/amd64

Server:
 Version:      swarm/1.2.3
 API version:  1.22
 Go version:   go1.5.4
 Git commit:   eaa53c7
 Built:        Fri May 27 17:25:03 UTC 2016
 OS/Arch:      linux/amd64

问题:

假设我成功地解决了初始计划并添加了第二个经理,即辅助经理。据我所知,Consul 维护有关 Swarm 集群的信息,它知道谁是主要的和次要的,它执行健康检查等等。

现在,我戴上了我的部署者帽子。我想将我的 Docker 客户端连接到 Swarm 集群,以便部署,例如,我的基于 nginx 的服务。我不想手动连接到 manager0manager1,这取决于在每种情况下哪个是主要的,我想对整个集群使用一个端点。所以:

如何使用 Consul DNS 解析通过单个端点指向正确的 Swarm 管理器? 例如:

docker -H tcp://swarm.cluster.example.com:3376 version

而不是:

docker -H tcp://manager0:3376 version

docker -H tcp://manager1:3376 version

我认为 HostAHostB 应该使用 Consul 作为 DNS 服务器。为此,您需要将 Consul 的默认 DNS 服务器端口更改为 53。之后,您更改主机的域名服务器(文件 /etc/resolv.conf)。

查看 link https://www.consul.io/docs/agent/dns.html

The DNS query system makes use of health check information to prevent routing to unhealthy nodes. When a service query is made, any services failing their health check or failing a node system check will be omitted from the results. To allow for simple load balancing, the set of nodes returned is also randomized each time. These mechanisms make it easy to use DNS along with application-level retries as the foundation for an auto-healing service oriented architecture.