$ docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 1s6uw7ew22xhvy5trv2ejnncd worker2 Ready Active 3pniknvvlt9hb5bjpdnwkp5zr * manager Ready Active Leader beh2b7riuqv7oz5nhbhuvmr0t worker1 Ready Active
$ docker service ps helloworld ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR c81lt2t8ride9ioduqv86i0v7 helloworld.1 alpine manager Running Running 7 minutes ago
这里要关注 DESIRED STATE 和 LAST STATE 两个状态。
在服务所在节点查看进程,这里是 manager
1 2 3
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 458df9db3e9a alpine:latest "ping docker.com" 10 minutes ago Up 10 minutes helloworld.1.c81lt2t8ride9ioduqv86i0v7
服务扩展
还在是 manager 节点,通过命令,我们可以改变集群中的节点实例。
1
$ docker service scale <SERVICE-ID>=<NUMBER-OF-TASKS>
在这里,我们把 helloworld 实例扩展到 5 个
1 2
$ docker service scale helloworld=5 helloworld scaled to 5
查看服务实例运行节点分布
1 2 3 4 5 6 7
$ docker service ps helloworld ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR c81lt2t8ride9ioduqv86i0v7 helloworld.1 alpine manager Running Running 20 minutes ago b98r4ypet6b9doiof1bb0aq68 helloworld.2 alpine worker1 Running Running 5 seconds ago 20hkvi1i3ihur0x17a989qvpy helloworld.3 alpine manager Running Running 8 seconds ago 472p0ykqnwtjvmo10bx1kutz8 helloworld.4 alpine worker2 Running Running 2 seconds ago dfmx6i4aunk27m8xstavncwxa helloworld.5 alpine worker1 Running Running 2 seconds ago
可以看到,manager 和 worker1 有 2 个实例,worker2 有 1 个实例。
到 manager 上执行
1 2 3 4
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e90f1e77d7aa alpine:latest "ping docker.com" About a minute ago Up About a minute helloworld.3.20hkvi1i3ihur0x17a989qvpy 458df9db3e9a alpine:latest "ping docker.com" 22 minutes ago Up 22 minutes helloworld.1.c81lt2t8ride9ioduqv86i0v7
到 worker1 上执行
1 2 3 4
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 806eb838fd86 alpine:latest "ping docker.com" 2 minutes ago Up 2 minutes helloworld.5.dfmx6i4aunk27m8xstavncwxa faa01a019dcf alpine:latest "ping docker.com" 2 minutes ago Up 2 minutes helloworld.2.b98r4ypet6b9doiof1bb0aq68
到 worker2 上执行
1 2 3
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f020134c72bb alpine:latest "ping docker.com" 2 minutes ago Up 2 minutes helloworld.4.472p0ykqnwtjvmo10bx1kutz8
删除服务
1
$ docker service rm helloworld
确认是否删除
1 2 3
$ docker service inspect helloworld [] Error: no such service: helloworld
$ docker service ps redis ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR 27mlfg8pqlvz9w4yky1q9fxm7 redis.1 redis:3.0.6 worker1 Running Running 2 seconds ago ejlctv6j92caxapd7g2bll0bo redis.2 redis:3.0.6 manager Running Preparing 16 seconds ago 3l0nu4zt99kecwig1sfie1km9 redis.3 redis:3.0.6 worker2 Running Preparing 16 seconds ago
这里要关注 CURRENT STATE,上边的状态说明 worker1 实例已经 Running,但 manager 和 worker2 还在 Preparing。 到各自主机上用 docker ps 可以证明这一点。等待片刻,所有实例的 CURRENT STATE 都变成 Running。
$ docker service ps redis ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR 27mlfg8pqlvz9w4yky1q9fxm7 redis.1 redis:3.0.6 worker1 Running Running 18 minutes ago ejlctv6j92caxapd7g2bll0bo redis.2 redis:3.0.6 manager Running Running 2 minutes ago 3l0nu4zt99kecwig1sfie1km9 redis.3 redis:3.0.6 worker2 Running Running 11 minutes ago
$ docker service ps redis ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR cf3stbcfz3zdmw5mgogya6amd redis.1 redis:3.0.7 manager Running Running 2 minutes ago 27mlfg8pqlvz9w4yky1q9fxm7 \_ redis.1 redis:3.0.6 worker1 Shutdown Shutdown 2 minutes ago ewv52c73p0klbx6hmofxm72ti redis.2 redis:3.0.7 worker2 Running Running about a minute ago ejlctv6j92caxapd7g2bll0bo \_ redis.2 redis:3.0.6 manager Shutdown Shutdown about a minute ago 4rcaexit4kupwcjrxdnjftgln redis.3 redis:3.0.7 worker1 Running Running about a minute ago 3l0nu4zt99kecwig1sfie1km9 \_ redis.3 redis:3.0.6 worker2 Shutdown Shutdown about a minute ago
拉掉节点
有时,比如计划维护时间,您需要将节点设置为不可用。 DRAIN 可用性防止节点从 swarm 管理器接收新任务。 它还意味着管理器停止在节点上运行的任务,并在具有 ACTIVE 可用性的节点上启动副本任务。
$ docker service ps my-web ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR a90s5hnb58dck8cxny4k2xgdk my-web.1 nginx worker1 Running Running 2 minutes ago 2148yi9va70eu27xl6nfi9une my-web.2 nginx worker2 Running Running about a minute ago