linux运维架构之路-K8s滚动更新及回滚
Posted Demon
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux运维架构之路-K8s滚动更新及回滚相关的知识,希望对你有一定的参考价值。
一、滚动更新
应用程序一次只更新一小部分副本,更新成功后,再更新更多的副本,最终完成所有副本的更新。 滚动更新的优点:零停机,整个更新过程始终有副本在运行,从而保证了业务的连续性。
1、创建三个副本Httpd服务,初始镜像为httpd:2.2.31,然后滚动更新至httpd:2.2.32
###cat httpd.yaml### apiVersion: apps/v1beta2 kind: Deployment metadata: name: httpd spec: replicas: 3 selector: matchLabels: app: httpd template: metadata: labels: app: httpd spec: containers: - name: httpd image: httpd:2.2.31 imagePullPolicy: IfNotPresent #默认值,本地有则使用本地镜像,不拉取 ports: - containerPort: 80
①创建
kubectl apply -f httpd.yaml
②查看
③修改deployment文件进行更新
应用
kubectl apply -f httpd.yaml --record
④再次查看更新后的httpd版本
⑤使用命令查看滚动更新详细
kubectl describe deployment httpd
结果:每次只更新替换一个pod副本
⑥使用命令替换镜像
kubectl set image deployments/httpd httpd=httpd:2.2.33
⑦验证如下:
[root@k8s-node1 rolling]# kubectl get deployment -o wide NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR httpd 3 4 1 3 42m httpd httpd:2.2.33 app=httpd mtomcat 1 1 1 1 4d mtomcat liujixiao/java-demo:latest app=mtomcat mynginx 1 1 1 1 9d mynginx nginx:latest name=mynginx mytomcat 1 1 1 1 9d mytomcat tomcat:8 app=mytomcat nginx 1 1 1 1 16d nginx nginx:latest name=nginx [root@k8s-node1 rolling]# kubectl get rs -o wide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR httpd-7584b484ff 3 3 3 11m httpd httpd:2.2.32 app=httpd,pod-template-hash=3140604099 httpd-cb5f5c5cb 1 1 0 38s httpd httpd:2.2.33 app=httpd,pod-template-hash=761917176 httpd-fbdc45d78 0 0 0 42m httpd httpd:2.2.31 app=httpd,pod-template-hash=968701834 mtomcat-7d6b5577b9 0 0 0 4d mtomcat tomcat:8 app=mtomcat,pod-template-hash=3826113365 mtomcat-b8d9c579 1 1 1 3d mtomcat liujixiao/java-demo:latest app=mtomcat,pod-template-hash=64857135 mynginx-7659f869f4 1 1 1 9d mynginx nginx:latest name=mynginx,pod-template-hash=3215942590 mytomcat-698fb6789 1 1 1 9d mytomcat tomcat:8 app=mytomcat,pod-template-hash=254962345 nginx-5bc649579b 1 1 1 5d nginx nginx:latest name=nginx,pod-template-hash=1672051356 nginx-fd7dc4fbc 0 0 0 16d nginx nginx:latest name=nginx,pod-template-hash=983870967
二、回滚
kubectl apply每次更新应用时,都会记录下当前的配置文件,保存为一个revision (版本),这样就可以通过这个版本回滚到特定的时间。默认配置下,K8s只会保留最近的几个revision,可以在Deployment配置文件中通过revisionHistoryLimit属性增加reviseion数量。
1、实践回滚功能
①创建三个应用配置文件httpd-v1.yaml、httpd-v2.yaml、httpd-v3.yaml
httpd-v1.yaml
apiVersion: apps/v1beta2 kind: Deployment metadata: name: httpd spec: revisionHistoryLimit: 10 replicas: 3 selector: matchLabels: app: httpd template: metadata: labels: app: httpd spec: containers: - name: httpd image: httpd:2.4.16 imagePullPolicy: IfNotPresent #默认值,本地有则使用本地镜像,不拉取 ports: - containerPort: 80
httpd-v2.yaml
apiVersion: apps/v1beta2 kind: Deployment metadata: name: httpd spec: revisionHistoryLimit: 10 replicas: 3 selector: matchLabels: app: httpd template: metadata: labels: app: httpd spec: containers: - name: httpd image: httpd:2.4.17 imagePullPolicy: IfNotPresent #默认值,本地有则使用本地镜像,不拉取 ports: - containerPort: 80
httpd-v3.yaml
apiVersion: apps/v1beta2 kind: Deployment metadata: name: httpd spec: revisionHistoryLimit: 10 replicas: 3 selector: matchLabels: app: httpd template: metadata: labels: app: httpd spec: containers: - name: httpd image: httpd:2.4.18 imagePullPolicy: IfNotPresent #默认值,本地有则使用本地镜像,不拉取 ports: - containerPort: 80
②通过kubectl apply部署并更新应用
kubectl apply -f httpd-v1.yaml --record deployment.apps "httpd" created [root@k8s-node1 rolling]# kubectl get deployment httpd -o wide NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR httpd 3 3 3 0 16s httpd httpd:2.4.16 app=httpd [root@k8s-node1 rolling]# kubectl apply -f httpd-v2.yaml --record deployment.apps "httpd" configured [root@k8s-node1 rolling]# [root@k8s-node1 rolling]# [root@k8s-node1 rolling]# kubectl get deployment httpd -o wide NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR httpd 3 4 1 0 27s httpd httpd:2.4.17 app=httpd [root@k8s-node1 rolling]# kubectl apply -f httpd-v3.yaml --record deployment.apps "httpd" configured [root@k8s-node1 rolling]# [root@k8s-node1 rolling]# [root@k8s-node1 rolling]# [root@k8s-node1 rolling]# kubectl get deployment httpd -o wide NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR httpd 3 4 1 2 36s httpd httpd:2.4.18 app=httpd
--record的作用是将当前命令记录到revision 记录中,这样我们就可以知道每个revison对应的是哪个配置文件,
通过kubectl rollout history deployment httpd查看历史记录,如下图:
[root@k8s-node1 rolling]# kubectl rollout history deployment httpd deployments "httpd" REVISION CHANGE-CAUSE 1 kubectl apply --filename=httpd-v1.yaml --record=true 2 kubectl apply --filename=httpd-v2.yaml --record=true 3 kubectl apply --filename=httpd-v3.yaml --record=true
③回滚到某个版本
查看当前版本:
[root@k8s-node1 rolling]# kubectl get deployment httpd -o wide NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR httpd 3 3 3 3 17m httpd httpd:2.4.18 app=httpd
回滚版本
kubectl rollout undo deployment httpd --to-revision=1
查看回滚后版本
[root@k8s-node1 rolling]# kubectl get deployment httpd -o wide NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR httpd 3 4 3 3 19m httpd httpd:2.4.16 app=httpd
此时再查看历史记录也会发生相应的变化:
[root@k8s-node1 rolling]# kubectl rollout history deployment httpd deployments "httpd" REVISION CHANGE-CAUSE 2 kubectl apply --filename=httpd-v2.yaml --record=true 3 kubectl apply --filename=httpd-v3.yaml --record=true 4 kubectl apply --filename=httpd-v1.yaml --record=true
CHANGE-CAUSE就是--record的结果,这里可以通过CHANGE-CAUSE知道每个revision的具体含义,所以执行kubect apply时加上--record参数。
以上是关于linux运维架构之路-K8s滚动更新及回滚的主要内容,如果未能解决你的问题,请参考以下文章