Setup Docker Swarm on RHEL 8.x

By | March 14, 2021

Why Docker Swarm?

Docker Swarm is the great way to deploy your application stacks to production, in a distributed cluster – in short it is the container orchestrator or container clustering. With Docker Swarm mode you have:
– Replicability, use the same files as when developing locally.
– Simplicity and speed for development and deployment.
– Robustness and security, with fault-tolerant clusters.

Swarm mode cluster


1. Demonstration LAB info

RoleHostnameIP AddressOSCPURAMDisk
Managermanager192.168.100.30RHEL 8.x2Cores2GB20GB
Workerworker-01192.168.100.31RHEL 8.x2Cores2GB20GB
Workerworker-02192.168.100.32RHEL 8.x2Cores2GB20GB

2. Let iptables see bridged traffic

cat <<EOF | sudo tee /etc/sysctl.d/iptables-bridge.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
modprobe br_netfilter
sysctl --system

3. Local firewall settings requirement

Open protocols and ports between the hosts
– TCP port 2377 for cluster management communications
– TCP and UDP port 7946 for communication among node(s)
– UDP port 4789 for overlay network traffic

# Firewall settings for manager node(s)
firewall-cmd --permanent --add-port=2377/tcp
firewall-cmd --permanent --add-port=7946/tcp
firewall-cmd --permanent --add-port=7946/udp
firewall-cmd --permanent --add-port=4789/udp
firewall-cmd --reload
# Firewall settings for worker node(s)
firewall-cmd --permanent --add-port=7946/tcp
firewall-cmd --permanent --add-port=7946/udp
firewall-cmd --permanent --add-port=4789/udp
firewall-cmd --reload

4. Install Docker Engine for each node(s) (manager & worker(s))

  • Configure local repository to install packages from RHEL disk
  • Create custom repository to install Docker dependency packages on RHEL 8.x. Note: we do not have RHEL subscription repository that is why I use dependency packages from CentOS 8.x instead.
cat <<'EOF' | sudo tee /etc/yum.repos.d/dockerdeps.repo
name=Docker BaseOS Deps on RHEL8.x
includepkgs=policycoreutils-python-utils python3-policycoreutils checkpolicy audit-libs-python3 policycoreutils python3-libsemanage python3-setools python3-audit libsemanage audit-libs audit libcgroup

name=Docker AppStream Deps on RHEL8.x
includepkgs=container-selinux slirp4netns fuse-overlayfs fuse3-libs
  • Add Docker CE official repository
sudo dnf config-manager \
  • Install requirement packages
sudo dnf install \
        device-mapper-persistent-data \
        container-selinux \
  • Install Docker Engine CE (online). Note: we must have internet connection!!!
sudo dnf install \
        docker-ce \
        docker-ce-cli \
  • Enable and start Docker Engine service
systemctl enable docker --now
  • Verify Docker CE version and Info
docker --version
docker info

Initialize Swarm Mode

  • Connect via SSH to manager node
  • Create Swarm Mode Cluster by running command:
docker swarm init --advertise-addr

Join Docker Swarm Cluster

1. To add worker node(s) to this Swarm, run the following command:

token=$(ssh -o StrictHostKeyChecking=no $manager "docker swarm join-token -q worker")
docker swarm join $manager:2377 --token $token

2. To add more manager(s) to this Swarm, run the following command: (optional)

token=$(ssh -o StrictHostKeyChecking=no $manager "docker swarm join-token -q manager")
docker swarm join $manager:2377 --token $token

3. To verify Swarm Cluster node(s) status, on manager node run the following command:

docker node ls

Demo – Deploy service

In this demo, we will deploy an application named “whoami” with below requirement:
– This application will use the overlay networks named “backend-net”
– This application replicate to 2 containers for each worker node(s)

  • Connect via SSH to manager node
  • Create docker overlay network named “backend-net”
docker network create --attachable --driver overlay backend-net
docker network ls
  • Create docker-compose.yml for whoami application
cat <<'EOF' | sudo tee docker-compose-whoami.yml
version: '3.7'

    external: true

    image: containous/whoami:latest
      - backend-net
      - 80:80
      mode: replicated
      replicas: 2
          - node.role == worker
  • Start deploy application with a stack named “whoami” with below command:
docker stack deploy -c docker-compose-whoami.yml whoami
  • To verify the deployment and service, run the following command:
docker stack ls
docker service ls
  • Finally you can access to “whoami” application with “http://<any node IP address>” from any IP address of the node(s) (manager or worker) as the similar result:
Access from worker-01:
Access from worker-02:
Access from manager:

One thought on “Setup Docker Swarm on RHEL 8.x

  1. Pingback: Deploy HAProxy as Load Balancer for Docker Swarm Mode - Systems & Network Administrator

Leave a Reply

Your email address will not be published. Required fields are marked *