To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
在work节点中使用上面的命令加入集群
1 2
$ sudo docker swarm join --token SWMTKN-1-5e8hc9wd657mw6gdauixga0yxdsppk4oywk6xm0e3ybasv24q0-02ytqkz23wy8ep1edid4f3xn1 192.168.192.132:2377 This node joined a swarm as a worker.
在manager节点中查看集群状态
1 2 3 4 5
$ sudo docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION 8xaaf06chmbub1d4dmx3auk06 tom-virtual-machine Ready Active 24.0.7 i30msqv8pimr68jq5hk9i7viq tom-virtual-machine Ready Active 20.10.7 ilq9xo3kl2xac3a2h65ewqqf1 * tom-virtual-machine Ready Active Leader 24.0.7
swarm集群使用
我们使用 docker service 命令来管理 Swarm 集群中的服务,该命令只能在管理节点运行。
1 2 3 4 5 6
$ sudo docker service create -p 80:80 --name nginx_s --replicas 2 nginx csbw2mr0ksykiqv6fmr1tz847 overall progress: 2 out of 2 tasks 1/2: running [==================================================>] 2/2: running [==================================================>] verify: Service converged
查看服务
1 2 3
$ sudo docker service ls ID NAME MODE REPLICAS IMAGE PORTS csbw2mr0ksyk nginx_s replicated 2/2 nginx:latest *:80->80/tcp
查看服务详情
1 2 3 4
sudo docker service ps nginx_s ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS w3cm931vedpq nginx_s.1 nginx:latest tom-virtual-machine Running Running 4 minutes ago eux1ujb4x6b2 nginx_s.2 nginx:latest tom-virtual-machine Running Running 4 minutes ago
服务伸缩
我们可以使用 docker service scale 对一个服务运行的容器数量进行伸缩。
当业务处于高峰期时,我们需要扩展服务运行的容器数量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$ sudo docker service scale nginx_s=5 nginx_s scaled to 5 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 tom@tom-virtual-machine:~$ sudo docker service ps nginx_s ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS w3cm931vedpq nginx_s.1 nginx:latest tom-virtual-machine Running Running 7 minutes ago eux1ujb4x6b2 nginx_s.2 nginx:latest tom-virtual-machine Running Running 7 minutes ago gzjxt3tspm1z nginx_s.3 nginx:latest tom-virtual-machine Running Running 42 seconds ago t22i858fhgmy nginx_s.4 nginx:latest tom-virtual-machine Running Running 17 seconds ago wfuhc11v692n nginx_s.5 nginx:latest tom-virtual-machine Running Running 18 seconds ago
当业务平稳时,我们需要减少服务运行的容器数量。
1 2 3 4 5 6 7 8 9 10 11 12
$ sudo docker service scale nginx_s=3 nginx_s scaled to 3 overall progress: 3 out of 3 tasks 1/3: running [==================================================>] 2/3: running [==================================================>] 3/3: running [==================================================>] verify: Service converged $ sudo docker service ps nginx_s ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS w3cm931vedpq nginx_s.1 nginx:latest tom-virtual-machine Running Running 9 minutes ago eux1ujb4x6b2 nginx_s.2 nginx:latest tom-virtual-machine Running Running 9 minutes ago t22i858fhgmy nginx_s.4 nginx:latest tom-virtual-machine Running Running 2 minutes ago