23.8 C
Texas

How To Install Docker Swarm On Ubuntu 20.04

In this tutorial, you’ll learn that how to Install Docker Swarm on Ubuntu 20.04. Docker Swarm is most popular tool that can be used to create a cluster of docker hosts. You can distribute applications among the hosts with the help of docker cluster created by swarm. Docker Swarm provides high end performance and availablity containing a manager & worker nodes.Actions are performed by the manager nodes. Let’s see how to install it on Ubuntu 20.04.

Update Your System

Hit the given command in your terminal to update your system.

sudo apt -y update

Step 1: Prepare Your Nodes

To prepare nodes, you’ve to configure hosts files on each host. In this guide I’m going to use one Manager node and 2 worker nodes.

- Advertisement -
sudo nano /etc/hosts

And add the Ip’s of your hosts.

192.168.1.10 manager
192.168.1.11 worker-1
192.168.1.12 worker-2

Save & close the file.

Verify that you can ping all the hosts using hostname instead of IP’s.

Step 2: Install Docker CE on Ubuntu 20.04

Install the Docker CE on the hosts. Hit the following command to install the required dependencies for Docker CE on Ubuntu 20.04.

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

Then add the Docker CE repository and GPG key on your hosts.

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"

Once done, update the packages.

sudo apt update

Double check that you’re going to install Docker CE from the official repository of Docker.

root@ubuntu20:~$ sudo 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

Verify the status of Docker CE.

root@ubuntu20:~$ 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

And add your user to docker group.

sudo usermod -aG docker ${USER}

Step 3: Create Docker Swarm Cluster on Ubuntu 20.04

First start the Docker Swarm on the manager node and then start add the worker nodes to the cluster. To start docker swarm on manager node, type the given command in your terminal.

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.

From the above result, copy the token “Join –token” command to add the worker nodes to the manager node. Here’s how you can add.

sudo docker swarm join --token SWMTKN-1-13xo81gxpb3ttjh5e335pfrmz9fbnliikgfys7u8l4r8k4m575-2gsjwjs3y1i4kgeua2yu840hw 192.168.1.10:2377
This node joined a swarm as a worker.

Verify that worker nodes are added to the manager node.

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

Now, create the service Nginx web server to run on the port 80 & expose it to the port 8080 on the host. To do this type

root@ubuntu20:~$ 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

Verify the status of created service using.

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 & Scale the Service

You’ve to make replicas of the web-server service in order to access it on the manager and the 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
Verify the 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

You can access the service from the web-browser of any host added with the help of IP address and port number like below:
192.168.1.10:8080

So, this is how you can install Docker Swarm on Ubuntu 20.04.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article