Using docker-machine to deploy multi-manager docker swarm

Of course, HA is a must in production environment. So, you gonna need multi-manager docker swarm. It is super easy when you know how.

First, you cannot use token, you need a discovery service (consul, etcd, zookeeper). So, create one if you don’t have, e.g.

$ docker-machine create -d virtualbox consul
$ docker-machine ssh consul
[email protected]:~$ docker run -d --name consul \
  --net host gliderlabs/consul-server \
  -advertise 192.168.0.100 \
  -bootstrap-expect 1

Next,  create multiple swarm managers (we have obsoleted the term swarm master, though.)

$ docker-machine create -d virtualbox --swarm \
  --swarm-master \
  --swarm-opt replication \
  --swarm-discovery consul://192.168.0.100:8500 \
  --engine-opt cluster-store=consul://192.168.0.100:8500 \
  manager-0 
$ docker-machine create -d virtualbox --swarm \
  --swarm-master \
  --swarm-opt replication \
  --swarm-discovery consul://192.168.0.100:8500 \
  --engine-opt cluster-store=consul://192.168.0.100:8500 \
  manager-1

Note that you need –swarm-master and –swarm-opt replication to make this works. Now, you have it. You can either

$ eval $(docker-machine env --swarm manager-0)

or

$ eval $(docker-machine env --swarm manager-1)

Check the “role” line, one will be the primary, the others will be replicas.

Normally, you would want to set docker environment to the primary. Somehow, if the primary has failed, one of the replicas will takeover and you can set the environment to the replica to control your swarm cluster. You can later recover the failed manager, or create a new manager.

Easy, right ? ;)