Scenario:
I'm building and configuring one Docker Swarm cluster. My idea is to deploy 2 sets of 3 virtual machines in 2 separated Hosts (HostA
and HostB
), using Consul as discovery backend:
HostA
: manager0
, node0
, node1
HostB
: manager1
, node2
, node3
By this way, I get the desired redundancy in both the Swarm managers and nodes. However, my current situation is in an earlier and much simpler stage: I've just spun a single Docker Swarm manager as follow:
docker \
run -d \
-p 3376:3376 \
swarm manage \
-H :3376 --advertise manager0:3376 \
--replication consul://consul0:8500/
(Note: The command above has been adapted for this question as some irrelevant arguments such as TLS verification have been cleared.)
This first seed of the Swarm cluster works as expected:
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
Question:
Let's say I successfully address the initial plan and I add a second manager, the secondary. As far as I can see, Consul maintain the information about the Swarm cluster, it knows who is the primary and the secondary, it performs healthchecks and so forth.
Now, I wear my deployer hat. I want to connect my Docker client to the Swarm cluster in order to deploy, for instance, my nginx-based services. I don't want to manually connect to either manager0
or manager1
depending on which one is the primary in each case, I'd like to use a single endpoint to the whole cluster. So:
How do I use Consul DNS resolution to point to the correct Swarm manager using a single endpoint? For example:
docker -H tcp://swarm.cluster.example.com:3376 version
instead of either:
docker -H tcp://manager0:3376 version
or
docker -H tcp://manager1:3376 version
I think that HostA
and HostB
should use Consul as a DNS Server. To do that, you need change default DNS Server Port of Consul to 53. After that, you change Domain Name Server of Hosts (file /etc/resolv.conf).
See the 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.