乐趣区

关于swarm集群:73-Service的创建维护和水平扩展

创立一个 service

在 swarm 模式下,基本上就不会应用 docker run 了

# swarm-manager 节点
docker service create --name busybox busybox /bin/sh -c "while true;do sleep 3600;done"

查看 service 列表

[vagrant@swarm-manager ~]$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
426kwqf7gxif busybox replicated 1/1 busybox:latest
  • replicated 示意此服务能够程度扩大
  • 1/1 分母示意 scale 的数量,分子示意 running 的数量

程度扩大 service

# 程度扩大为 5 个
docker service scale busybox=5

再次查看 service 列表

[vagrant@swarm-manager ~]$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
426kwqf7gxif busybox replicated 5/5 busybox:latest

查看 service 的散布

docker service ps busybox
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
gf76bj8oe9u7 busybox.1 busybox:latest swarm-manager Running Running 9 minutes ago
5htg690swaw2 busybox.2 busybox:latest swarm-work2 Running Running about a minute ago
b6v2qfmke4hu busybox.3 busybox:latest swarm-manager Running Running about a minute ago
ywyt6d4pf918 busybox.4 busybox:latest swarm-work1 Running Running about a minute ago
nutiiyw70rz2 busybox.5 busybox:latest swarm-work2 Running Running about a minute ago

模仿容器 down 掉

# swarm-work2 节点
docker container stop fc23134c1f30
fc23134c1f30

而后咱们就能够看出 scale 会主动复原为 5

[vagrant@swarm-manager ~]$ docker service ps busybox
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
gf76bj8oe9u7 busybox.1 busybox:latest swarm-manager Running Running 34 minutes ago
24boc8ooo2wd busybox.2 busybox:latest swarm-work1 Ready Ready less than a second ago
5htg690swaw2 \_ busybox.2 busybox:latest swarm-work2 Shutdown Failed less than a second ago "task: non-zero exit (137)"
b6v2qfmke4hu busybox.3 busybox:latest swarm-manager Running Running 27 minutes ago
ywyt6d4pf918 busybox.4 busybox:latest swarm-work1 Running Running 27 minutes ago
nutiiyw70rz2 busybox.5 busybox:latest swarm-work2 Running Running 27 minutes ago
[vagrant@swarm-manager ~]$ docker service ps busybox
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
gf76bj8oe9u7 busybox.1 busybox:latest swarm-manager Running Running 34 minutes ago
24boc8ooo2wd busybox.2 busybox:latest swarm-work1 Running Running 8 seconds ago
5htg690swaw2 \_ busybox.2 busybox:latest swarm-work2 Shutdown Failed 13 seconds ago "task: non-zero exit (137)"
b6v2qfmke4hu busybox.3 busybox:latest swarm-manager Running Running 27 minutes ago
ywyt6d4pf918 busybox.4 busybox:latest swarm-work1 Running Running 27 minutes ago
nutiiyw70rz2 busybox.5 busybox:latest swarm-work2 Running Running 27 minutes ago

这就是 swarm 的容器主动复原机制了

service 移除

docker service rm busybox
退出移动版