Top

K8S-容器升级和回滚

Pod容器的升级和回滚

我的环境是Centos7.4!3台服务器:Master、Minion(node) –(Kubernetes v1.5.2)

192.168.5.10-server-1|192.168.5.11-server-2
↓简述
1.高并发、高可用系统普及的今天,服务的升级更新至少要做到“业务不中断”.而滚动更新(Rolling-update)恰是满足这一需求的一种系统更新升级方案. 2.滚动更新就是针对多实例服务的一种不中断服务的更新升级方式.一般情况,对于多实例服务,滚动更新采用对各个实例逐个进行单独更新而非同一时刻对所有实例进行全部更新的方式. 3.对于Kubernetes集群部署的Service来说,Rolling update就是指一次仅更新一个Pod,然后逐个进行更新,而不是在同一时刻将该Service下面的所有Pod shutdown,然后去更新,逐个更新可以避免将业务中断.
↓原理
通过kubectl get deployment hainan-nginx-group -o yaml可以查看到以下两个参数,通过它们来精确地控制着每次滚动的pod数量. 1.maxSurge 滚动更新过程中运行操作期望副本数的最大pod数,可以为绝对数值(eg:5),但不能为0,也可以为百分数(eg:10%),默认为25%. 2.maxUnavailable 滚动更新过程中不可用的最大pod数,可以为绝对数值(eg:5),但不能为0,也可以为百分数(eg:10%),默认为25%.

↓原文来自

K8S中文社区:http://docs.kubernetes.org.cn/643.html

1. 容器创建

↓创建3个容器.
无法显示
↓访问一下.
[root@server-2 ~]# curl 192.168.5.11:30921
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

2. 容器升级

↓nginx:v2为更新的镜像名(仓库中需要有镜像).
[root@server-1 ~]# kubectl set image deployments/hainan-nginx-group hainan-nginx-group=nginx:v2 -n default deployment "hainan-nginx-group" image updated

2.1 查看状态

↓查看更新进度(desired=期望处于READY的副本数、cureent=当前的副本总数、up-to-date=当前完成更新的副本数、available=当前可用的副本数).
[root@server-1 docker]# kubectl get deployments -n default NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE hainan-nginx-group 3 3 3 2 20m [root@server-1 docker]# kubectl get deployments -n default NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE hainan-nginx-group 3 3 3 3 20m
↓查看部署状态.
[root@server-1 docker]# kubectl rollout status deployments/hainan-nginx-group -n default deployment "hainan-nginx-group" successfully rolled out

2.2 测试

↓更新镜像成功.
无法显示
↓访问成功.
[root@server-2 ~]# curl 192.168.5.11:30921 nginx-v2

3. 容器回滚

↓回滚到上一次的副本集.
[root@server-1 ~]# kubectl rollout undo deployments/hainan-nginx-group -n default deployment "hainan-nginx-group" rolled back

3.1 测试

[root@server-2 ~]# curl 192.168.5.11:30921
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

4. 指定回滚

↓查看已经部署的版本(就是副本集).
[root@server-1 docker]# kubectl rollout history deploy/hainan-nginx-group deployments "hainan-nginx-group" REVISION CHANGE-CAUSE 2 none> 3 none>
↓查看指定的版本信息.
[root@server-1 ~]# kubectl rollout history deploy/hainan-nginx-group --revision=3
1
2
3
4
5
6
deployments "hainan-nginx-group" with revision #3
Labels: app=hainan-nginx-group
pod-template-hash=2522410565
Containers:
hainan-nginx-group:
Image: nginx:latest
[root@server-1 ~]# kubectl rollout history deploy/hainan-nginx-group --revision=2
1
2
3
4
5
6
deployments "hainan-nginx-group" with revision #2
Labels: app=hainan-nginx-group
pod-template-hash=2330782816
Containers:
hainan-nginx-group:
Image: nginx:v2
↓指定回滚到某一版本.
[root@server-1 ~]# kubectl rollout undo deploy/hainan-nginx-group --to-revision=2 -n default deployment "hainan-nginx-group" rolled back

4.1 测试

[root@server-2 ~]# curl 192.168.5.11:30921
nginx-v2

5. 总结

1
2
3
4
5
6
7
容器升级       	     kubectl set image deployments/hainan-nginx-group hainan-nginx-group=nginx:v2 -n default
容器升级进度 kubectl get deploy/hainan-nginx-group -n default
容器升级是否成功 kubectl rollout status deployments/hainan-nginx-group -n default
容器回滚 kubectl rollout undo deployments/hainan-nginx-group -n default
查看已经部署过的版本 kubectl rollout history deploy/hainan-nginx-group
查看指定副本版本的信息 kubectl rollout history deploy/hainan-nginx-group --revision=3
容器回滚到指定的版本 kubectl rollout undo deploy/hainan-nginx-group --to-revision=2 -n default