Kubernetes服务的滚动升级
2017-01-17
当需要升级集群中的某个服务时,需要停止该服务相关的Pod,pull最新的docker镜像,然后重新启动。 如果一下把所有Pod都停止,服务在升级的时间内会不可用。因此,Kubernetes提供了滚动升级的功能。
kubectl rolling-update命令滚动升级 #
集群中有下面的RC:
tomcat-rc.yaml
1apiVersion: v1
2kind: ReplicationController
3metadata:
4 name: tomcat
5spec:
6 replicas: 5
7 template:
8 metadata:
9 labels:
10 app: tomcat
11 spec:
12 containers:
13 - name: tomcat
14 image: tomcat:7
15 ports:
16 - containerPort: 8080
1kubectl get pod -o wide | grep tomcat
2tomcat-0f1bc 1/1 Running 0 7s 10.244.3.15 cent1
3tomcat-4l3s3 1/1 Running 0 7s 10.244.2.12 cent2
4tomcat-6zw8s 1/1 Running 0 7s 10.244.3.14 cent1
5tomcat-9mmfg 1/1 Running 0 7s 10.244.2.11 cent2
6tomcat-q728k 1/1 Running 0 7s 10.244.3.13 cent1
现在需要升级tomcat这个RC下的Pod,这里的需求只是将tomcat镜像从7升级到8:
1kubectl rolling-update tomcat --image=tomcat:8
2Created tomcat-d88a9205146d65acff8e0543aa3b6b62
3Scaling up tomcat-d88a9205146d65acff8e0543aa3b6b62 from 0 to 5, scaling down tomcat from 5 to 0 (keep 5 pods available, don't exceed 6 pods)
4Scaling tomcat-d88a9205146d65acff8e0543aa3b6b62 up to 1
5Scaling tomcat down to 4
6Scaling tomcat-d88a9205146d65acff8e0543aa3b6b62 up to 2
7Scaling tomcat down to 3
8Scaling tomcat-d88a9205146d65acff8e0543aa3b6b62 up to 3
9Scaling tomcat down to 2
10Scaling tomcat-d88a9205146d65acff8e0543aa3b6b62 up to 4
11Scaling tomcat down to 1
12Scaling tomcat-d88a9205146d65acff8e0543aa3b6b62 up to 5
13Scaling tomcat down to 0
14Update succeeded. Deleting old controller: tomcat
15Renaming tomcat to tomcat-d88a9205146d65acff8e0543aa3b6b62
16replicationcontroller "tomcat" rolling updated
从上面命令的输出可以看出,升级过程是先创建一个新版的Pod,然后再停止一个旧版本的Pod,逐步完成整个RC的更新。 升级完成后,查看rc:
1kubectl get rc tomcat -o wide
2NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
3tomcat 5 5 5 5m tomcat tomcat:8 app=tomcat,deployment=d88a9205146d65acff8e0543aa3b6b62
可以看到RC增加了一个deployment=88a9205146d65acff8e0543aa3b6b62的Label,是由RC的内容HASH计算得到的,作为签名用于表示RC是否发生变化。
如果滚动升级过程中发现问题,可以回滚:
1kubectl rolling-update tomcat --image=tomcat:7 --rollback