You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose
Docker swarm is a tool used to create a docker host cluster. With the help of docker swarm, we can create a high-availability and high-performance cluster in which applications are distributed among hosts. The Docker group is composed of managers and worker nodes, and operations are performed by the managers. In this guide, we are studying how to set up a Docker swarm cluster on Ubuntu 20.04.
Step 1: Prepare the node.
In my setup, I have a manager node and two worker nodes. On each host, configure the hosts file to include all other nodes.
sudo vim /etc/hosts
Add the following to the file
192.168.1.10 manager
192.168.1.11 worker-01
192.168.1.12 worker-02
save document. Ensure that all hosts can be pinged from each host using host name instead of IP address.
Step 2: Install Docker CE on Ubuntu 20.04
We will install Docker CE on the host. To install Docker CE on Ubuntu 20.04, follow these steps:
Use the following command to install dependent packages
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Use the following command to add the docker key and docker repository to your host:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
Update package
sudo apt-get update
Make sure you want to install from the official Docker repository instead of the default Ubuntu repository.
$ apt-cache policy docker-ce
docker-ce:
Installed: (none)
Candidate: 5:19.03.13~3-0~ubuntu-focal
Version table:
5:19.03.13~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
5:19.03.12~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
5:19.03.11~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
5:19.03.10~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
5:19.03.9~3-0~ubuntu-focal 500
500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
Install Docker CE on Ubuntu 20.04
sudo apt install docker-ce
After installation, the Docker daemon should be started. Confirm the status by running the following command:
$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-10-17 16:28:08 EAT; 57s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 2807 (dockerd)
Tasks: 8
Memory: 37.5M
CGroup: /system.slice/docker.service
└─2807 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Add your user to the Docker group to avoid typing sudo every time you run the docker command.
sudo usermod -aG docker ${USER}
Step 3: Create a Docker Swarm cluster
To set up a cluster cluster, we first need to initialize the Docker Swarm mode on the manager node, and then add the worker node to the cluster. Run the following command to initialize the Docker swarm node on the manager.
$ sudo docker swarm init --advertise-addr 192.168.1.10
Swarm initialized: current node (fsuaqqpihi2eabmmq8gldzhpv) is now a manager.
To add a worker to this swarm, run the following command:
sudo docker swarm join --token SWMTKN-1-018kvdektwa74z8fajb5c1u6jyz6qfk4ood8u4qotw7go9jj0p-cfpnh7omy86xcgoh45vau2kaj 192.168.1.10:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
Now, we will use the “join-token” in the cluster “manager” node to add the “worker-01” and worker-02 nodes to the cluster “manager”. Run the command printed in the init command output.
$ sudo docker swarm join --token SWMTKN-1-13xo81gxpb3ttjh5e335pfrmz9fbnliikgfys7u8l4r8k4m575-2gsjwjs3y1i4kgeua2yu840hw 192.168.1.10:2377
This node joined a swarm as a worker.
Check whether the worker node has been successfully added to the cluster by running the following command on the manager:
$ sudo docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
etx5xc5guzftmsqx4naqwvump worker-02 Ready Active 19.03.13
xop4jvrkywz4ldsgwqmfacssc * manager Ready Active Leader 19.03.13
bghrkp7ll1b9lb0ikv8x51gzy worker-01 Ready Active 19.03.13
Step 4: Deploy the application in the cluster
Let’s create a service Nginx web server, make it run on the default http port 80, and then expose it to port 8080 on the host.
$ sudo docker service create --name web-server --publish 8080:80 nginx:1.13-alpine
pq5txw0p9c1qcjrrl2lw3mh5p
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
Confirm the created service by running the following command:
$ sudo docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
pq5txw0p9c1q web-server replicated 1/1 nginx:1.13-alpine *:8080->80/tcp
Step 5: Copy and extend the service.
We will make 3 copies of the web server service so that the service can be accessed on the manager and two worker nodes.
$sudo docker service scale web-server=3
web-server scaled to 3
overall progress: 3 out of 3 tasks
1/3: running [==================================================>]
2/3: running [==================================================>]
3/3: running [==================================================>]
verify: Service converged
Confirm the service copy created
$ sudo docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
pq5txw0p9c1q web-server replicated 3/3 nginx:1.13-alpine *:8080->80/tcp
Use all the following node IPs to access the service from the browser: https://192.168.1.10:8080, https://192.168.1.11:8080 and https://192.168.1.12:8080, you should get a nginx welcome page, As follows: below:
This is a step by step guide on how to set up Docker Swarm on Ubuntu 20.04. I believe this is very helpful. Check out more interesting guides below:
- How to install Kubernetes cluster on Ubuntu using kubeadm
- How to install MicroK8s Kubernetes cluster on CentOS 8
- How to run a local Kubernetes cluster in a Docker container
- Install Docker CE on RHEL 7 Linux
- How to run Docker/Podman containers as Systemd services
You can download this article in PDF format via the link below to support us.Download the guide in PDF formatClose