Docker DCA – Creating a Docker Swarm

This post is intended to address the Docker DCA exam objective around setting up Docker Swarm, configuring managers, adding nodes and setting up a backup schedule.

What is Docker Swarm ?

Docker Swarm is a Docker feature than allows you to build a cluster of Docker servers (nodes) to run your Docker containers. Docker Swarm helps you with container orchestration, high availability and scaleability. You can read about the key concepts of Docker Swarm here.

A Swarm consists of multiple Docker hosts which run in ‘swarm mode’. Amongst these hosts, there are manager nodes (which manage the swarm) and worker nodes (which run the services/containers).  You can read about the features of swarm mode here.

In this article I’ll be setting up a small swarm demo environment, which I will build on in future posts as I work through the DCA objectives. For this I will use three Centos linux nodes, with Docker already installed and all on the same network segment. We’ll get started by creating a swarm manager node.

Creating a Docker Swarm and Docker Swarm Manager

The Swarm Manager is responsible for managing the Docker swarm. On the host you wish to be a swarm manager, run the following, ensuring you specifiy a static private IP on the correct network segment:

$ docker swarm init --advertise-addr 172.31.96.95

If the command worked correctly, you will get a message such as that shown below:

Swarm initialized: current node (3nx7gfbwz3rb41fiwewq9qcwm3v3r) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-54j209g5yhv1ssf9flrncuesfdqgaaqp9ajo9949zazdyxzj35scwc-5fw8cenw3mv75aa8ygivv5y5t 172.31.96.95:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

To get confirmation of the current status of the new swarm, run ‘docker node ls’, as shown below:

[host]$ docker node ls
ID                            HOSTNAME                            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
3nx7gfbwz3rb41fi9qcwm3v3r *   host1                               Ready               Active              Leader              18.06.0-ce

We can also run a ‘docker info’ on the manager node:

[host]$ docker info | grep Swarm -A5
Swarm: active
 NodeID: 3nx7gfbwz3fsdrb41fi9qcwm3v3r
 Is Manager: true
 ClusterID: qlcjoz601swlskw433dkct6re
 Managers: 1
 Nodes: 1

As you can see, we have the node listed as a swarm manager. Now we can move on and add two additional nodes to the cluster.

Docker Swarm Worker Nodes

After creating the swarm manager node, we can add some worker nodes to the swarm. To do so, we simply need to run the command that was shown after we ran the swarm init command on each node:

docker swarm join --token SWMTKN-1-54j209g5yhv1ssf9flrncuegaaqp9ajo9949zazdyfsdxyx35xzj35scwc-5fw8cenw3mv75aa8ygivv5y5t 172.31.96.95:2377

After running the command you should see the following message:

This node joined a swarm as a worker.

Running the docker info command on the worker nodes should show something like:

Swarm: active
 NodeID: 31b5ximbn32nfc20kmdr1gz67vx
 Is Manager: false
 Node Address: 172.31.98.10
 Manager Addresses:
  172.31.96.95:2377

On the manager node, running ‘docker node ls’ should now show:

$ docker node ls
ID                            HOSTNAME                            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
3nx7gfbfz3rb41fi9qcwm3v3r *   host1                               Ready               Active              Leader              18.06.0-ce
1cl9xphfzdriam7mg8mi75ts7     host2                               Ready               Active                                  18.06.0-ce
31b5ximbnnfc20dmdr1gz67vx     host3                               Ready               Active                                  18.06.0-ce

And that’s it – we now have a working docker swarm!

Docker Swarm Backup and Restore

It’s important to create backups of your swarm data, so that you can recover the swarm if anything happens to the swarm. Note that you should run backups on all the master nodes in a swarm as not all data is available on all master nodes. More info on this here – https://docs.docker.com/ee/admin/backup/back-up-swarm/.

To perform a backup you can run:

sudo systemctl stop docker
sudo tar -zvcf backup.tar.gz /var/lib/docker/swarm
sudo systemctl start docker

This will stop the Docker service, backup the entire /var/lib/docker/swarm directory, then start the Docker service.

To perform a restore, we can use the following steps:

sudo systemctl stop docker
sudo rm -rf /var/lib/docker/swarm/*
sudo tar -zxvf backup.tar.gz -C /var/lib/docker/swarm/
sudo systemctl start docker
docker node ls

As always, test out this process in a test environment to ensure it works for you!

Docker Swarm High Availability

I just wanted to add something on high availability, as it’s one of the objectives for the DCA exam. As always, in a production environment, it is a good idea to consider high availability/fault tolerance when designing a docker swarm. You should have multiple swarm manager nodes in your swarm, and be aware of how cluster nodes communicate and how to maintain a cluster quorum. You can read a lot more about these concepts in the following links:

You should learn about the part raft plays in the swarm, and look into the recommended approach for distributing management nodes across availability zones.

Learning Docker?

If you are starting out, then I highly recommend this book. Thirsty for more?

Then it’s time to take your Docker skills to the next level with this book (It’s my favorite). Also, check out my page on Docker Certification

Related posts

Mastering the Linux ifconfig Command

Docker Exec Command With Practical Examples

Debugging with Git Bisect

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More