Fedora 29
Sponsored Link

Docker : Docker Swarm Cluster2018/11/07

 
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]
This example is based on the environment that SELnux is Permissive or Disabled and also Firewalld is disabled.
[2]
[3] On all Nodes, Disable live-restore option. It's impossible to enable Live-restore and Swarm-mode together.
[root@node01 ~]#
vi /etc/sysconfig/docker
# line 4: comment out and add a line which does not have [live-restore] word

#
OPTIONS='--selinux-enabled --log-driver=journald --live-restore'
OPTIONS='--selinux-enabled --log-driver=journald'
[root@node01 ~]#
systemctl restart docker

[4] Configure Swarm Cluster on Manager Node.
[root@node01 ~]#
docker swarm init

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

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

    docker swarm join \
    --token SWMTKN-1-2kvisbt5skhhq4temzsqn21vgjdfgn7mowe01dsorgaduamx80-c04zx7stbda65p8d56iiajg4m \
    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-2kvisbt5skhhq4temzsqn21vgjdfgn7mowe01dsorgaduamx80-c04zx7stbda65p8d56iiajg4m \
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
pojsu8tvixbfwpnrsj2tadlwg *  node01.srv.world  Ready   Active        Leader
tjjn75t9tmhmfwk6ftoz1gsp0    node03.srv.world  Ready   Active
vq7ycrxgpf8jhri1x1md5t5ry    node02.srv.world  Ready   Active
[7]
After creating Swarm Cluster, next, configure services that the Swarm Cluster provides.
Create the same container image on all Nodes for the service first.
On this exmaple, use a Container image which provides http service like an example of the link.
[8] Configure service on Manager Node. After successing to configure service, access to the Manager node's Hostname or IP address to verify it works normally. By the way, requests to worker nodes are load-balanced with round-robin like follows.
[root@node01 ~]#
docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
web_server          latest              016a56dd6080        11 minutes ago      512 MB
docker.io/fedora    latest              8c568f104326        3 hours ago         267 MB

# create a service with 2 repricas

[root@node01 ~]#
docker service create --name swarm_cluster --replicas=2 -p 80:80 web_server:latest

1xixq7jmy6la590k3d54uin0k
# show service list

[root@node01 ~]#
docker service ls

ID            NAME           MODE        REPLICAS  IMAGE
1xixq7jmy6la  swarm_cluster  replicated  2/2       web_server:latest

# inspect the service

[root@node01 ~]#
docker service inspect swarm_cluster --pretty

ID:             1xixq7jmy6la590k3d54uin0k
Name:           swarm_cluster
Service Mode:   Replicated
 Replicas:      2
Placement:
UpdateConfig:
 Parallelism:   1
 On failure:    pause
 Max failure ratio: 0
ContainerSpec:
 Image:         web_server:latest
Resources:
Endpoint Mode:  vip
Ports:
 PublishedPort 80
  Protocol = tcp
  TargetPort = 80

# show service state

[root@node01 ~]#
docker service ps swarm_cluster

ID            NAME             IMAGE              NODE              DESIRED STATE  CURRENT STATE          ERROR  PORTS
sgro87htqs3h  swarm_cluster.1  web_server:latest  node03.srv.world  Running        Running 7 minutes ago
a5vqor24ceg1  swarm_cluster.2  web_server:latest  node01.srv.world  Running        Running 7 minutes ago

# verify it works normally

[root@node01 ~]#
curl http://node01.srv.world/

node02
[root@node01 ~]#
curl http://node01.srv.world/

node03
[root@node01 ~]#
curl http://node01.srv.world/

node02
[root@node01 ~]#
curl http://node01.srv.world/

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

[root@node01 ~]#
docker service scale swarm_cluster=3

swarm_cluster scaled to 3
[root@node01 ~]#
docker service ps swarm_cluster

ID            NAME             IMAGE              NODE              DESIRED STATE  CURRENT STATE             ERROR  PORTS
sgro87htqs3h  swarm_cluster.1  web_server:latest  node03.srv.world  Running        Running 10 minutes ago
a5vqor24ceg1  swarm_cluster.2  web_server:latest  node01.srv.world  Running        Running 10 minutes ago
obm0zzilqptz  swarm_cluster.3  web_server:latest  node02.srv.world  Running        Preparing 14 seconds ago

# verify working

[root@node01 ~]#
curl http://node01.srv.world/

node02
[root@node01 ~]#
curl http://node01.srv.world/

node03
[root@node01 ~]#
curl http://node01.srv.world/

node01
Matched Content