Step By Step Guide to Create Docker Swarm Cluster on Ubuntu 20.04 LTS

Hello, In this blog we are discussing about Docker Swarm. It is a tool used to create a docker hosts cluster. It provides a high availability and high performance cluster where applications are distributed among the hosts. Using docker swarm, we can easily increasing the number of container instance for the same application.

There are some points to create docker swam cluster & manage manager-node & worker-node on Ubuntu:

Prerequisites:

  • 2 virtual machine or AWS EC2.[Manager-Node & Worker-Node]
  • Ubuntu Host machine with Sudo privileges.

Install Docker on Both machine/Server.

  • Update the System.

apt-get update

  • Install dependency packages.

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

  • Add docker key and docker repository to your both servers.

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
&&
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

  • Update the packages.

apt-get update

  • Install docker-ce.

apt install docker-ce

  • Start & Enable the Docker.

systemctl start docker
systemctl enable docker

Create Docker Swarm Cluster on Manager Node

  • Run the following command on manager node.

Syntax:  

docker swarm init --advertise-addr ip-address (ip-addr of manager-node)

For example:

docker swarm init --advertise-addr 15.228.222.25

  • Here is the command output.

[email protected]:/home/ubuntu# docker swarm init --advertise-addr 15.228.222.25
Swarm initialized: current node (ws7wf67jnaquz8rgpwcr96a05) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-5p9pl4jvt2qi2t0h8q48ialtukji5u1uelctzetimqjff0gx76-3ua8kuzk5o9dmtwu815u3cco6 15.228.222.25:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

  • Check the status of the Manager Node.

docker node ls

  • Here is the command output.

[email protected]:/home/ubuntu# docker node ls
ID          HOSTNAME       STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
ws7cr96a05* ip-172-31-16-93 Ready Active       Leader          20.10.12

  • Check the status of the Docker Swarm Cluster.

docker info

  • Here is the command output.

Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Docker Buildx (Docker Inc., v0.7.1-docker)
scan: Docker Scan (Docker Inc., v0.12.0)
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.12
Storage Driver: overlay2
Backing Filesystem: extfs
...
Cgroup Version: 1
Plugins:
Volume: local
...
Swarm: active
NodeID: ws7cr96a05
Is Manager: true
ClusterID: kod4rin3n750zyrm5wvw6zk3d
Managers: 1
Nodes: 1
Default Address Pool: 10.0.0.0/8
SubnetSize: 24
Data Path Port: 4789
Orchestration:
Task History Retention Limit: 5
....
Autolock Managers: false
Root Rotation In Progress: false
Node Address: 15.228.222.25
Manager Addresses:
15.228.222.25:2377
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc version: v1.0.2-0-g52b36a2
....

Add Worker Node to swarm cluster

  • Copy the output of the “swarm init” command from manager node, then paste that output on the Worker Node to join the Swarm Cluster.

Syntax:

docker swarm join --token SWMTKN-1-token-number  manager-ip-address:2377

For example:

docker swarm join --token SWMTKN-1-5p9pl4jvt2qi2t0h8q48ialtukji5u1uelctzetimqjff0gx76-3ua8kuzk5o9dmtwu815u3cco6 15.228.222.25:2377

  • Here is the command output.

[email protected]:/home/ubuntu# docker swarm join --token SWMTKN-1-5p9pl4jvt2qi2t0h8q48ialtukji5u1uelctzetimqjff0gx76-3ua8kuzk5o9dmtwu815u3cco6 15.228.222.25:2377
This node joined a swarm as a worker.

Go to Manager Node

  • Check the Status of Manager node.

docker node ls

  • Here is the command output.

[email protected]:/home/ubuntu# docker node ls
ID          HOSTNAME       STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
ws7cr96a05* ip-172-31-16-93  Ready  Active     Leader          20.10.12
fvwn6upa    ip-172-31-42-187 Ready  Active                     20.10.12

  • Now Docker Swarm Cluster is up and running mode.

To Launch web service in Docker Swarm

  • On Manager Node,run the following command to deploy a web server (nginx) service.

Syntax:

docker service create --name service-name -p 80:80 image-name

For example:

docker service create --name web-server -p 80:80 nginx:1.13-alpine

  • Here is the command output.

[email protected]:/home/ubuntu# docker service create --name web-server -p 80:80 nginx:1.13-alpine
sgpi9i85s3zyfen2wv8g3h2mu
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged

  • Check the docker service.

docker service ls

  • Here is the command output.

[email protected]:/home/ubuntu# docker service ls
ID           NAME       MODE      REPLICAS IMAGE              PORTS
sgpi9i85s3zy web-server replicated    1/1  nginx:1.13-alpine *:80->80/tcp

Replicas and Scaling the service.

  • Run the following command to make 2 replicas of the web-server service so that it is accessible on the manager and worker node.

docker service scale web-server=2

  • Here is the command output.

[email protected]:/home/ubuntu# docker service scale web-server=2
web-server scaled to 2
overall progress: 2 out of 2 tasks
1/2: running [==================================================>]
2/2: running [==================================================>]
verify: Service converged

  • Check the status of web server service.

docker service ps web-server

  • Here is the command output.

[email protected]:/home/ubuntu# docker service ps web-server
ID      NAME        IMAGE               NODE         DESIRED STATE CURRENT STATE   ERROR PORTS
gwy9o9a web-server.1 nginx:1.13-alpine ip-172-31-16-93  Running    Running 5 minutes ago
o884w7m web-server.2 nginx:1.13-alpine ip-172-31-42-187 Running    Running 59 seconds ago

  • Check the status of running docker services.

docker service ls

  • Here is the command output.

[email protected]:/home/ubuntu# docker service ls
ID             NAME         MODE       REPLICAS   IMAGE                 PORTS
sgpi9i85s3zy   web-server  replicated   2/2     nginx:1.13-alpine   *:80->80/tcp

Test the Docker Swarm

  • Open Nginx web page using manager & Worker Node Ip-address on Browser.

In Manager-Node.

In Worker-Node.

  • Nginx web server service is now distributed across both nodes (Manager & Worker). Docker Swarm also provides high availability for service.
  • If the web server goes down on the Worker Node, then the new container will be launched on the Manager Node.

Test High Availability

  • To Stop docker on Worker-node.

systemctl stop docker

  • Check the status of web server services.

docker service ps web-server

  • Here is the command output.

[email protected]:/home/ubuntu# docker service ps web-server
ID       NAME       IMAGE              NODE           DESIRED STATE CURRENT  STATE    ERROR PORTS
gwy9o9a web-server.1 nginx:1.13-alpine ip-172-31-16-93 Running       Running  17 minutes ago
paegey  web-server.2 nginx:1.13-alpine  ip-172-31-16-93 Running      Running  15 seconds ago
o884w7m \_ web-server.2 nginx:1.13-alpine ip-172-31-42-187 Shutdown  Running  13 minutes ago

Leave a Reply

Your email address will not be published.