How to Setup Consul Multinode Cluster with Docker?

06 / Mar / 2017 by Shivam Agrawal 3 comments

consul-logo

Consul is a service discovery and a configuration system tool used to discover the services running on multiple nodes. It is both distributed and highly available. It provides a powerful interface to get a better picture of infrastructure and services running on them. Consul gives us a variety of features that help to determine our infrastructure in a better way such as service and node discovery mechanism, health check, tagging system, system-wide key/value storage, consensus-based election routines and so on.

In this blog, we showcase how to create a three-server cluster. We run the Consul master in docker containers and therefore we have it installed in all the three nodes.

Let us first set up a consul container on consul-node1 with a –bootstrap-expect flag. The number associated with this flag will be the number of nodes required to start bootstrapping. If you give this flag in all the nodes either of it will start the bootstrap process. Alternatively, node that is assigned the flag will start in particular.

docker run -d -h consul-node1 -v /mnt:/data \
    -p 192.168.33.60:8300:8300 \
    -p 192.168.33.60:8301:8301 \
    -p 192.168.33.60:8301:8301/udp \
    -p 192.168.33.60:8302:8302 \
    -p 192.168.33.60:8302:8302/udp \
    -p 192.168.33.60:8400:8400 \
    -p 192.168.33.60:8500:8500 \
    -p 172.17.0.1:53:53/udp \
    progrium/consul -server -advertise 192.168.33.60 -bootstrap-expect 3

In this command, 192.168.33.60 is the machine IP while 172.17.0.1 is the docker0 interface IP. When we see the logs of the container it shows:

consul1

Similarly, when we see the UI of the consul we see no nodes appearing.

consul2

Now run the consul containers on the two other machines with –advertise flag and –join flag. With –join flag we give the IP of the bootstrapping node, i.e, 192.168.33.60.

docker run -d -h consul-node2 -v /mnt:/data  \
    -p 192.168.33.61:8300:8300 \
    -p 192.168.33.61:8301:8301 \
    -p 192.168.33.61:8301:8301/udp \
    -p 192.168.33.61:8302:8302 \
    -p 192.168.33.61:8302:8302/udp \
    -p 192.168.33.61:8400:8400 \
    -p 192.168.33.61:8500:8500 \
    -p 172.17.0.1:53:53/udp \
    progrium/consul -server -advertise 192.168.33.61 -join 192.168.33.60

When the other container is up and running, we see the logs of the first container. You can see that it tries to join the cluster. However, since there are only two nodes,  bootstrap process has not yet begun.

consul3

Next, run the third consul container on consul-node3 and observe the logs of the first container. As you can see, the cluster leadership is complete and we have 3 node consul cluster functioning.

docker run -d -h consul-node3 -v /mnt:/data  \
    -p 192.168.33.62:8300:8300 \
    -p 192.168.33.62:8301:8301 \
    -p 192.168.33.62:8301:8301/udp \
    -p 192.168.33.62:8302:8302 \
    -p 192.168.33.62:8302:8302/udp \
    -p 192.168.33.62:8400:8400 \
    -p 192.168.33.62:8500:8500 \
    -p 172.17.0.1:53:53/udp \
    progrium/consul -server -advertise 192.168.33.62 -join 192.168.33.60

consul4

consul5

Consequently, when we run a docker container on consul-node3, it is not discovered by consul because we are not running any registrators. Thus we need to run 3 registrators for and on each node so that the data is in sync. These registrators give necessary information of the cluster to the consul master.

docker run -d --name=registrator1 --net=host --volume=/var/run/docker.sock:/tmp/docker.sock gliderlabs/registrator:latest consul://192.168.33.60:8500
docker run -d --name=registrator2 --net=host --volume=/var/run/docker.sock:/tmp/docker.sock gliderlabs/registrator:latest consul://192.168.33.61:8500
docker run -d --name=registrator3 --net=host --volume=/var/run/docker.sock:/tmp/docker.sock gliderlabs/registrator:latest consul://192.168.33.62:8500

This will be done on all the nodes. As you can see, after running registrators on all nodes, the services are running perfectly.

consul6

We are through with setting up Consul Multinode Cluster With Docker. I hope the blog was useful. Watch out this space for more such knowledge.

FOUND THIS USEFUL? SHARE IT

comments (3)

  1. abiya

    Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.

    Reply

Leave a comment -