Docker Swarm Multi-Manager Setup

13 / Oct / 2016 by Ranvijay Jamwal 0 comments

Docker Swarm has gained a lot of popularity in the last few months. It is Docker’s orchestration tool. Docker has been coming up with a lot of updates and Docker Swarm is very stable as compared to when it was launched. It is as Docker says “Production Ready.” You can go through the blog written by my colleague on how to setup Docker Swarm on AWS .

Screenshot from 2016-10-03 10:02:50

In this blog, I will be using Docker Machine to create my Docker Swarm nodes and masters using virtual box driver. Also, I will be using another virtual machine to set up Consul for Nodes and Masters discovery. Then I will also show you how setup HA for Docker Swarm Master (manager). So, let’s get started.

Consul will be used for node discovery. For now, I just have multiple Consul containers in one virtual box Docker Machine. For production, we can put those on different machines for HA.

Prerequisites

1. Docker Machine should installed. Install it from here.
2. VirtualBox installed should be above version 4.0.
3. Docker Engine should be present on your local machine.

Steps To Setup Your Swarm

1. First, we will create a machine for Consul.  Use the following command to create Consul machine:

[js]docker-machine ls
(to check the current state and details of machines)

docker-machine create -d=virtualbox consul-machine
(-d means driver for docker-machine, you can even use –driver virtualbox [/js]

Other drivers like AWS are also supported which will bring up new EC2 instances and make them a Docker Machine. You can read more on Docker documentation website.

Just connect to the machine using:

[js]docker-machine ssh machine-name [/js]

Now, you just need to create a Docker Compose file and enter the following contents:

[js]
myconsul:
image: progrium/consul
restart: always
hostname: consul
ports:
– 8500:8500
command: "-server -bootstrap"
[/js]

Install docker-compose from in the Consul machine by following the steps mentioned here.
Now, run the below command inside the Consul machine to get Consul up and running:

[js]docker-compose up -d
(if the filename of the compose file is docker-compose.yml else pass the filename explicitly using -f switch) [/js]

Just do a docker ps and netstat ntlp to check Consul is running on 8500 port of this machine.

2. Now that Consul is setup, let’s set up the two managers (masters) for Docker Swarm, one being primary and the other being a standby. This can be done simply by using the following two commands:

[js]
docker-machine create -d virtualbox –swarm \
–swarm-master \
–swarm-opt replication \
–swarm-discovery consul://192.168.99.114:8500 \
–engine-opt cluster-store=consul://192.168.99.114:8500 \
manager-0
[/js]

The command above will bring your first manager up, and the command given below should bring the next one up:

[js]
docker-machine create -d virtualbox –swarm \
–swarm-master \
–swarm-opt replication \
–swarm-discovery consul://192.168.99.114:8500 \
–engine-opt cluster-store=consul://192.168.99.114:8500 \
manager-1
[/js]

To connect to any of these managers, use the below given command:

[js]eval $(docker-machine env manager-0)[/js]

We will connect to the Swarm after the nodes are up. So, let’s move on to that.

3. Bringing up the nodes is just similar to using the above command without the –swarm-master. Use the command:

[js]docker-machine create -d virtualbox –swarm –swarm-discovery consul://192.168.99.114:8500 –engine-opt cluster-store=consul://192.168.99.114:8500 node-1 [/js]

4. Let’s connect to the Docker Swarm manager’s engine. For doing that just run the following command:

[js]eval $(docker-machine env –swarm manager-0)[/js]

Run docker ps to see what containers are running in the swarm, and it should show none.

Now, let’s run a new container say Ubuntu in the Swarm. By default new containers in the Swarm run on the nodes, manager-0. If you wish that containers never come up on the manager then just use drain on the manager. Now, run a container using the command:

[js]docker run -itd –name ubuntu1 ubuntu[/js]

Do a docker ps and see. It will show, on which node the container is running:

Screenshot from 2016-08-22 14:20:29

So, now you’re connected to Docker Swarm and every new container comes up in the Swarm.

To work inside any of these containers, you can login to the nodes or else from the manager you can just run docker exec -it container-name bash (bash or any other command) to get into the desired container.
There are some other features like you can launch a container on a node with a specific name, or where similar types of container are running using label and affinity.

5. Let’ see what happens if our primary manager goes down:

Screenshot from 2016-08-22 14:42:32

Running command to stop the manager-0:

Screenshot from 2016-08-22 14:44:48

Now, checking info of manager-1:

Screenshot from 2016-08-22 14:45:24

Woohoo! The secondary manager is now the primary. So, this is how you can easily setup your own Docker Swarm. The blog also portrays how failover of managers takes place in Docker Swarm using Docker Machine.

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *