Ubuntu 18.04
Sponsored Link

Docker : Configure Swarm Cluster2018/06/12

 
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] Configure Swarm Cluster on Manager Node.
root@node01:~#
docker swarm init

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

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

    docker swarm join --token SWMTKN-1-3dt7tg314vzxg8bb5ax64ldq6cqxznqj1rhozof1ewp5qmq3al-066oh7rcdnbt0mx1ysxnw3k3d 10.0.0.51:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[3] 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-3dt7tg314vzxg8bb5ax64ldq6cqxznqj1rhozof1ewp5qmq3al-066oh7rcdnbt0mx1ysxnw3k3d 10.0.0.51:2377

This node joined a swarm as a worker.
[4] 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
kstx00g477nbhtalspbkv4wfl *   node01.srv.world    Ready               Active              Leader
pzbva8u3pj1d4fscynfjle4bd     node02.srv.world    Ready               Active
z380j4bwyp9d2dy00dtzh56bv     node03.srv.world    Ready               Active
[5] 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, create a Container image which provides http service like follows on all Nodes.
root@node01:~#
vi Dockerfile
FROM ubuntu
MAINTAINER ServerWorld <admin@srv.world>

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

EXPOSE 80
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]

root@node01:~#
docker build -t apache2_server:latest ./

[6] 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
apache2_server      latest              6a1cbac1a456        13 minutes ago      218MB
ubuntu              latest              113a43faa138        6 days ago          81.2MB

# create a service with 2 repricas

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

jo17duq5qiupchrw9bcdrqi89
overall progress: 2 out of 2 tasks
1/2: running
2/2: running
verify: Service converged
# show service list

root@node01:~#
docker service ls

ID              NAME            MODE          REPLICAS    IMAGE                   PORTS
jo17duq5qiup    swarm_cluster   replicated    2/2         apache2_server:latest   *:80->80/tcp

# inspect the service

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

ID:             jo17duq5qiupchrw9bcdrqi89
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:         apache2_server:latest
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              ...
d2am5warj1w2  swarm_cluster.1  apache2_server:latest node02.srv.world  Running        Running about a minute ago
k6ql1oxoeemh  swarm_cluster.2  apache2_server:latest node01.srv.world  Running        Running about a minute ago

# verify it works normally

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

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

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

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

node01
[7] 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
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
root@node01:~#
docker service ps swarm_cluster

ID            NAME            IMAGE                 NODE              DESIRED STATE  CURRENT STATE             ...
d2am5warj1w2  swarm_cluster.1 apache2_server:latest node02.srv.world  Running        Running 4 minutes ago
k6ql1oxoeemh  swarm_cluster.2 apache2_server:latest node01.srv.world  Running        Running 4 minutes ago
eitu3hkbd5h4  swarm_cluster.3 apache2_server:latest node03.srv.world  Running        Running about a minute 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