Ubuntu 26.04

Docker : Swarm Cluster2026/05/08

 

Configure Docker Swarm to create Docker Cluster with multiple Docker nodes.

On this example, Configure Swarm Cluster with 3 Docker nodes like follows.
There are 2 roles on Swarm Cluster, those are [Manager nodes] and [Worker nodes].
This example shows to set those roles like follows.

 -----------+---------------------------+--------------------------+------------
            |                           |                          |
        eth0|10.0.0.51              eth0|10.0.0.52             eth0|10.0.0.53
 +----------+-----------+   +-----------+----------+   +-----------+----------+
 | [ node01.srv.world ] |   | [ node02.srv.world ] |   | [ node03.srv.world ] |
 |       Manager        |   |        Worker        |   |        Worker        |
 +----------------------+   +----------------------+   +----------------------+

[1]
[2] Disable live-restore feature on all Nodes.
(because it can not use live-restore feature on Swarm mode)
root@node01:~#
vi /etc/docker/daemon.json
# create new

{
    "live-restore": false
}

root@node01:~#
systemctl restart docker

[3] If UFW is enabled, allow service port on all Nodes.
root@node01:~#
ufw allow 2377/tcp

root@node01:~#
ufw allow 4789/udp

root@node01:~#
ufw allow 7946/tcp

root@node01:~#
ufw allow 7946/udp

Rule added
Rule added (v6)
[4] Configure Swarm Cluster on Manager Node.
root@node01:~#
docker swarm init

Swarm initialized: current node (b6ql42pe7czhs7jdnq3ui30oy) is now a manager.

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

    docker swarm join --token SWMTKN-1-0yqj4ubklc3g2ycfwpgkm30a87tfug6frs3gpioj4qla5o8vrg-87tzw1nz4j5jngjo12vvq9g29 10.0.0.51:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[5] Join in Swarm Cluster on all Worker Nodes.
It's OK to run the command which was shown when running swarm init on Manager Node.
root@node02:~#
docker swarm join \
--token SWMTKN-1-0yqj4ubklc3g2ycfwpgkm30a87tfug6frs3gpioj4qla5o8vrg-87tzw1nz4j5jngjo12vvq9g29 10.0.0.51:2377

This node joined a swarm as a worker.
[6] Verify with a command [node ls] that worker nodes could join in Cluster normally.
root@node01:~#
docker node ls

ID                            HOSTNAME           STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
b6ql42pe7czhs7jdnq3ui30oy *   node01.srv.world   Ready     Active         Leader           29.1.3
sz00419xo5x57fia9dxluwu4n     node02.srv.world   Ready     Active                          29.1.3
ovoae18ewenygyq9dp339d8nf     node03.srv.world   Ready     Active                          29.1.3
[7] After creating Swarm Cluster, configure services the Swarm Cluster provides.
For example, create a Nginx container to configure Swarm service.
Generally, it is used a container image on a rgistry on all Nodes, but on this example, create container images on each Node to verify settings and accesses for Swarm Cluster.
root@node01:~#
vi Dockerfile
FROM ubuntu
LABEL Maintainer="ServerWorld <admin@srv.world>"

RUN apt-get update
RUN apt-get -y install nginx
RUN echo "Nginx on node01" > /var/www/html/index.html

EXPOSE 80
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]

root@node01:~#
docker build -t nginx-server:latest .
# if UFW is enabled, allow http

root@node01:~#
ufw allow http
[8] Configure service on Manager Node.
After succeeding to configure service, access to the Manager node's Hostname or IP address to verify it works normally. Access requests to worker nodes are load-balanced with round-robin like follows.
root@node01:~#
docker images

IMAGE                 ID             DISK USAGE   CONTENT SIZE   EXTRA
nginx-server:latest   b4ed29536585        227MB         69.9MB
ubuntu:latest         f3d28607ddd7        160MB         45.3MB

# create a service with 2 replicas

root@node01:~#
docker service create --name swarm_cluster --replicas=2 -p 80:80 nginx-server:latest

image nginx-server:latest could not be accessed on a registry to record
its digest. Each node will access nginx-server:latest independently,
possibly leading to different nodes running different
versions of the image.

ptt338d9qzbpmbrq6q411ozkh
overall progress: 2 out of 2 tasks
1/2: running   [==================================================>]
2/2: running   [==================================================>]
verify: Service ptt338d9qzbpmbrq6q411ozkh converged

# show service list

root@node01:~#
docker service ls

ID             NAME            MODE         REPLICAS   IMAGE                 PORTS
ptt338d9qzbp   swarm_cluster   replicated   2/2        nginx-server:latest   *:80->80/tcp

# inspect the service

root@node01:~#
docker service inspect swarm_cluster --pretty


ID:             ptt338d9qzbpmbrq6q411ozkh
Name:           swarm_cluster
Service Mode:   Replicated
 Replicas:      2
Placement:
UpdateConfig:
 Parallelism:   1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Update order:      stop-first
RollbackConfig:
 Parallelism:   1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Rollback order:    stop-first
ContainerSpec:
 Image:         nginx-server:latest
 Init:          false
Resources:
Endpoint Mode:  vip
Ports:
 PublishedPort = 80
  Protocol = tcp
  TargetPort = 80
  PublishMode = ingress

# show service state

root@node01:~#
docker service ps swarm_cluster

ID             NAME              IMAGE                 NODE               DESIRED STATE   CURRENT STATE           ERROR     PORTS
oss7ewq6siia   swarm_cluster.1   nginx-server:latest   node02.srv.world   Running         Running 2 minutes ago
mjqgh9uex5bo   swarm_cluster.2   nginx-server:latest   node03.srv.world   Running         Running 2 minutes ago

# verify it works normally

root@node01:~#
curl node01.srv.world

Nginx on node02
root@node01:~#
curl node01.srv.world

Nginx on node03
root@node01:~#
curl node01.srv.world

Nginx on node02
root@node01:~#
curl node01.srv.world

Nginx on node03
[9] If you'd like to change the number of replicas, configure like follows.
# change replicas to 3

root@node01:~#
docker service scale swarm_cluster=3

swarm_cluster scaled to 3
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service swarm_cluster converged

root@node01:~#
docker service ps swarm_cluster

ID             NAME              IMAGE                 NODE               DESIRED STATE   CURRENT STATE            ERROR     PORTS
y9pc9klg3nqn   swarm_cluster.1   nginx-server:latest   node02.srv.world   Running         Running 2 minutes ago
aq33kd1jsco7   swarm_cluster.2   nginx-server:latest   node01.srv.world   Running         Running 2 minutes ago
eszevpheil0u   swarm_cluster.3   nginx-server:latest   node03.srv.world   Running         Running 36 seconds ago

# verify accesses

root@node01:~#
curl node01.srv.world

Nginx on node01
root@node01:~#
curl node01.srv.world

Nginx on node02
root@node01:~#
curl node01.srv.world

Nginx on node03
Matched Content