Deployment: 声明式地升级应用

使用Deployment资源升级pod

现在我们要考虑pod升级的情况了,如v1升级到v2和回滚等操作,这个场景需要改动下我们的服务。返回值里标记版本信息。
1
// cat app.js
2
const http = require('http');
3
const os = require('os');
4
5
console.log("Kubia server starting...");
6
7
var handler = function(request, response) {
8
console.log("Received request from " + request.connection.remoteAddress);
9
response.writeHead(200);
10
response.end("This is v1 running in pod " + os.hostname() + "\n");
11
};
12
13
var www = http.createServer(handler);
14
www.listen(8080);
Copied!
不同的版本服务已经做好了,可以直接部署使用,我们体验一下。
1
# cat kubia-deployment-v1.yaml
2
apiVersion: apps/v1beta1
3
kind: Deployment # 我们这里引入了Deployment
4
metadata:
5
name: kubia
6
spec:
7
replicas: 3
8
template:
9
metadata:
10
name: kubia
11
labels:
12
app: kubia
13
spec:
14
containers:
15
- image: luksa/kubia:v1
16
name: nodejs
Copied!
1
$ kubectl create -f kubia-deployment-v1.yaml --record
2
deployment.apps/kubia created
3
4
$ k get all
5
NAME READY STATUS RESTARTS AGE
6
pod/kubia-5dfcbbfcff-f5c5t 1/1 Running 0 80s
7
pod/kubia-5dfcbbfcff-nrrxq 1/1 Running 0 80s
8
pod/kubia-5dfcbbfcff-tthwm 1/1 Running 0 80s
9
10
NAME READY UP-TO-DATE AVAILABLE AGE
11
deployment.apps/kubia 3/3 3 3 80s
12
13
NAME DESIRED CURRENT READY AGE
14
replicaset.apps/kubia-5dfcbbfcff 3 3 3 81s
Copied!
观察下,Deployment自动帮我们创建了rs/pod这些。
1
$ kubectl get deployment kubia
2
NAME READY UP-TO-DATE AVAILABLE AGE
3
kubia 3/3 3 3 53s
4
5
$ kubectl describe deployment kubia
6
Name: kubia
7
Namespace: liuzongxian
8
... ...
9
... ...
10
11
#下面指令表示发版完成了 :)
12
$ kubectl rollout status deployment kubia 127 ↵
13
deployment "kubia" successfully rolled out
Copied!
Deployment 是我们常用的发版方式,且--record记录发版历史,这样我们能够回滚很便利
好像deployment没带来什么好处,下面看看升级、回滚

执行滚动升级

为了演示v1升级v2过程,我们做一下服务监控工作。这次连通服务service一起创建出来。
1
# cat kubia-deployment-and-service-v1.yaml 130 ↵
2
apiVersion: apps/v1beta1
3
kind: Deployment
4
metadata:
5
name: kubia
6
spec:
7
replicas: 3
8
template:
9
metadata:
10
name: kubia
11
labels:
12
app: kubia
13
spec:
14
containers:
15
- image: luksa/kubia:v1
16
name: nodejs
17
---
18
apiVersion: v1
19
kind: Service
20
metadata:
21
name: kubia
22
spec:
23
type: LoadBalancer
24
selector:
25
app: kubia
26
ports:
27
- port: 80
28
targetPort: 8080
Copied!
1
#清理掉前面的deployment
2
$ k delete deployment.apps/kubia
3
deployment.apps "kubia" deleted
Copied!
1
#使用带有服务的deployment部署
2
$ k create -f kubia-deployment-and-service-v1.yaml
3
deployment.apps/kubia created
4
service/kubia created
5
6
#设置服务滚动升级的速度,就是不要太快了
7
$ kubectl patch deployment kubia -p '{"spec": {"minReadySeconds": 10}}' 130 ↵
8
deployment.extensions/kubia patched
Copied!
服务部署好后,因为有负载均衡器地址,可以访问到服务
还记得获取LB的地址么,使用k get svc试试
1
#打开服务监控脚本,不停的访问服务,会显示版本号
2
while true; do curl http://<130.21.109.222替换自己的>; done
Copied!
如果LB申请不下来,可以使用busybox内网测试,见:
1
$ kubectl run busybox --rm -i --tty --image busybox -- sh
2
$ while true; do wget -O- http://<172.22.2.107替换自己的>; sleep 1; done
Copied!
执行升级操作,这里是将deployment中的image由v1改为了v2
1
$ kubectl set image deployment kubia nodejs=luksa/kubia:v2
2
deployment.extensions/kubia image updated
Copied!
我们观察到curl返回结果由全部是v1,开始陆续出现了v2,直至全都是v2
1
This is v1 running in pod kubia-5dfcbbfcff-b2445
2
This is v1 running in pod kubia-5dfcbbfcff-78m6h
3
This is v1 running in pod kubia-5dfcbbfcff-pvwdj
4
This is v2 running in pod kubia-7c699f58dd-b2fng
5
This is v2 running in pod kubia-7c699f58dd-b2fng
6
This is v1 running in pod kubia-5dfcbbfcff-b2445
7
This is v1 running in pod kubia-5dfcbbfcff-b2445
8
This is v2 running in pod kubia-7c699f58dd-b2fng
9
This is v1 running in pod kubia-5dfcbbfcff-pvwdj
Copied!
这样升级就完成了,一个指令就陆续的完成了部署,是不是很简单呢!

回滚pod到上个版本

我们准备了一个有错误的版本v3,它不能正常工作,在从v2到v3升级时看看发生什么
1
$ kubectl set image deployment kubia nodejs=luksa/kubia:v3
2
deployment.extensions/kubia image updated
Copied!
能看到错误的版本出现了
1
This is v2 running in pod kubia-7c699f58dd-b2fng
2
This is v2 running in pod kubia-7c699f58dd-c9wpf
3
This is v3 running in pod kubia-5c98f77977-n8bk4
4
This is v2 running in pod kubia-7c699f58dd-c9wpf
5
This is v2 running in pod kubia-7c699f58dd-np59f
6
This is v2 running in pod kubia-7c699f58dd-b2fng
7
Some internal error has occurred! This is pod kubia-5c98f77977-n8bk4
8
This is v2 running in pod kubia-7c699f58dd-c9wpf
9
This is v2 running in pod kubia-7c699f58dd-np59f
10
Some internal error has occurred! This is pod kubia-5c98f77977-n8bk4
11
This is v2 running in pod kubia-7c699f58dd-np59f
12
This is v2 running in pod kubia-7c699f58dd-c9wpf
13
This is v2 running in pod kubia-7c699f58dd-np59f
Copied!
此时我们做一下回滚操作
1
$ kubectl rollout undo deployment kubia
2
deployment.extensions/kubia rolled back
Copied!
然后看到服务回滚到v2了,恢复正常
1
This is v2 running in pod kubia-7c699f58dd-lxc4w
2
This is v2 running in pod kubia-7c699f58dd-6mq49
3
Some internal error has occurred! This is pod kubia-5c98f77977-n8bk4
4
This is v2 running in pod kubia-7c699f58dd-d96vg
5
This is v2 running in pod kubia-7c699f58dd-d96vg
6
This is v2 running in pod kubia-7c699f58dd-d96vg
7
This is v2 running in pod kubia-7c699f58dd-6mq49
Copied!
做到这里是不是觉得升级与回滚都特别便利了呢?我们看看它是怎么做到的
1
$ k get all
2
NAME READY STATUS RESTARTS AGE
3
pod/kubia-5c98f77977-hn48c 1/1 Running 0 8m17s
4
pod/kubia-5c98f77977-n8bk4 1/1 Running 0 8m33s
5
pod/kubia-5c98f77977-vdh69 1/1 Running 0 8m1s
6
7
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
8
service/kubia LoadBalancer 172.22.11.131 47.52.107.163 80:32456/TCP 32m
9
10
NAME READY UP-TO-DATE AVAILABLE AGE
11
deployment.apps/kubia 3/3 3 3 32m
12
13
NAME DESIRED CURRENT READY AGE
14
replicaset.apps/kubia-5c98f77977 3 3 3 8m35s
15
replicaset.apps/kubia-5dfcbbfcff 0 0 0 32m
16
replicaset.apps/kubia-7c699f58dd 0 0 0 28m
Copied!
还记得--record参数么,它帮我们记录了发版历史,从replicaset能看出来已经有了三个版本,所以在回滚时,它能知道我们回滚到哪个历史版本
deployment能够控制升级速度、出错暂停升级、回滚到指定版本等等,非常强大,大家可以多做做练习
我们回滚到最初的版本v1玩一下
1
$ kubectl rollout history deployment kubia
2
deployment.extensions/kubia
3
REVISION CHANGE-CAUSE
4
1 <none>
5
3 <none>
6
4 <none>
7
8
$ kubectl rollout undo deployment kubia --to-revision=1
9
deployment.extensions/kubia rolled back
Copied!
回到版本v1了
1
This is v1 running in pod kubia-5dfcbbfcff-z86v5
2
This is v1 running in pod kubia-5dfcbbfcff-pztrs
3
This is v2 running in pod kubia-7c699f58dd-lxc4w
4
This is v1 running in pod kubia-5dfcbbfcff-qcvjx
5
This is v1 running in pod kubia-5dfcbbfcff-pztrs
6
This is v1 running in pod kubia-5dfcbbfcff-qcvjx
7
This is v1 running in pod kubia-5dfcbbfcff-z86v5
8
This is v1 running in pod kubia-5dfcbbfcff-pztrs
9
This is v1 running in pod kubia-5dfcbbfcff-qcvjx
Copied!
So!Deployment是管理发版的利器。做完这里,你已经完成了绝大部分的容器管理内容了!
最后,在做下一节前,注意清理资源

思考题

  • 记录发版历史有个重要参数,是什么?怎么从v2回滚到v1版本呢?