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

Prerequisites

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
EOF
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
[dockerdeps-baseos]
name=Docker BaseOS Deps on RHEL8.x
baseurl=http://mirror.centos.org/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=0
enabled=1
includepkgs=policycoreutils-python-utils python3-policycoreutils checkpolicy audit-libs-python3 policycoreutils python3-libsemanage python3-setools python3-audit libsemanage audit-libs audit libcgroup

[dockerdeps-appstream]
name=Docker AppStream Deps on RHEL8.x
baseurl=http://mirror.centos.org/centos/$releasever/AppStream/$basearch/os/
gpgcheck=0
enabled=1
includepkgs=container-selinux slirp4netns fuse-overlayfs fuse3-libs
EOF
  • Add Docker CE official repository
sudo dnf config-manager \
        --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  • Install requirement packages
sudo dnf install \
        device-mapper-persistent-data \
        container-selinux \
        lvm2
  • Install Docker Engine CE (online). Note: we must have internet connection!!!
sudo dnf install \
        docker-ce \
        docker-ce-cli \
        containerd.io
  • 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 192.168.100.30

Join Docker Swarm Cluster

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

manager=192.168.100.30
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)

manager=192.168.100.30
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'

networks:
  backend-net:
    external: true

services:
  app:
    image: containous/whoami:latest
    networks:
      - backend-net
    ports:
      - 80:80
    deploy:
      mode: replicated
      replicas: 2
      placement:
        constraints:
          - node.role == worker
EOF
  • 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: http://192.168.100.31
Access from worker-02: http://192.168.100.32
Access from manager: http://192.168.100.30

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.