Run the following command to initialize docker swarm.
root@ip-172-31-91-97:/home/ubuntu# docker swarm init --advertise-addr 52.203.29.219
Swarm initialized: current node (zhqxor0uw79okh8thcm6boj0o) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-105nukjje4l0yys14czxrpma8a2cxy1xx2y2n09u490pw1ve4i-4faqsr4p0hrjofywizcp04c6r 52.203.29.219:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
To join your node to the master apply the following command in the node server.
root@ip-172-31-93-154:/home/ubuntu# docker swarm join --token SWMTKN-1-105nukjje4l0yys14czxrpma8a2cxy1xx2y2n09u490pw1ve4i-4faqsr4p0hrjofywizcp04c6r 52.203.29.219:2377
This node joined a swarm as a worker.
To list the nodes:
root@ip-172-31-91-97:/home/ubuntu# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
zhqxor0uw79okh8thcm6boj0o * ip-172-31-91-97 Ready Active Leader 18.09.7
6wc42orryexx8nft23wiz7ekv ip-172-31-93-154 Ready Active 18.09.7
Trying to launch 5 replicas of the
root@ip-172-31-91-97:/home/ubuntu# docker service create --replicas 5 -p 80:80 --name web nginx
mqjtaf0ir040bt6ueyrlm782d
overall progress: 5 out of 5 tasks
1/5: running [==================================================>]
2/5: running [==================================================>]
3/5: running [==================================================>]
4/5: running [==================================================>]
5/5: running [==================================================>]
verify: Service converged
Find out the status of the service, by using the following command:
root@ip-172-31-91-97:/home/ubuntu# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
mqjtaf0ir040 web replicated 5/5 nginx:latest *:80->80/tcp
Please access the master IP “52.203.29.219” in the web browser to see the Nginx page
How it’s orchestrated to the different nodes by using the following command:
root@ip-172-31-91-97:/home/ubuntu# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
kmsc094t6yb7 web.1 nginx:latest master Running Running 16 minutes ago
im1a7ktqlatx web.2 nginx:latest node Running Running 16 minutes ago
on6grrbbsyho web.3 nginx:latest node Running Running 16 minutes ago
dvhd6iiw9pu9 web.4 nginx:latest master Running Running 16 minutes ago
ippgbdeevik8 web.5 nginx:latest node Running Running 16 minutes ago
Run the “docker ps” in both Master and Node to see the clusters.
In Master:
root@ip-172-31-91-97:/home/ubuntu# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c8ba8995c33f nginx:latest "nginx -g 'daemon of…" 17 minutes ago Up 17 minutes 80/tcp web.1.kmsc094t6yb7ghfcnt0758gc5
561248a69f72 nginx:latest "nginx -g 'daemon of…" 17 minutes ago Up 17 minutes 80/tcp web.4.dvhd6iiw9pu9w7guhwabgqz1i
In Node:
root@ip-172-31-93-154:/home/ubuntu# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1238146e70b5 nginx:latest "nginx -g 'daemon of…" 17 minutes ago Up 17 minutes 80/tcp web.2.im1a7ktqlatxx0jum4h8ce0gv
e170b9d89acd nginx:latest "nginx -g 'daemon of…" 17 minutes ago Up 17 minutes 80/tcp web.5.ippgbdeevik8s7cja241lebes
aabfb9c3fa7b nginx:latest "nginx -g 'daemon of…" 17 minutes ago Up 17 minutes 80/tcp web.3.on6grrbbsyhodx401oy31p9d9
Scale up container using the following command:
root@ip-172-31-91-97:/home/ubuntu# docker service scale web=8
web scaled to 8
overall progress: 8 out of 8 tasks
1/8: running [==================================================>]
2/8: running [==================================================>]
3/8: running [==================================================>]
4/8: running [==================================================>]
5/8: running [==================================================>]
6/8: running [==================================================>]
7/8: running [==================================================>]
8/8: running [==================================================>]
verify: Waiting 4 seconds to verify that tasks are stable…
verify: Service converged
Manager will stop tasks running on that node and launch the replicas on other nodes . In case the other node is a Master because we only have two nodes ( one master and one slave) 🙂
root@ip-172-31-91-97:/home/ubuntu# docker node update --availability drain node
node
Now our master rescheduled the containers to other node(master)
root@ip-172-31-91-97:/home/ubuntu# docker service ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
kmsc094t6yb7 web.1 nginx:latest master Running Running about an hour ago
mb1h3dk584l2 web.2 nginx:latest master Running Running 3 minutes ago
im1a7ktqlatx _ web.2 nginx:latest node Shutdown Shutdown 4 minutes ago
9hyvj3p54oih web.3 nginx:latest master Running Running 3 minutes ago
on6grrbbsyho _ web.3 nginx:latest node Shutdown Shutdown 4 minutes ago
dvhd6iiw9pu9 web.4 nginx:latest master Running Running about an hour ago
j8mmgjjxy9un web.5 nginx:latest master Running Running 3 minutes ago
ippgbdeevik8 _ web.5 nginx:latest node Shutdown Shutdown 4 minutes ago
s3lmyklnt5t2 web.6 nginx:latest master Running Running 9 minutes ago
9k6moe4r88hr web.7 nginx:latest master Running Running 9 minutes ago
n8xmrybrgzfa web.8 nginx:latest master Running Running 4 minutes ago
7xl49qa3qosj _ web.8 nginx:latest node Shutdown Shutdown 4 minutes ago