Docker: Create a swarm
What is a Docker Swarm?
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)#!/bin/bashapt-get update apt-get -y install apt-transport-https ca-certificates curl gnupg-agent software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - apt-key fingerprint 0EBFCD88 add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" apt-get update apt-get install -y docker-ce=5:18.09.5~3-0~ubuntu-bionic docker-ce-cli=5:18.09.5~3-0~ubuntu-bionic containerd.io
To all three servers copy the script, mark it as executable and do the following:$ chmod +x install_docker.sh
$ sudo -s
# ./install_docker.sh
# exit
$ sudo usermod -aG docker $(whoami)
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:
$ docker swarm init --advertise-addr <Host Private IP Address>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:Swarm initialized: current node (xf8ko0iqbmf99iycq039e56bo) is now a manager.To add a worker to this swarm, run the following command:docker swarm join --token SWMTKN-1-65i1n9yxn6flnenxf4vtdewkv3k74acmeldota2j46te5qcw5y-6e2o9b5xtqukjcybzvsrc9l9u 192.168.1.40:2377To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
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$ docker swarm join --token SWMTKN-1-65i1n9yxn6flnenxf4vtdewkv3k74acmwldotm2j46te5qcw5y-6e2o9b5xtqukjcybzvsrc9l9u 172.31.28.40:2377
If everything gone well you should see the following:This node joined a swarm as a worker.
Confirming the swarm creation
At m1 shell enter:$ docker node ls
This command shows the number of nodes that have joined the swarm and their status, the output should be something like thisID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
xf8kq039e6bo * m1 Ready Active Leader 18.09.5
tmj0lnk1t8ed c1 Ready Active 18.09.5
tdwvq0rq184f c2 Ready Active 18.09.5
All three servers are active and m1 is marked as “Leader”.
I hope you found this article interesting :)