What is a Docker Swarm?

A Docker Swarm is a group of either physical or virtual machines that are running the Docker application and that have been configured to join together in a cluster. Once a group of machines have been clustered together, you can still run the Docker commands that you’re used to, but they will now be carried out by the machines in your cluster. The activities of the cluster are controlled by a swarm manager, and machines that have joined the cluster are referred to as nodes.

What is Docker Swarm used for?

Docker swarm is a container orchestration tool, meaning that it allows the user to manage multiple containers deployed across multiple host machines.

One of the key benefits associated with the operation of a docker swarm is the high level of availability offered for applications. In a docker swarm, there are typically several worker nodes and at least one manager node that is responsible for handling the worker nodes’ resources efficiently and ensuring that the cluster operates efficiently.

Scenario

We want to configure a swarm two worker nodes and one manager node.

Solution

In our example all three hosts will run Ubuntu 18.04, Their hostnames would be

m1: The manager node

c1 and c2: The worker nodes

docker_user: is the user

Create the following file (install_docker.sh)

To all three servers copy the script, mark it as executable and do the following:

Those commands installed docker to all three hosts and added our user to the docker group, this is needed to allow running docker without the use of sudo, don't forget to log-out and log in again to the shell for the group changes to get applied.

Define the swarm manager

If everything goes fine docker should be installed on all three hosts but the swarm is not created yet, On m1 enter the following:

Doing this we promote m1 host as the swarm manager, the IP address should be an IP reachable from the workers that we want to join the swarm.

If everything gone well you should see something like the following:

This message confirms that the swarm created, this server is the swarm manager and also provides further instructions on how to add more workers or managers, we will need to copy the docker swarm join part and paste it on the c1 and c2 shells

If everything gone well you should see the following:

Confirming the swarm creation

At m1 shell enter:

This command shows the number of nodes that have joined the swarm and their status, the output should be something like this

All three servers are active and m1 is marked as “Leader”.

I hope you found this article interesting :)

Written by

DevOps engineer, loves Linux, Python, cats and Amiga computers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store