Saturday, December 28, 2024
Google search engine
HomeUncategorisedHow To Install Docker Swarm On Ubuntu 22.04|20.04

How To Install Docker Swarm On Ubuntu 22.04|20.04

.tdi_3.td-a-rec{text-align:center}.tdi_3 .td-element-style{z-index:-1}.tdi_3.td-a-rec-img{text-align:left}.tdi_3.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_3.td-a-rec-img{text-align:center}}

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 22.04|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

.tdi_2.td-a-rec{text-align:center}.tdi_2 .td-element-style{z-index:-1}.tdi_2.td-a-rec-img{text-align:left}.tdi_2.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_2.td-a-rec-img{text-align:center}}

192.168.1.10	manager.example.com manager
192.168.1.11	worker-01.example.com worker-01
192.168.1.12	worker-02.example.com 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 22.04|20.04

We are going to install Docker CE on the hosts. To install Docker CE on Ubuntu22.04|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 lsb-release

Add docker key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker-archive-keyring.gpg

Add docker repository to your hosts with the following commands:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Update packages

sudo apt 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:20.10.17~3-0~ubuntu-jammy
  Version table:
     5:20.10.17~3-0~ubuntu-jammy 500
        500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
     5:20.10.16~3-0~ubuntu-jammy 500
        500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
     5:20.10.15~3-0~ubuntu-jammy 500
        500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
     5:20.10.14~3-0~ubuntu-jammy 500
        500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages
     5:20.10.13~3-0~ubuntu-jammy 500
        500 https://download.docker.com/linux/ubuntu jammy/stable amd64 Packages

Install Docker CE on Ubuntu22.04|20.04:

sudo apt install docker-ce

After installing, Docker daemon should be started. Confirm the status by running the below command:

$ 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}
newgrp docker

Step 3: Create Docker Swarm Cluster onUbuntu 22.04|20.04

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.

Get interface IP address

# Get active primary interface name
$ ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 52:54:00:13:e7:d6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 192.168.200.255 scope global dynamic noprefixroute enp1s0
       valid_lft 3578sec preferred_lft 3578sec
    inet6 fe80::bfeb:53e3:8760:78ee/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
    link/ether 02:42:e8:4e:10:c8 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever

For me the interface for which Docker manager will advertise on is enp1s0

INT_NAME="enp1s0" #replace accordingly
HOST_IP=$(ip addr show $INT_NAME | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)

Confirm IP address is saved correctly:

$ echo $HOST_IP
192.168.1.10

Run the below command to initialize Docker swarm node on the manager.

$ sudo docker swarm init --advertise-addr $HOST_IP
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.

Step 4: Join Worker Nodes to the Cluster

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 5: 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 6: 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:

Set up docker swarm on ubuntu 20.04

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

.tdi_4.td-a-rec{text-align:center}.tdi_4 .td-element-style{z-index:-1}.tdi_4.td-a-rec-img{text-align:left}.tdi_4.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_4.td-a-rec-img{text-align:center}}

RELATED ARTICLES

Most Popular

Recent Comments