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
IDNAMEMODEREPLICASIMAGEPORTS
426kwqf7gxifbusyboxreplicated1/1busybox:latest
  • replicated 表示此服务可以水平扩展
  • 1/1 分母表示scale的数量,分子表示running的数量

水平扩展service

# 水平扩展为5个
docker service scale busybox=5

再次查看service列表

[vagrant@swarm-manager ~]$ docker service ls
IDNAMEMODEREPLICASIMAGEPORTS
426kwqf7gxifbusyboxreplicated5/5busybox:latest

查看service的分布

docker service ps busybox
IDNAMEIMAGENODEDESIRED STATECURRENT STATEERRORPORTS
gf76bj8oe9u7busybox.1busybox:latestswarm-managerRunningRunning 9 minutes ago
5htg690swaw2busybox.2busybox:latestswarm-work2RunningRunning about a minute ago
b6v2qfmke4hubusybox.3busybox:latestswarm-managerRunningRunning about a minute ago
ywyt6d4pf918busybox.4busybox:latestswarm-work1RunningRunning about a minute ago
nutiiyw70rz2busybox.5busybox:latestswarm-work2RunningRunning 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
阅读 311

从转行到入门,记录我的自学成长之路

59 声望
15 粉丝
0 条评论
你知道吗?

从转行到入门,记录我的自学成长之路

59 声望
15 粉丝
文章目录
宣传栏