Docker Swarm 2020(1)Simply Install and Use Swarm
Docker Swarm 2020(1)Simply Install and Use Swarm
Check my Docker
docker --version Docker version 19.03.8, build afacb8b
Swarm Manager - rancher-home Swarm Worker - rancher-worker1 Swarm Worker - rancher-worker2
Some useful ports: 2377 - cluster management 7946 - node to node 4789 - overlay network
On rancher-home
docker swarm init --advertise-addr 192.168.56.110 Swarm initialized: current node (st1b4xuylrj6mm5oggsk6hp04) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-3ak17tlxe1ljqv8pk7gn1kwrjbz9hzgkn48wh8vu27o39ypjf6-b4mxpq45n2y0i6dpel3hwjlyw 192.168.56.110:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
If I forget about the token information just in case
docker swarm join-token worker To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-3ak17tlxe1ljqv8pk7gn1kwrjbz9hzgkn48wh8vu27o39ypjf6-b4mxpq45n2y0i6dpel3hwjlyw 192.168.56.110:2377
docker info Swarm: active NodeID: st1b4xuylrj6mm5oggsk6hp04 Is Manager: true ClusterID: linol1nuatvwjbvquh1fg7x8h Managers: 1 Nodes: 1 Default Address Pool: 10.0.0.0/8
SubnetSize: 24 Data Path Port: 4789
docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION st1b4xuylrj6mm5oggsk6hp04 * rancher-home Ready Active Leader 19.03.8
Add the worker node to the swarm cluster On rancher-worker1
docker --version Docker version 19.03.8, build afacb8b
Join the cluster
docker swarm join --token SWMTKN-1-3ak17tlxe1ljqv8pk7gn1kwrjbz9hzgkn48wh8vu27o39ypjf6-b4mxpq45n2y0i6dpel3hwjlyw 192.168.56.110:2377
On rancher-worker2
docker --version Docker version 19.03.8, build afacb8b
docker swarm join --token SWMTKN-1-3ak17tlxe1ljqv8pk7gn1kwrjbz9hzgkn48wh8vu27o39ypjf6-b4mxpq45n2y0i6dpel3hwjlyw 192.168.56.110:2377
On the manager machine, we can see all the nodes
docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION st1b4xuylrj6mm5oggsk6hp04 * rancher-home Ready Active Leader 19.03.8 d85e6hmp2saf2xfyiq8nopvkp rancher-worker1 Ready Active 19.03.8 ccr3520glvgpyg7dn1zbkr8zg rancher-worker2 Ready Active 19.03.8
Remove the node from cluster
docker swarm leave Node left the swarm.
docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION st1b4xuylrj6mm5oggsk6hp04 * rancher-home Ready Active Leader 19.03.8 d85e6hmp2saf2xfyiq8nopvkp rancher-worker1 Ready Active 19.03.8 ccr3520glvgpyg7dn1zbkr8zg rancher-worker2 Down Active 19.03.8
Remove it from the manager
docker node rm --force ccr3520glvgpyg7dn1zbkr8zg ccr3520glvgpyg7dn1zbkr8zg
docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION st1b4xuylrj6mm5oggsk6hp04 * rancher-home Ready Active Leader 19.03.8 d85e6hmp2saf2xfyiq8nopvkp rancher-worker1 Ready Active 19.03.8
Manager and Deploy Service Create a simple service
docker service create --replicas 1 --name helloworld alpine ping docker.com m45as37skje2gts6t6curew77 overall progress: 1 out of 1 tasks 1/1: running
verify: Service converged
List the service
docker service ls ID NAME MODE REPLICAS IMAGE PORTS m45as37skje2 helloworld replicated 1/1 alpine:latest
Check the detail
docker service inspect --pretty helloworld ID: m45as37skje2gts6t6curew77 Name: helloworld Service Mode: Replicated Replicas: 1 Placement: UpdateConfig: Parallelism: 1 On failure: pause Monitoring Period: 5s Max failure ratio: 0 Update order: stop-first
List the server information
docker service ps helloworld ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS vo5d6agsy1dw helloworld.1 alpine:latest rancher-home Running Running about a minute ago
Scale the service to 5
docker service scale helloworld=5
List all the services
docker service ps helloworld ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS 30nlrg5uu4c8 helloworld.1 alpine:latest rancher-home Running Running 3 minutes ago
vo5d6agsy1dw _ helloworld.1 alpine:latest rancher-home Shutdown Failed 3 minutes ago "task: non-zero exit (255)"
fa9e3qlmcm47 helloworld.2 alpine:latest rancher-worker2 Running Running 30 seconds ago
8mr4dyaga779 helloworld.3 alpine:latest rancher-worker1 Running Running 29 seconds ago
7hbk7w99fo8z helloworld.4 alpine:latest rancher-worker1 Running Running 29 seconds ago
0s7rbd9vwv3r helloworld.5 alpine:latest rancher-home Running Running 35 seconds ago
Remove the service
docker service rm helloworld
Run and Update the Tomcat Service
docker service create --replicas 1 --name tomcat-service tomcat:8.5.8
Scale up to 2
docker service scale tomcat-service=2
Rolling update to tomcat:9.0.33
docker service update --image tomcat:9.0.33 tomcat-service
Check status
service ps tomcat-service ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS tei2ojnu7jxk tomcat-service.1 tomcat:9.0.33 rancher-worker2 Running Running 33 seconds ago
p6fg4k7l2jie _ tomcat-service.1 tomcat:8.5.8 rancher-worker2 Shutdown Shutdown 53 seconds ago
q2it5kmfvois tomcat-service.2 tomcat:9.0.33 rancher-home Running Running 55 seconds ago
e3dnf70mx0km _ tomcat-service.2 tomcat:8.5.8 rancher-home Shutdown Shutdown about a minute ago
r5kvdoruqdcf tomcat-service.3 tomcat:9.0.33 rancher-worker1 Running Running about a minute ago
33j1j47rk4pn _ tomcat-service.3 tomcat:8.5.8 rancher-worker1 Shutdown Shutdown about a minute ago
Only see 3 history
docker swarm update --task-history-limit 3
References: https://yq.aliyun.com/articles/225511 https://outmanzzq.github.io/2019/06/14/portainer-for-docker-swarm/ https://note.qidong.name/2018/11/docker-swarm/ https://www.jianshu.com/p/df744c4e375e https://github.com/jasonGeng88/blog/blob/master/201704/docker_swarm.md https://stackoverflow.com/questions/41705134/how-can-i-remove-shutdown-docker-service-tasks-after-a-rolling-update
Maybe an useful tool https://github.com/docker/machine