Tutorial-9122023

How Install and Configure a Docker Swarm Cluster on CentOS 8

How Install and Configure a Docker Swarm Cluster on CentOS 8

Docker is an open-source tool that can be used to create, deploy and run applications using a container. The container allows you to package up an application with all required dependencies and ship it out as a single package.

Docker Swarm is a clustering tool used for managing the Docker host. It allows you to make a group of Docker hosts into a single logical virtual server. This way you can scale your application horizontally and increase the number of the container instance. Docker swarm offers very useful features including, container self-healing, load balancing, container scale up and scale down, service discovery, and rolling updates.

In this post, we will show you how to set up a Docker Swarm cluster on CentOS 8.

Prerequisites

  • Two servers running CentOS 8.
  • A root password is configured on each server.

Install Docker on Both Node

First, you will need to install Docker on both nodes. By default, the latest version of Docker is not included in the CentOS 8 default repository. So you will need to add a Docker repo in your system.

You can add it with the following command:
dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

Once the Docker repo is created, install the Docker with the following command:
dnf install docker-ce --nobest

After installing Docker, start the Docker service and enable it to start at system reboot with the following command:
systemctl start docker
systemctl enable docker

You can also verify the status of Docker with the following command:
systemctl status docker

You should get the following output:
? docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2021-03-12 03:55:24 EST; 6s ago
Docs: https://docs.docker.com
Main PID: 2173 (dockerd)
Tasks: 8
Memory: 44.7M
CGroup: /system.slice/docker.service
??2173 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Mar 12 03:55:23 workernode dockerd[2173]: time="2021-03-12T03:55:23.570387991-05:00" level=error msg="Failed to built-in GetDriver graph btrfs>
Mar 12 03:55:23 workernode dockerd[2173]: time="2021-03-12T03:55:23.617049696-05:00" level=warning msg="Your kernel does not support cgroup bl>
Mar 12 03:55:23 workernode dockerd[2173]: time="2021-03-12T03:55:23.617096273-05:00" level=warning msg="Your kernel does not support cgroup bl>
Mar 12 03:55:23 workernode dockerd[2173]: time="2021-03-12T03:55:23.617278059-05:00" level=info msg="Loading containers: start."
Mar 12 03:55:23 workernode dockerd[2173]: time="2021-03-12T03:55:23.884953789-05:00" level=info msg="Default bridge (docker0) is assigned with>
Mar 12 03:55:24 workernode dockerd[2173]: time="2021-03-12T03:55:24.039811428-05:00" level=info msg="Loading containers: done."
Mar 12 03:55:24 workernode dockerd[2173]: time="2021-03-12T03:55:24.066358016-05:00" level=info msg="Docker daemon" commit=363e9a8 graphdriver>
Mar 12 03:55:24 workernode dockerd[2173]: time="2021-03-12T03:55:24.066498611-05:00" level=info msg="Daemon has completed initialization"
Mar 12 03:55:24 workernode systemd[1]: Started Docker Application Container Engine.
Mar 12 03:55:24 workernode dockerd[2173]: time="2021-03-12T03:55:24.119523516-05:00" level=info msg="API listen on /var/run/docker.sock"

Now, verify the installed version of Docker using the following command:
docker --version

You should see the following output:
Docker version 20.10.5, build 55c4c88

Configure Firewall

Next, you will need to allow ports 2376, 2377, 7946 and 80 through the firewall on both nodes. You can allow them with the following command:
firewall-cmd --permanent --add-port=2376/tcp
firewall-cmd --permanent --add-port=2377/tcp
firewall-cmd --permanent --add-port=7946/tcp
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=7946/udp
firewall-cmd --permanent --add-port=4789/udp

Next, reload the firewalld to apply the changes:
firewall-cmd --reload

Initialize the Docker Swarm Cluster

Next, you will need to initialize the Docker Swarm cluster on the manager node. You can do it with the following command:
docker swarm init --advertise-addr 45.58.32.185

You should get the following output:
Swarm initialized: current node (cq8xpscsls2ctqhdha8lhdrph) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-24ciicg1knfh8htmvymnfw1igx64tcq6ah91n6amk18m2ek9qo-8sf9oysu08t5mf4ggd4ut7o3e 45.58.32.185:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

You can use the command shown in the above output in the Docker worker node to join the node to the cluster.

You can check the information of the Docker Swarm cluster with the following command:
docker info

You should get the following output:
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)

Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 20.10.5
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: active
NodeID: cq8xpscsls2ctqhdha8lhdrph
Is Manager: true
ClusterID: m7jrgvuw1k7pvfd1qyc3mffpl
Managers: 1
Nodes: 1
Default Address Pool: 10.0.0.0/8
SubnetSize: 24
Data Path Port: 4789
Orchestration:
Task History Retention Limit: 5
Raft:
Snapshot Interval: 10000
Number of Old Snapshots to Retain: 0
Heartbeat Tick: 1
Election Tick: 10
Dispatcher:
Heartbeat Period: 5 seconds
CA Configuration:
Expiry Duration: 3 months
Force Rotate: 0
Autolock Managers: false
Root Rotation In Progress: false
Node Address: 45.58.32.185
Manager Addresses:
45.58.32.185:2377

You can now verify the Docker Swarm node with the following command:
docker node ls

You should get the following output:
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
cq8xpscsls2ctqhdha8lhdrph * masternode Ready Active Leader 20.10.5

Add Worker Node to Docker Swarm Cluster

Next, you will need to add the worker node to the Docker Swarm manager node. You can do it with the following command on the worker node:
docker swarm join --token SWMTKN-1-24ciicg1knfh8htmvymnfw1igx64tcq6ah91n6amk18m2ek9qo-8sf9oysu08t5mf4ggd4ut7o3e 45.58.32.185:2377

You should get the following output:
This node joined a swarm as a worker.

On the Docker Manager node, verify the worker node with the following command:
docker node ls

You should see that the worker node is added to the Docker Swarm:
cq8xpscsls2ctqhdha8lhdrph * masternode Ready Active Leader 20.10.5
bipfv8sfm94a9po0uame5rd1n workernode Ready Active 20.10.5

Launch a service in Docker Swarm

Here, we will create a new Nginx webservice and scale it with two containers. You can create it by running the following command on Manager node:
docker service create -p 80:80 --name webservice --replicas 2 nginx

You should get the following output:
agyxlaswxakrbboakkyydsh0k
overall progress: 2 out of 2 tasks
1/2: running [==================================================>] 2/2: running [==================================================>] verify: Service converged

You can now check the webservice with the following command:
docker service ls

You should see the following output:
ID NAME MODE REPLICAS IMAGE PORTS
agyxlaswxakr webservice replicated 2/2 nginx:latest *:80->80/tcp

You can also check the running container with the following command:
docker ps

You should see the following output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c887cad1df2e nginx:latest "/docker-entrypoint.…" 32 seconds ago Up 30 seconds 80/tcp webservice.2.jelyj9gmeb7ikl2scg7mz8yg8

To get the detail information of webservice, run the following command:
docker service ps webservice

You should see the following output:
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
eye9zukwwrkq webservice.1 nginx:latest workernode Running Running 3 minutes ago
jelyj9gmeb7i webservice.2 nginx:latest masternode Running Running 3 minutes ago

Verify Docker Swarm

At this point, we have deployed an Nginx container across the cluster nodes including the management node. You can now access your Nginx webserver using any of the Worker node or Manager node IP address:

Docker container deployed successfully

Conclusion

In the above guide, you learned how to set up the Docker Swarm cluster on CentOS 8. You can now add any number of worker nodes to the Docker Swarm cluster and scale your application.

Add Comment

Click here to post a comment