102、如何滚动更新 Service (Swarm09)
在前面的实验中,我们部署了多个副本的服务,本节将讨论如何滚动更新每一个副本。
滚动更新降低了应用更新的风险,如果某个副本更新失败,整个更新将暂停,其他副本则可以继续提供服务。
同时在更新的过程中,总是有副本在运行的,因此也保证了业务的连续性。
下面我们进行一个实验,将一个 3 副本的httpd Service,我们将 httpd镜像的版本进行升级
httpd:2.4.35 升级到 httpd:2.4.37
root@host03:~# docker service create --name httpd_2435 --replicas 3 httpd:2.4.35 # 创建Service 镜像 httpd:2.4.35,副本数3
8ppmoni91g7u22z4f4if61ro7
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
root@host03:~# docker service update --image httpd:2.4.37 httpd_2435 # 更新Service 镜像到 httpd:2.4.37
httpd_2435
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
root@host03:~# docker service ps httpd_2435 # 更新前Service各副本状态
ID NAME IMAGE NODE DESIRED ST PORTS
hkb5vb4ocqu6 httpd_2435.1 httpd:2.4.35 host02 Running
3v4vikx98pgm httpd_2435.2 httpd:2.4.35 host01 Running
d58skm47doqf httpd_2435.3 httpd:2.4.35 host02 Running
root@host03:~# docker service ps httpd_2435 # 更新副本2过程中
ID NAME IMAGE NODE DESIRED ST PORTS
hkb5vb4ocqu6 httpd_2435.1 httpd:2.4.35 host02 Running
ys34lst9obij httpd_2435.2 httpd:2.4.37 host01 Ready
3v4vikx98pgm \_ httpd_2435.2 httpd:2.4.35 host01 Shutdown
d58skm47doqf httpd_2435.3 httpd:2.4.35 host02 Running
root@host03:~# docker service ps httpd_2435 # 更新副本2完毕
ID NAME IMAGE NODE DESIRED ST PORTS
hkb5vb4ocqu6 httpd_2435.1 httpd:2.4.35 host02 Running
ys34lst9obij httpd_2435.2 httpd:2.4.37 host01 Running
3v4vikx98pgm \_ httpd_2435.2 httpd:2.4.35 host01 Shutdown
d58skm47doqf httpd_2435.3 httpd:2.4.35 host02 Running
root@host03:~# docker service ps httpd_2435 # 更新副本3完毕
ID NAME IMAGE NODE DESIRED ST PORTS
hkb5vb4ocqu6 httpd_2435.1 httpd:2.4.35 host02 Running
ys34lst9obij httpd_2435.2 httpd:2.4.37 host01 Running
3v4vikx98pgm \_ httpd_2435.2 httpd:2.4.35 host01 Shutdown
pujxt7yrum1d httpd_2435.3 httpd:2.4.37 host01 Running
d58skm47doqf \_ httpd_2435.3 httpd:2.4.35 host02 Shutdown
root@host03:~# docker service ps httpd_2435 # 更新副本1完毕
ID NAME IMAGE NODE DESIRED ST PORTS
hguoom4ihf8t httpd_2435.1 httpd:2.4.37 host02 Running
hkb5vb4ocqu6 \_ httpd_2435.1 httpd:2.4.35 host02 Shutdown
ys34lst9obij httpd_2435.2 httpd:2.4.37 host01 Running
3v4vikx98pgm \_ httpd_2435.2 httpd:2.4.35 host01 Shutdown
pujxt7yrum1d httpd_2435.3 httpd:2.4.37 host01 Running
d58skm47doqf \_ httpd_2435.3 httpd:2.4.35 host02 Shutdown
root@host03:~# docker service update --rollback httpd_2435 # 回滚到之前的版本
httpd_2435
rollback: manually requested rollback
overall progress: rolling back update: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
root@host03:~# docker service ps httpd_2435 # 回滚到之前的版本后,新开了一个 httpd:2.4.25,而不是使用之前的 httpd:2.4.35
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
py7dm0500o2z httpd_2435.1 httpd:2.4.35 host02 Running Running 19 seconds ago
hguoom4ihf8t \_ httpd_2435.1 httpd:2.4.37 host02 Shutdown Shutdown 20 seconds ago
hkb5vb4ocqu6 \_ httpd_2435.1 httpd:2.4.35 host02 Shutdown Shutdown about an hour ago
rdvhf3z4zh7r httpd_2435.2 httpd:2.4.35 host01 Running Running 17 seconds ago
ys34lst9obij \_ httpd_2435.2 httpd:2.4.37 host01 Shutdown Shutdown 18 seconds ago
3v4vikx98pgm \_ httpd_2435.2 httpd:2.4.35 host01 Shutdown Shutdown about an hour ago
xed1n7sy7td8 httpd_2435.3 httpd:2.4.35 host01 Running Running 22 seconds ago
pujxt7yrum1d \_ httpd_2435.3 httpd:2.4.37 host01 Shutdown Shutdown 23 seconds ago
d58skm47doqf \_ httpd_2435.3 httpd:2.4.35 host02 Shutdown Shutdown about an hour ago
默认配置下,Swarm一次只能更新一个副本,并且两个副本之间没有等待时间,我们可以通过 -- update-parallelism 设置并行更新的副本数目,通过--update-delay 指定滚动更新的时间间隔。
下面的例子中,我们有20个副本,更新并发数 4 ,更新延时 10s
root@host03:~# docker service update --image httpd:2.2 --update-parallelism 4 --update-delay 10s httpd_2435
ps:弹性伸缩时,即增减副本数并不受此限制,会以最快的速度完成伸缩