7.3 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

你可能感兴趣的:(dockerswarm集群)