在本节中,主要介绍在Docker swarm中如何不中断应用高可靠性的情况下更新服务和stack.这也叫做zero downtime deployment.还有就是swam如何管理密钥,保证容器之间的通信是安全可靠的。
一. Zero downtime deployment
1. 比较常见的更新方法
(1) Rolling updates
(2) Blue-green deployments
(3)Canary releases
2. Rolling updates
在关键的应用中,每个应用的服务都运行多个复本。举个例子,假如我们有三个复本实例,认为至少得有两个实例在同一时间运行,假如有100个实例复本,至少得有90实例复本在同一时间要有效。所以我们可以定义同时更新的实例有多少 个。在第二种假设的情况中,就是同时更新实例数为10个。
说白话一点就是假如一个应用有100个实例容器提供服务,那么我们每次更新10个,这样轮着更新。直至100个更新完,在这期间不停止服务。
注意:在署在多个节点的服务进行rolling updates时,最好是无状态的服务,假如是有状态的服务,建议调整方法,这个方法可能不太适合。
(1)定义stack文件 ,内容如下:
xiodi@c720131:~/docker$ cat stack.yaml
version: "3.5"
services:
web:
image: nginx:1.12-alpine
ports:
- 80:80
deploy:
replicas: 10
update_config:
parallelism: 2
delay: 10s
(2)使用stack文件部署服务
xiodi@c720131:~/docker$ sudo docker stack deploy -c stack.yaml web
[sudo] password for xiodi:
Creating network web_default
Creating service web_web
(3)在服务部署后,使用下面的命令进行服务的监控
xiodi@c720131:~/docker$ sudo watch docker stack ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
tsmz1yh1r8sq web_web.1 nginx:1.12-alpine c720131.xiodi.cn Running Running about a minute ago
9efbgiu8xfu6 web_web.2 nginx:1.12-alpine c720132.xiodi.cn Running Running about a minute ago
j3ka20b1330l web_web.3 nginx:1.12-alpine c720132.xiodi.cn Running Running about a minute ago
t96zpspkrdll web_web.4 nginx:1.12-alpine c720132.xiodi.cn Running Running about a minute ago
q7vgy2b5ripc web_web.5 nginx:1.12-alpine ubuntu Running Running about a minute ago
lkrxparc8y5x web_web.6 nginx:1.12-alpine c720131.xiodi.cn Running Running about a minute ago
0nw5y74ow3vc web_web.7 nginx:1.12-alpine ubuntu Running Running about a minute ago
wpsn04qxzy0a web_web.8 nginx:1.12-alpine c720131.xiodi.cn Running Running about a minute ago
qcernrxbachq web_web.9 nginx:1.12-alpine ubuntu Running Running about a minute ago
kd5dbexndk61 web_web.10 nginx:1.12-alpine c720132.xiodi.cn Running Running about a minute ago
(4)更新web服务,然后观察结果 。
(5)更新完成后,显示结果如下
xiodi@c720131:~/docker$ sudo docker stack ps web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
y3e6nsy7qa0a web_web.1 nginx:1.13-alpine c720131.xiodi.cn Running Running 3 minutes ago
tsmz1yh1r8sq \_ web_web.1 nginx:1.12-alpine c720131.xiodi.cn Shutdown Shutdown 3 minutes ago
zkmadin1p6ir web_web.2 nginx:1.13-alpine c720132.xiodi.cn Running Running 3 minutes ago
9efbgiu8xfu6 \_ web_web.2 nginx:1.12-alpine c720132.xiodi.cn Shutdown Shutdown 3 minutes ago
m4xhcns8j57p web_web.3 nginx:1.13-alpine ubuntu Running Running 4 minutes ago
j3ka20b1330l \_ web_web.3 nginx:1.12-alpine c720132.xiodi.cn Shutdown Shutdown 4 minutes ago
m884or5qpns4 web_web.4 nginx:1.13-alpine c720132.xiodi.cn Running Running 3 minutes ago
t96zpspkrdll \_ web_web.4 nginx:1.12-alpine c720132.xiodi.cn Shutdown Shutdown 3 minutes ago
rlzj3jqdz6zc web_web.5 nginx:1.13-alpine c720132.xiodi.cn Running Running 3 minutes ago
q7vgy2b5ripc \_ web_web.5 nginx:1.12-alpine ubuntu Shutdown Shutdown 3 minutes ago
ntzku0o2fdh9 web_web.6 nginx:1.13-alpine c720131.xiodi.cn Running Running 3 minutes ago
lkrxparc8y5x \_ web_web.6 nginx:1.12-alpine c720131.xiodi.cn Shutdown Shutdown 3 minutes ago
l2j1mml2c21e web_web.7 nginx:1.13-alpine ubuntu Running Running 4 minutes ago
0nw5y74ow3vc \_ web_web.7 nginx:1.12-alpine ubuntu Shutdown Shutdown 4 minutes ago
tdvpcny3zjf8 web_web.8 nginx:1.13-alpine c720131.xiodi.cn Running Running 3 minutes ago
wpsn04qxzy0a \_ web_web.8 nginx:1.12-alpine c720131.xiodi.cn Shutdown Shutdown 3 minutes ago
nkkjxxq9xos1 web_web.9 nginx:1.13-alpine ubuntu Running Running 3 minutes ago
qcernrxbachq \_ web_web.9 nginx:1.12-alpine ubuntu Shutdown Shutdown 3 minutes ago
px1bxz2omzfq web_web.10 nginx:1.13-alpine c720132.xiodi.cn Running Running 3 minutes ago
kd5dbexndk61 \_ web_web.10 nginx:1.12-alpine c720132.xiodi.cn Shutdown Shu
(5)执行下列命令,移除stack.
xiodi@c720131:~/docker$ sudo docker stack rm web
Removing service web_web
Removing network web_default
(6)如果部署单一的服务的话,可以使用以下命令设置新的一些rolling update值。
xiodi@c720131:~/docker$ sudo docker service create --name web
> --replicas 10
> --update-parallelism 2
> --update-delay 10s
> nginx:alpine