CentOS 7
Ads

Docker : Use Docker Swarm
2017/04/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] Install and run Docker service on all nodes, refer to here.
The Docker version must be up to v1.12. If your Docker is not the version, update Docker with [yum update docker], it's possible to update it from CentOS Extras repository.
[root@node01 ~]#
docker -v

Docker version 1.12.6, build 96d83a5/1.12.6
[2] Turn to disable "live-restore" option on all Nodes. (because it can not run with Swarm mode)
[root@node01 ~]#
vi /etc/docker/daemon.json
{
    "live-restore": false
}

[root@node01 ~]#
systemctl restart docker

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

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

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

    docker swarm join \
    --token SWMTKN-1-1l6o776w0ktvg6vw4cony5et76gkrjzw4xke0cmas3ftl6ghnm-8ppur7wbl9bej6fjcrdtaufsg \
    10.0.0.51:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[4] 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-1l6o776w0ktvg6vw4cony5et76gkrjzw4xke0cmas3ftl6ghnm-8ppur7wbl9bej6fjcrdtaufsg \
10.0.0.51:2377

This node joined a swarm as a worker.
[5] 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
2f7pmmoilf753g7wfctcik7yh *  node01.srv.world  Ready   Active        Leader
6ok99v3dkt9mg4jvzg7qi4zqb    node02.srv.world  Ready   Active
a3dtuaqibnrym3u3wg32cuq7p    node03.srv.world  Ready   Active
[6]
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.
[7] 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              b7cf7440922c        4 minutes ago       306.4 MB
docker.io/centos    latest              98d35105a391        3 weeks ago         192.5 MB

# create a service with 2 repricas

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

7xg4yssy516xwgkjx2vxbw05d
# show service list

[root@node01 ~]#
docker service ls

ID            NAME           REPLICAS  IMAGE              COMMAND
7xg4yssy516x  swarm_cluster  2/2       web_server:latest

# inspect the service

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

ID:             7xg4yssy516xwgkjx2vxbw05d
Name:           swarm_cluster
Mode:           Replicated
 Replicas:      2
Placement:
UpdateConfig:
 Parallelism:   1
 On failure:    pause
ContainerSpec:
 Image:         web_server:latest
Resources:
Ports:
 Protocol = tcp
 TargetPort = 80
 PublishedPort = 80

# show service status

[root@node01 ~]#
docker service ps swarm_cluster

ID              NAME             IMAGE              NODE              DESIRED STATE  CURRENT STATE               ERROR
78lh8sn1myw...  swarm_cluster.1  web_server:latest  node01.srv.world  Running        Running about a minute ago
4ag74wr59rb...  swarm_cluster.2  web_server:latest  node03.srv.world  Running        Running about a minute ago

# verify it works normally

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

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

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

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

Docker Node : node01.srv.world
[8] 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
78lh8sn1myw...  swarm_cluster.1  web_server:latest  node01.srv.world  Running        Running 34 minutes ago
4ag74wr59rb...  swarm_cluster.2  web_server:latest  node03.srv.world  Running        Running 34 minutes ago
46ecs1c0tjv...  swarm_cluster.3  web_server:latest  node02.srv.world  Running        Running 13 seconds ago

# verify working

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

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

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

Docker Node : node03.srv.world
 
Tweet