Docker – 6. Swarm

Docker Swarm allows to work with multiple host systems.

Docker Swarm can signify a swarm of containers that are located on multiple host systems. Specific installation of project Docker on specific host systems are from the perspective of Docker Swarm calld nodes. There are leading nodes called managers, while other nodes are workers. Managers are relaying tasks to the workers, which are defined in the services.

Hint: Docker is running perfectly on WebSupport VPS. Try out the premium virtual server today!

Installation

We will introduce you a small company (or more precisely a swarm) that has one manager (owner, director) and he employs two workers (worker-1 and worker-2). So we would be able to connect the Manager to his Workers in the Docker Swarm tool, we need to learn the IP addresses of the computer with manager node and also with the workers node. Speaking plainly: Manager and workers has to be first introduces. On all tree computers we will run the command:

hostname -I

and the first IP address will be the one we will need in this guide. Here we have the IP address of the manager 10.1.1.21, worker-1 has the address 10.1.1.9 and worker-2 has 10.1.1.10. Your IP address may differ.

Docker may be installed on the manager host device, but on the other two (worker-1 and worker-2) Docker may yet be installed. We will log in via SSH to worker-1 (10.1.1.9) and worker-2 (10.1.1.10) and proceed:

Firstly it is necessary tu secure the updates of all packages. We will install all updated software via two command in one go:

sudo apt update && sudo apt upgrade

If the system localtes any updated packages, it will ask you, if you want to proceed with their installation:

Do you want to continue? [Y/n]

Press the key y to start the update or press n, to cancel the update. Then press the Enter key.

For installing project Docker we will use official script that will ease the installation to 2 simple commands:

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

After we finish the installation on both host computers we are all set and we can proceed to the tool Docker Swerm.

Manager

On the host computer with IP address of the manager (here 10.1.1.21, your IP address may differ) we will create a swarm using this command:

docker swarm init --advertise-addr 10.1.1.21

Next steps will be displayed right after we create the swarm on the manager computer. To connect the workers to the swarm, we need to execute the following command on both worker computers (here 10.1.1.9 and 10.1.1.10, your’s may differ):

docker swarm join --token SWMTKN-1-0e8pb0jgkf5fh0q9ttig4so3lhzdbwr1rha9dqcczvxryqdw27-1loythwbn59jxbkx2vj1qouxv 10.1.1.21:2377

After we create the swarm on the manager computer, an unique identifier is created (a token – here SWMTKN-1-0e8pb0jgkf5fh0q9ttig4so3lhzdbwr1rha9dqcczvxryqdw27-1loythwbn59jxbkx2vj1qouxv, your’s will differ). It is, in essence, the company name that was created by the manager. All workers who want to work in this company (join), have to know, except the name (token), the IP address and port as well (here 10.1.1.21:2377, your’s may differ). Simply put: Manager has created his company, selected a name and accepted two workers that has just got to the job (service).

That the both workers (worker-1 a worker-2) are successfully connected, we can check from the confirmation message on the host computers of the workers, and also on the manager host computer via command listing all manager and worker nodes:

docker node ls

A list of managers and workers connected to the swarm is displayed with all their details.

On all 3 nodes we now need to run web server Nginx and opet the port from inside of the container to the host computer (-p 80:8080). Using the tool Docker Swarm we create a service we name nginx-swarm-test (--name nginx-swarm-test) and define that we want 3 copies of web server Nginx (--replicas=3). So we could work in the command line after the service is executed we will use a switch -d. Execute this command on the managers computer:

docker service create --name nginx-swarm-test --replicas=3 -d -p 80:8080 nginx

By that we secure that on all 3 devices the web server Nginx will run. We can check if the service called nginx-swarm-test is running (first line) or if some tasks were created on all three nodes under this service (second line). Simply put: Manager created tasks for himself and for his workers in the service.

docker service ls
docker service ps nginx-swarm-test

If we would like to see, what page is sent to each node, use the tool curl and IP address of the node. Logged in on the manager device enter these 3 commands:

curl localhost:8080
curl 10.1.1.9:8080
curl 10.1.1.10:8080

Summary

We have shown you how to use the tool Docker Swarm and how to work with multiple installation of Docker on multiple host systems.

Updated on August 10, 2021

Was this article helpful?

Related Articles