You can support us by downloading this article as PDF from the Link below. Download the guide as PDF

Docker swarm is a tool used to create a cluster of docker hosts. With docker swarm, we can create a high availability and high performance cluster where applications are distributed among the hosts. Docker swarm consists of a manager and worker nodes and operations are performed from the manager. In this guide, we are looking at how to set up Docker swarm cluster on Ubuntu 20.04.

Step 1: Prepare your nodes.

In my set up, I have one manager node and two worker nodes. On each host, configure hosts file to include all the other nodes.

sudo vim /etc/hosts

Add the following content to the file

192.168.1.10	manager
192.168.1.11	worker-01
192.168.1.12	worker-02

Save the file. Ensure that you can ping all the hosts from each host using hostname and not IP address.

Step 2: Install Docker CE on Ubuntu 20.04

We are going to install Docker CE on the hosts. To install Docker CE on Ubuntu 20.04, following the following steps:

Install dependency packages with the below command

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common

Add docker key and docker repository to your hosts with the following commands:

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 packages

sudo apt-get update

Ensure that you are going to install from the official Docker repo instead of default Ubuntu repo.

$ 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 installing, Docker daemon should be started. Confirm the status by running the below 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 Docker group to avoid typing sudo everytime you run docker commands.

sudo usermod -aG docker ${USER}

Step 3: Create Docker Swarm Cluster

To set up swarm cluster, we first need to initialize Docker Swarm mode on the manager node then add the worker nodes to the cluster. Run the below command to initialize 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.

We are now going to add the ‘worker-01‘ and worker-02 nodes to the cluster ‘manager’ using the ‘join-token’ from the cluster ‘manager’ node. 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 if the worker nodes are successfully added to the cluster by running the below 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 application in the Cluster

Let’s create a service Nginx web server to run on default http port 80, and then expose it to the 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 below 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: Replicas and Scaling the service.

We are going to make 3 replicas of the web-server service so that it is accessible on the manager and the 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 created service replicas

$ sudo docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS 
pq5txw0p9c1q        web-server          replicated          3/3                 nginx:1.13-alpine   *:8080->80/tcp

Access the service from your browser using all the nodes IPs: http://192.168.1.10:8080, http://192.168.1.11:8080 and http://192.168.1.12:8080 and you should get a nginx welcome page as below:

This has been a step-by-step guide on how to set up Docker Swarm on Ubuntu 20.04. I believe it has been helpful. Check more interesting guides below:

You can support us by downloading this article as PDF from the Link below. Download the guide as PDF