Docker Swarm is a container orchestration built on Docker Engine. It allows you to create and deploy a cluster of Docker nodes with multiple servers. Docker Swarm simplifies the deployment of your containerized application in a service. Docker Swarm provides a simple and straightforward way to manage and orchestrate containers.

Docker Swarm provides a high level of availability for applications. In Docker Swarm, you can run a single application or service on multiple nodes called “worker nodes.” There is also a node called “Swarm Manager” that provides centralized management and orchestration of Docker Swarm.

This tutorial shows you how to set up Docker Swarm on Debian 11 servers. You will install Docker, set up the UFW (Uncomplicated Firewall), initialize the “Swarm Manager” and add “Nodes”. You’ll also learn the basic use of Docker Swarm for deploying applications.

Prerequisites

To complete this tutorial, you will need multiple Debian 11 servers and a non-root user with sudo/root administrator privileges.

In this demonstration, we will use three Debian 11 servers with the following details:

Hostname    IP Address        Used as

-------------------------------------

manager     192.168.5.100     Swarm Manager

node1       192.168.5.120     Node

node2       192.168.5.121     Node

With all of these requirements in place, you are ready to start.

Installing Docker

The default Debian repository provides a package for Docker. But, to ensure that you are installing the latest version of Docker, you will install Docker via the official Docker repository.

Also, you must install Docker on all of your Debian servers.

Before adding the repository, run the following apt command to install some basic packages. When prompted to confirm the installation, input Y and press ENTER.

sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release -y

<img alt="install dependencies" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/1-install-dependencies.png633489d56e474.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="307" loading="lazy" src="data:image/svg xml,” width=”750″>

After installing basic packages, add the GPG key for the Docker repository using the following command. With this command, you will create a new directory “/etc/apt/keyrings” and store the GPG key of the Docker repository in it.

sudo mkdir -p /etc/apt/keyrings

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Next, add the Docker repository for the Debian Linux system using the command below.

echo 

  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian

  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Now, update and refresh your package index via the apt command below.

sudo apt update

You will see the new Docker repository being updated.

<img alt="add repository" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/2-add-repository.png633489d92402f.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="250" loading="lazy" src="data:image/svg xml,” width=”750″>

Next, install Docker packages using the following apt command. You will be asked to confirm the installation, input Y to confirm, and press ENTER to proceed.

sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin

<img alt="install docker" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/3-install-docker.png633489da539db.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="302" loading="lazy" src="data:image/svg xml,” width=”750″>

After installing Docker, run the following command to start and enable the ‘docker‘ service. This allows ‘docker’ to run automatically at system boot.

sudo systemctl start docker

sudo systemctl enable docker

Lastly, verify the ‘docker‘ service via the systemctl command below.

sudo systemctl status docker

If the ‘docker‘ service is running, you should see the output like the following screenshot:

<img alt="check service status" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/4-check-docker-service.png633489dfbe3a5.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="268" loading="lazy" src="data:image/svg xml,” width=”750″>

Additionally, if you plan to use a non-root user for your deployment, you must add your user to the ‘docker‘ and allow the user to run Docker containers.

sudo usermod -aG docker user

Setting up Firewall

If you are deploying Docker Swarm for production, you must set up the firewall between all nodes. In this example, you will use UFW as the default firewall for your Debian servers.

Below are some ports that you must open for the Docker Swarm to work properly:

  • TCP port 2377 for cluster management communications – this is used if you have multiple Swarm Manager.
  • TCP and UDP port 7946 for communication among nodes – for all nodes on the Swarm environment.
  • UDP port 4789 for overlay network traffic

Install UFW to your Debian systems using the following apt command.

sudo apt install ufw -y

After UFW is installed, run the following ‘ufw‘ command to add the “OpenSSH” application profile. Then, start and enable UFW.

During the process, you will be asked to confirm whether to run and enable the UFW firewall. Input y to confirm and press ENTER to proceed.

sudo ufw allow "OpenSSH"

sudo ufw enable

<img alt="enable ufw firewall" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/6-enable-firewall.png633489e0704e1.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="205" loading="lazy" src="data:image/svg xml,” width=”738″>

Now run the following ufw command to open some ports for Docker Swarm.

sudo ufw allow 2377/tcp

sudo ufw allow 7946/tcp

sudo ufw allow 7946/udp

sudo ufw allow 4789/udp

Lastly, verify the list of enabled UFW rules on your server via the following ufw command. Be sure all ports for Docker Swarm is enabled and available on UFW.

sudo ufw status

<img alt="check firewall" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/8-check-firewall-status.png633489e1780b4.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="335" loading="lazy" src="data:image/svg xml,” width=”507″>

At this point, you have Docker installed and the UFW configured. Let’s initialize Swarm Manager next.

Initializing Swarm Manager

Swarm Manager is the center of the Docker Swarm deployment. It manages the swarm state itself and has no limit on the number of the manager nodes. Deploying multiple Swarm Manager will create a fault-tolerance environment.

The Swarm Manager is cluster management that is integrated with docker Engine. it provides a declarative service model, service scaling, desired state of services, load balancing, and rolling updates.

In this example, you will initialize one node as the Swarm Manager.

To initialize the Swarm Manager, run the following docker command. In this example, the Swarm manager will be running on the IP address 192.168.5.30. And the network for containers on the Docker Swarm environment will be 10.20.0.0/16.

docker swarm init --advertise-addr 192.168.5.30 --default-addr-pool 10.20.0.0/16

When Swarm Manager is initialized, you will see the following output. Also, you can see the details token for joining Nodes to the Swarm manager.

<img alt="initialize swarm manager" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/9-initialize-manager.png633489e37d297.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="140" loading="lazy" src="data:image/svg xml,” width=”750″>

Now, run the following docker command to verify the Swarm mode is enabled.

docker info

In the following screenshot, you can see the Swarm mode is “active” with the pool of IP address “10.20.0.0/16“. Also, the current node that is available on the Docker Swarm is “1“.

<img alt="check swarm mode" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/10-check-swarm.png633489e43c04f.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="321" loading="lazy" src="data:image/svg xml,” width=”500″>

You can verify the list of nodes on the Docker Swarm via the docker command below. You should see there is only one node on the current Docker Swarm, which is the Swarm Manager.

docker node ls

<img alt="check list node" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/11-check-list-node.png633489e4c8721.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="109" loading="lazy" src="data:image/svg xml,” width=”750″>

Adding Worker Nodes to Swarm

Worker nodes is where containers are executed and running. On the worker node, the Docker Engine must be installed on each server, and also the worker node can be promoted as the Swarm Manager.

To add a worker node, you can use the “docker swarm join” command followed by the token that is generated when you initialize the Swarm Manager.

Add both ‘node1‘ and ‘node2‘ servers to the Docker Swarm via the “docker swarm join” command below. You can replace the token with your generated token during the Swarm Manager initialization.

docker swarm join 

--token SWMTKN-1-5tui5dveqw6n2b0m6suut0w7nf9w0wb5jyvfhmykb4jdehps1j-ag99f7750mmnim6ao5yp0x3s2

192.168.5.30:2377

When the node is added, you will get the output message such as “This node joined a swarm as a worker“.

Below is the output that you will see on both ‘node1‘ and ‘node2‘ servers.

<img alt="join node1" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/12-join-node1.png633489e56f611.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="127" loading="lazy" src="data:image/svg xml,” width=”750″>

Also, if you lost the swarm token for joining the worker node, you can show using the following command.

docker swarm join-token worker

Lastly, back to the “manager” server and verify the list of nodes on your Docker Swarm environment via the “docker” command below.

docker node ls

In the following screenshot, you can see that both “node1” and “node2” is added to the Docker Swarm with the status “Ready” and the docker engine “20.10.18“.

<img alt="check node ls" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/13-check-nodes.png633489e649edb.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="142" loading="lazy" src="data:image/svg xml,” width=”750″>

The Docker Swarm deployment is complete, you can now deploy your containerized application to the Swarm.

Managing Services on Docker Swarm

Service is a term of application deployment on Docker Swarm. It’s the image for your microservices, it could be an HTTP server, a database server, or other applications that will be running on your Docker Swarm.

To set up a service in Docker Swarm, you must specify the image that you will be using, the port that will be exposed, and also the number of replicas.

After you have deployed Docker Swarm, you will now learn the basic deployment of your services to Docker Swarm.

Create a new service “test-httpd” via the command “docker service create” as below.

In this example, you will create a new service “test-httpd” with the 1 replica and expose port “8000” on the Swarm node. The image that you will be using is “httpd:alpine”.

docker service create --replicas 1 --name test-httpd -p 8000:80 httpd:alpine

<img alt="run service" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/14-run-service.png633489e6b421e.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="139" loading="lazy" src="data:image/svg xml,” width=”750″>

Once the “test-httpd” service is running, verify using the following command. You should see the service “test-httpd” is running with 1 replica and expose the TCP port 8000.

docker service ls

<img alt="list service" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/15-list-service.png633489e71eaee.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="176" loading="lazy" src="data:image/svg xml,” width=”750″>

Now, run the following command to check the details service “test-httpd”.

docker service inspect test-httpd

docker service inspect --pretty test-httpd

Below you can see the detailed service of “test-httpd”, this includes the number of replicas, external ports, the base image that you are using, etc.

<img alt="inspect service" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/16-inspect-service.png633489e8bc86b.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="617" loading="lazy" src="data:image/svg xml,” width=”598″>

You can also check the running container on the “test-httpd” service via the “docker service ps” command as below.

docker service ps test-httpd

You can see the “test-httpd.1” container is running on the “manager” server.

<img alt="list running container" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/17-list-container-running.png633489e966bab.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="98" loading="lazy" src="data:image/svg xml,” width=”750″>

Now open the port “8000” on all servers via the ufw command below.

sudo ufw allow 8000/tcp

Then, verify the “test-httpd” service using the curl command as below. The “192.168.5.30” is the Swarm Manager IP address and the “test-httpd” service is running on port 8000.

curl 192.168.5.30:8000

curl -I 192.168.5.30:8000

You will see the default index.html page of the “test-httpd” service.

<img alt="access service" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/18-access-service.png633489ea0ecc6.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="276" loading="lazy" src="data:image/svg xml,” width=”484″>

Scaling Services on Docker Swarm

Now you will learn how to scale your services on Docker Swarm.

Run the following command to scale the “test-httpd” service to 3 replicas.

docker service scale test-httpd=3

<img alt="scale service" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/19-scale-service.png633489eaa7883.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="207" loading="lazy" src="data:image/svg xml,” width=”652″>

Now verify the running container on the “test-httpd” service using the command below. You should see three different containers on the “test-http” service, which is running on server manager, node1, and node2.

docker service ps test-httpd

<img alt="list service" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/20-list-service.png633489eb5ee14.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="121" loading="lazy" src="data:image/svg xml,” width=”750″>

Lastly, run the following docker command to verify the running container on the node1 and node2 servers. You should see the container with the name “test-httpd.random name” is running on both servers. Also, you can see that the container is accessible via TCP port 8000.

Checking container on node1.

docker ps

curl node1:8000

<img alt="check from node1" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/21-check-container-node1.png633489ebb0625.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="117" loading="lazy" src="data:image/svg xml,” width=”750″>

Checking container on node2.

docker ps

curl node2:8000

<img alt="check node2" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/22-check-container-node2.png633489ecafd64.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="100" loading="lazy" src="data:image/svg xml,” width=”750″>

Deleting Services on Docker Swarm

Now is the time to clean up your environment by deleting services that you have deployed to Docker Swarm.

Run the following command to remove and delete the service “test-httpd” from the Docker Swarm.

docker service rm test-httpd

Then, verify the “test-httpd” service via the “docker service inspect” command below. You should see the service is deleted.

docker service inspect test-httpd

<img alt="service deleted" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/23-delete-service.png633489ef59b06.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="238" loading="lazy" src="data:image/svg xml,” width=”682″>

Additionally, you can also verify via the docker command below to check the list of running containers and images on your Swarm.

You should see the container is removed and the httpd:alpine image is available on all of the servers.

docker ps

docker images

<img alt="check service deleted" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/24-verify-service.png633489efa8f82.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="187" loading="lazy" src="data:image/svg xml,” width=”657″>

Conclusion

In this tutorial, you have deployed the Docker Swarm with three Debian 11 servers. You have also learned the basic installation of Docker Engine on Linux server and the basic configuration of UFW firewall on Debian system. And finally, you have also learned the basic application deployment to Docker Swarm via the “docker service” command.

From here, you can now deploy your containerized applications with all of the dependencies to the Docker Swarm. You can also add more Swarm Manager to set up the high availability of your Docker Swarm environment.