Welcome to our guide on how to Setup Docker Swarm Cluster on Ubuntu 22.04. Docker is a technology that makes it simple to develop, test, and deploy applications utilizing containers. It offers a simple environment for deploying and running applications. A cluster of physical or virtual machines that are all executing the same program under the name “docker” is known as a “docker swarm.”
Table of Contents
Setup Docker Swarm Cluster on Ubuntu 22.04
The steps below will guide you through the configuration and Setup of Docker Swarm Cluster on Ubuntu 22.04.
Prerequisites
Here are the requirements for us to set up a complete and functional Docker Swarm Cluster on Ubuntu 22.04.
- Server a Manager hostname(dockermanager.dockerswarm.com) IP- 192.168.56.101
- Server b Node 1 hostname(workernode01.dockerswarm.com) IP- 192.168.56.102
- Server c Node 2 hostname(workernode02.dockerswarm.com) IP- 192.168.56.103
Configure /etc/hosts file
Using the editor of your choice, edit /etc/hosts files in all the 3 servers;
sudo vim /etc/hosts
Add the following content to the file, replacing the IP addresses with your own;
192.168.56.101 dockermanager.dockerswarm.com dockermanager
192.168.56.102 workernode01.dockerswarm.com workernode01
192.168.56.103 workernode02.dockerswarm.com workernode02
Install Docker CE on Ubuntu 22.04
We are going to install Docker CE in all three hosts as follows:
Update the hosts;
sudo apt update
Run the command below to install dependent packages;
sudo apt install apt-transport-https ca-certificates curl software-properties-common lsb-release
Run the following command to add a docker key to the hosts;
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker-archive-keyring.gpg
Run the following command to add a docker repository to the hosts;
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Run system updates in all the hosts;
sudo apt update
Now, install Docker CE on the three hosts;
sudo apt install docker-ce
Check the status of the docker in each host;
systemctl status docker
Output;
● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2023-09-19 18:50:29 UTC; 24s ago TriggeredBy: ● docker.socket Docs: https://docs.docker.com Main PID: 54745 (dockerd) Tasks: 9 Memory: 26.5M CGroup: /system.slice/docker.service └─54745 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Configure Docker Swarm Cluster on Ubuntu 22.04
To setup Docker Swarm, we need to initialize Docker Swarm mode on the DockerManager host.
Run the following command to initialize the Docker Swarm. Replace the IP address with the DockerManager host IP;
sudo docker swarm init --advertise-addr 192.168.56.101
Here is the output of the above command;
Swarm initialized: current node (f2rkkojrrk332jyh3fmvttpmq) is now a manager. To add a worker to this swarm, run the following command: sudo docker swarm join --token SWMTKN-1-5ct25i0lj0zoocnefpj8sxjoarr4tto6krn5d6u0amx2ae51wj-0qfth1kbbc88bh8jpcwhjini0 192.168.56.101:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
From the above output, we got the token for joining the worker nodes to the cluster;
sudo docker swarm join --token SWMTKN-1-5ct25i0lj0zoocnefpj8sxjoarr4tto6krn5d6u0amx2ae51wj-0qfth1kbbc88bh8jpcwhjini0 192.168.56.101:2377
Join WorkerNodes To the Docker Swarm Cluster
To join the two worker nodes(WorkerNode01 and WorkerNode02), we will use the token generated from the master node(Dockermanger).
On WorkerNode01, run the following command to join it to the cluster;
sudo docker swarm join --token SWMTKN-1-5ct25i0lj0zoocnefpj8sxjoarr4tto6krn5d6u0amx2ae51wj-0qfth1kbbc88bh8jpcwhjini0 192.168.56.101:2377
This node joined a swarm as a worker.
On WorkerNode02, run the following command to join it to the cluster;
sudo docker swarm join --token SWMTKN-1-5ct25i0lj0zoocnefpj8sxjoarr4tto6krn5d6u0amx2ae51wj-0qfth1kbbc88bh8jpcwhjini0 192.168.56.101:2377
This node joined a swarm as a worker.
Now, verify if the nodes have been added successfully to the Docker Swarm cluster.
Run the following command on DockerManager;
sudo docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
f2rkkojrrk332jyh3fmvttpmq * dockermanager Ready Active Leader 24.0.2
tgthmz9ugmx8obplm6gq4q1pj workernode01 Ready Active 24.0.2
xm0dueep1q5f55z7i46jbxebp workernode02 Ready Active 24.0.2
Deploying an Application on the Docker Swarm Cluster
Tasks are launched as services in place of containers in Docker Swarm. We will run the following command on DockerManager(master node) in order to deploy our first application on the Docker Swarm cluster. In this example, we will deploy the Nginx application on its default HTTP port 80;
sudo docker service create --name web-server --publish 80:80 nginx:latest
ldkp699sulrnj0t37cuursbk0
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
Check the service created;
sudo docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
ldkp699sulrn web-server replicated 1/1 nginx:latest *:80->80/tcp
Run the following command to check the status of the service;
sudo docker service ps web-server
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
4avf5yvbnzil web-server.1 nginx:latest dockermanager Running Running 12 minutes ago
Accessing the Nginx Web Service
As of right now, we are aware that the service is active on every node, including the Docker Manager. Let’s try accessing the addresses below on the web browser;
- http://192.168.56.101
- http://192.168.56.102
- http://192.168.56.103
You will be able to get a page similar to the one below:
This shows that our Nginx application is running on all the nodes successfully.
Scaling Up the Nginx service on Docker Swarm Cluster
We will replicate the web-server service six times and make it available on the docker manager and two worker nodes.
Run the following command on DockerManager to scale up or replicate the web-server service on Docker Swarm;
sudo docker service scale web-server=6
Output;
web-server 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
We can check the created replicas as follows;
sudo docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
ldkp699sulrn web-server replicated 6/6 nginx:latest *:80->80/tcp
We can also check the running services after scaling up;
sudo docker service ps web-server
Output;
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS 4avf5yvbnzil web-server.1 nginx:latest dockermanager Running Running about an hour ago se6bn1zth5i9 web-server.2 nginx:latest dockermanager Running Running 4 minutes ago padgpve4uhot web-server.3 nginx:latest workernode01 Running Running 4 minutes ago 5gmc6l5j94o9 web-server.4 nginx:latest workernode01 Running Running 4 minutes ago fn6lk85v40ty web-server.5 nginx:latest workernode02 Running Running 4 minutes ago dzkmy4y3ukru web-server.6 nginx:latest workernode02 Running Running 4 minutes ago
Scaling Down the Nginx service on Docker Swarm Cluster
On DockerManager we can also scale down web-server service to any number we require. In this example, we will scale down the web-server service to four.
Run the following command to scale down the web-server service on Docker Swarm;
sudo docker service scale web-server=4
Output;
web-server scaled to 4 overall progress: 4 out of 4 tasks 1/4: running [==================================================>] 2/4: running [==================================================>] 3/4: running [==================================================>] 4/4: running [==================================================>] verify: Service converged
We can check the created replicas as follows;
sudo docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
ldkp699sulrn web-server replicated 4/4 nginx:latest *:80->80/tcp
Now, check the running services after scaling down;
sudo docker service ps web-server
Output;
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS 4avf5yvbnzil web-server.1 nginx:latest dockermanager Running Running about an hour ago se6bn1zth5i9 web-server.2 nginx:latest dockermanager Running Running 13 minutes ago padgpve4uhot web-server.3 nginx:latest workernode01 Running Running 12 minutes ago fn6lk85v40ty web-server.5 nginx:latest workernode02 Running Running 13 minutes ago
Conclusion
This marks the end of our guide on how to Setup Docker Swarm Cluster on Ubuntu 22.04. In this guide, you learn how docker swarm works and get to know how to deploy an application. Stay tuned to get amazing guides from our site.
Other Guides
Install Docker CE on Ubuntu 22.04
Install Cockpit on Ubuntu 22.04/20.04