Docker is a tool that allows one to build, debug and deploy applications easily by using containers. It provides a lightweight environment to deploy and run an application. Docker swarm is a group of physical or virtual machines running the docker application and have been joined in a cluster. When joined one can issue a command to be executed by machines in the cluster. All these activities are controlled by a swarm manger and the joined machines are nodes.The key benefits of Docker swarm mode are; container self-healing, container scale up and scale down, load balancing service discovery and rolling updates

Both Docker and Kubernetes are container Orchestration tools. They share the following similarities:

  • They both get daily contributions from the Global community
  • They both receive regular updates by CNCF.

Docker and Kubernetes being similar in some way, they have the following differences.

DockerKubernetes
1.Does not have a web UI. Uses third party tools i.e portainer1. Has an easy web interface
2. Easy installation, install Docker Engine and assign IPs and opening ports2. Involves installing Kubectl first
3. Deploy and define applications using predefined swarm files3. Deployment involves describing declarative updates while updating Pods and ReplicaSets
4. Deploys containers quickly making orchestration faster4. Scaling involves creating new pods and it to nodes with available resources
5. Does not offer a monitoring solution out-of-the-box5. Offers multiple native logging and monitoring solutions

From the above comparison there are differences between the two tools. But in the end, they both solve challenges to digital transfomation.

This article demonstrates how to Setup Docker Swarm Cluster on Rocky Linux 8.

Setup Pre-requisites

In this guide, we will need 3 servers, one as a Docker manager and two nodes as below;

  1. Server 1- Manager hostname(dockermanager.example.com) IP- 192.168.1.27
  2. Server 2- Node 1 hostname(node1.example.com) IP- 192.168.1.26
  3. server 3- Node 2 hostname(node2.example.com) IP- 192.168.1.30

Then go to /etc/hosts file on all the servers with the command sudo vi /etc/hosts and add the line below replacing the IP_Addresses and Hostnames with your own.

192.168.1.27    dockermanager.example.com   dockermanager
192.168.1.26    node1.example.com         workernode1
192.168.1.30    node2.example.com         workernode2

1. Uninstall Older Version

We need to uninstall older versions of docker before we install a new version with their associated dependencies on all servers.

sudo dnf remove -y docker-common docker container-selinux docker-selinux docker-engine

2. Install Dependencies

We need to install docker dependencies on all servers as below:

sudo dnf install -y lvm2 device-mapper device-mapper-persistent-data device-mapper-event device-mapper-libs device-mapper-event-libs

Step 1- Install Docker Engine on Rocky Linux 8.

Now we proceed and install Docker Engine on the 3 Rocky Linux 8 servers through the following steps:

1. Add Docker Repository

Let us now add the required Docker repositories to our systems.

sudo dnf install -y dnf-utils
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

2. Install Docker Engine

With repositories added, we are set to install Docker Engine on Rocky Linux 8.

sudo dnf install -y docker-ce docker-ce-cli containerd.io

On all the nodes, start and enable the service to run on boot as below.

sudo systemctl start docker
sudo systemctl enable docker

Step 2 – Open Firewall Ports

After installing Docker Engine on the servers, we now open ports as below.

1. On Docker Manager

Allow the following ports on Docker manager as below.

sudo firewall-cmd --permanent --add-port=2376/tcp
sudo firewall-cmd --permanent --add-port=2377/tcp
sudo firewall-cmd --permanent --add-port=7946/tcp
sudo firewall-cmd --permanent --add-port=7946/udp
sudo firewall-cmd --permanent --add-port=4789/udp
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --reload

Proceed and restart Docker service on Docker manager.

sudo systemctl restart docker

2. On WorkerNodes

On Node1 and Node2 open the following ports

sudo firewall-cmd --permanent --add-port=2376/tcp
sudo firewall-cmd --permanent  --add-port=7946/tcp
sudo firewall-cmd --permanent --add-port=7946/udp
sudo firewall-cmd --permanent --add-port=4789/udp
sudo firewall-cmd --permanent --add-port=80/tcp
sudo firewall-cmd --reload

Now restart Docker service on the nodes.

sudo systemctl restart docker

Step 3- Start Docker Swarm On Rocky Linux 8

To initialize docker swarm Init execute the command below on Docker Manager. Replacing the IP_Address with the address of your Docker manager

sudo docker swarm init --advertise-addr 192.168.1.27

Sample output:

$ sudo docker swarm init --advertise-addr 192.168.1.27
Swarm initialized: current node (mbiap5ii2psyzozoun3553d8c) is now a manager.

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

    docker swarm join --token SWMTKN-1-64b7zlw8w0vas81kyzsw5vzccw220rg5ggv6gc9wlj07jib27z-cwajjmlcadxbrf8e4e4fn8h7u 192.168.1.27:2377

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

Execute this command to see the status of Docker manager and the list of nodes in your cluster.

$ sudo docker node ls
ID                            HOSTNAME        STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
mbiap5ii2psyzozoun3553d8c *   dockermanager   Ready     Active         Leader           20.10.7

Alternatively, use docker info to view the status of docker swarm.

$ sudo docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.7
 Storage Driver: overlay2..........................

Step 4 – Add the Nodes to the Swarm

With the Docker manager configured, we now need to add nodes to our created swarm. We join the cluster using the token issued earlier after running sudo docker swarm init --advertise-addr 192.168.1.27

Run this command on each node.

1. On Node1:

$ sudo docker swarm join --token SWMTKN-1-64b7zlw8w0vas81kyzsw5vzccw220rg5ggv6gc9wlj07jib27z-cwajjmlcadxbrf8e4e4fn8h7u 192.168.1.27:2377
This node joined a swarm as a worker.

2. On Node2:

$ sudo docker swarm join --token SWMTKN-1-64b7zlw8w0vas81kyzsw5vzccw220rg5ggv6gc9wlj07jib27z-cwajjmlcadxbrf8e4e4fn8h7u 192.168.1.27:2377
This node joined a swarm as a worker.

Now with nodes added sucessfully, verify the status by running this command on Docker manger.

$ sudo docker node ls
ID                            HOSTNAME        STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
mbiap5ii2psyzozoun3553d8c *   dockermanager   Ready     Active         Leader           20.10.7
ssg784ymr9kj1l6ibmaj1p2ed     node1           Ready     Active                          20.10.7
gremhhuyltdv7qdcmxuwsxb9n     node2           Ready     Active                          20.10.7

Step 5 – Launch service in Docker Swarm

In Docker swarm, containers are replaced with tasks and launched as a service. For example in this instance, I want to create a service called webserver with 5 containers and state of containers inside the service to be 5. I will achieve this by running this command on the Docker manager.

$ sudo docker service create -p 80:80 --name webserver --replicas 5 httpd
tp0vqj86vdmu3s1cugz9kx8d7
overall progress: 0 out of 5 tasks 
1/5: preparing [=================================>                 ] 
2/5: preparing [=================================>                 ] 
3/5: preparing [=================================>                 ] 
4/5: preparing [=================================>                 ] 
5/5: preparing [=================================>                 ]
overall progress: 5 out of 5 tasks 
1/5: running   
2/5: running   
3/5: running   
4/5: running   
5/5: running   
verify: Service converged

In this case, a service “webserver” has been created with a desired stateof tasks is 5 and will be launched from docker image “httpd”

List the service by running this on Docker manager.

$ sudo docker service ls
ID             NAME        MODE         REPLICAS   IMAGE          PORTS
tp0vqj86vdmu   webserver   replicated   5/5        httpd:latest   *:80->80/tcp

To view the status of the service, issue this:

$ sudo docker service ps webserver
ID             NAME          IMAGE          NODE            DESIRED STATE   CURRENT STATE            ERROR     PORTS
8bxuwkz6szmt   webserver.1   httpd:latest   node2           Running         Running 16 minutes ago             
q4vq8ngc7r31   webserver.2   httpd:latest   node1           Running         Running 16 minutes ago             
d0wteptnluql   webserver.3   httpd:latest   node2           Running         Running 16 minutes ago             
om3ddjgo35ht   webserver.4   httpd:latest   dockermanager   Running         Running 16 minutes ago             
y45isuxb6bgl   webserver.5   httpd:latest   node1           Running         Running 16 minutes ago  

Now we know that the service is running across all nodes including the docker manager. Lets try and access it using a web browser using the addresses: http:// 192.168.1.27 or http:// 192.168.1.26 or http:// 192.168.1.30 these addresses should be replaced with your own. The web page looks like this.

Step 6 – Scale up and scale down Services.

One of the features of Docker swarm is scaling up and down of services. This can be done easily as below.

Scale down a service in Docker swarm

Using the previous created task webserver, lets scale it down to 3 containers.

Run this command on the Docker manager.

sudo docker service scale webserver=3

Sample output.

[[email protected] ~]$ sudo docker service scale webserver=3
webserver scaled to 3
overall progress: 3 out of 3 tasks 
1/3: running   
2/3: running   
3/3: running   
verify: Service converged

Scale up a service in Docker swarm.

Let us scale the service to 6 containers. We run the below command on the Docker manager.

sudo docker service scale webserver=6

Sample output

[[email protected] ~]$ sudo docker service scale webserver=6
webserver scaled to 6
overall progress: 6 out of 6 tasks 
1/6: running   
2/6: running   
3/6: running   
4/6: running   
5/6: running   
6/6: running   
verify: Service converged

Best Docker Learning Courses:

Conclusion

From this guide, we have learned how to install and configure docker swarm. We have also demonstrated the amazing features of docker swarm. I hope this article was of importance.

Find related articles on our website.

Install Docker and Docker Compose on Rocky Linux 8

How To Run Nextcloud 21 on Docker Containers using Podman

Install Docker CE on Rocky Linux 8 / AlmaLinux 8

LEAVE A REPLY

Please enter your comment!
Please enter your name here