How to install and configure Docker Swarm cluster on CentOS 8

How to install and configure Docker Swarm cluster on CentOS 8

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

Docker Swarm is a cluster tool for managing Docker hosts. It allows you to set up a group of Docker hosts as a logical virtual server. In this way, you can scale the application horizontally and increase the number of container instances. Docker swarm provides very useful features, including container self-healing, load balancing, container zooming in and out, service discovery, and rolling updates.

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

prerequisites

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

Install Docker on both nodes

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. Therefore, you will need to add a Docker repository to the system.

You can add it with the following command:

dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

After creating the Docker repository, install Docker using the following command:

dnf install docker-ce --nobest

After installing Docker, start the Docker service and use the following command to start it when the system restarts:

systemctl start dockersystemctl 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, use the following command to verify the installed Docker version:

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 to pass through the firewalls on both nodes. You can allow them with the following command:

firewall-cmd --permanent --add-port=2376/tcpfirewall-cmd --permanent --add-port=2377/tcpfirewall-cmd --permanent --add-port=7946/tcpfirewall-cmd --permanent --add-port=80/tcpfirewall-cmd --permanent --add-port=7946/udpfirewall-cmd --permanent --add-port=4789/udp

Next, reload 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 use the following commands to do so:

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

Now you can 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 the secondary node to the Docker Swarm cluster

Next, you need to add the worker node to the Docker Swarm manager node. You can use the following command on the worker node to do this:

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, use the following command to verify the worker node:

docker node ls

You should see that the worker node has been added to Docker Swarm:

cq8xpscsls2ctqhdha8lhdrph *   masternode   Ready     Active         Leader           20.10.5
bipfv8sfm94a9po0uame5rd1n     workernode   Ready     Active                          20.10.5

Start the service in Docker Swarm

Here, we will create a new Nginx web service and extend it with two containers. You can create it by running the following command on the 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 

Now, you can check the web service 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 detailed information about the web service, 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

So far, we have deployed Nginx containers on all cluster nodes including the management node. Now, you can use any Worker node or Manager node IP address to access the Nginx web server:

in conclusion

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

Related Posts