105、Replicated Mode VS Global Mode (Swarm12)
Swarm 可以在 Service 创建和运行过程中灵活的通过 --replicas 调整容器副本数量,内部调度器则会根据当前集群的资源使用情况在不同的node 上启停容器,这就是 Service 默认的 replicated mode 。在此模式下,node 上运行的副本数有多有少,一般情况下,资源更丰富的node 运行的副本数更多,反之亦然。
除了 replicated mode ,Service 还提供了一个 global mode ,其作用是强制在每个node上都运行一个且最多一个副本。
global mode 特别适合需要运行daemon 的集群环境。比如要收集所有容器的日志,就可以用 global mode 创建 Service,在所有 node 上都运行 gliderlabs/logspout 容器,及时之后有新的node加入,swarm 也会自动在新的node上启动一个 gliderlabs/logspout 副本
root@host03:~# docker service create --mode global --name bbox busybox sleep 99999
fvbo6fpu7gb29qi0kl2ausunt
overall progress: 2 out of 2 tasks
asn5ufnogzky: running
hvt2ez9e7zvq: running
verify: Service converged
root@host03:~# docker service ps bbox
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
9zo4pzhl7ysf bbox.asn5ufnogzkyqigk4mizatoer busybox:latest host02 Running Running 10 seconds ago
xuxima4u6am4 bbox.hvt2ez9e7zvqm2hz8nix1eke7 busybox:latest host01 Running Running 10 seconds ago
root@host03:~# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
hvt2ez9e7zvqm2hz8nix1eke7 host01 Ready Active 18.09.6
asn5ufnogzkyqigk4mizatoer host02 Ready Active 18.09.6
h6rzavsz2vjxstwj3pytiebjb * host03 Ready Drain Leader 18.09.6
root@host03:~# docker node update host03 --availability active
host03
root@host03:~# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
hvt2ez9e7zvqm2hz8nix1eke7 host01 Ready Active 18.09.6
asn5ufnogzkyqigk4mizatoer host02 Ready Active 18.09.6
h6rzavsz2vjxstwj3pytiebjb * host03 Ready Active Leader 18.09.6
root@host03:~# docker service ps bbox
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
j4bmhr5yqokk bbox.h6rzavsz2vjxstwj3pytiebjb busybox:latest host03 Running Preparing 5 seconds ago
9zo4pzhl7ysf bbox.asn5ufnogzkyqigk4mizatoer busybox:latest host02 Running Running about a minute ago
xuxima4u6am4 bbox.hvt2ez9e7zvqm2hz8nix1eke7 busybox:latest host01 Running Running about a minute ago
root@host03:~# docker node update host03 --availability drain
host03
root@host03:~# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
hvt2ez9e7zvqm2hz8nix1eke7 host01 Ready Active 18.09.6
asn5ufnogzkyqigk4mizatoer host02 Ready Active 18.09.6
h6rzavsz2vjxstwj3pytiebjb * host03 Ready Drain Leader 18.09.6
root@host03:~# docker service ps bbox
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
j4bmhr5yqokk bbox.h6rzavsz2vjxstwj3pytiebjb busybox:latest host03 Shutdown Shutdown less than a second ago
9zo4pzhl7ysf bbox.asn5ufnogzkyqigk4mizatoer busybox:latest host02 Running Running 3 minutes ago
xuxima4u6am4 bbox.hvt2ez9e7zvqm2hz8nix1eke7 busybox:latest host01 Running Running 3 minutes ago
上面实验验证了 global mode 下,当有新的node加入时(这里用host03 解除 Drain演示),会自动启动一个副本。而当host03再次标记为 Drain时,host03上的副本会被shutdown
无论我们采用哪种mode ,我们都无法精细的控制副本运行在哪个节点上。