一、滚动更新
应用程序一次只更新一小部分副本,更新成功后,再更新更多的副本,最终完成所有副本的更新。 滚动更新的优点:零停机,整个更新过程始终有副本在运行,从而保证了业务的连续性。
1、创建三个副本Httpd服务,初始镜像为httpd:2.2.31,然后滚动更新至httpd:2.2.32
###cat httpd.yaml###
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: httpd
spec:
replicas:
selector:
matchLabels:
app: httpd
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd:2.2.
imagePullPolicy: IfNotPresent #默认值,本地有则使用本地镜像,不拉取
ports:
- containerPort:
①创建
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.
⑦验证如下:
[root@k8s-node1 rolling]# kubectl get deployment -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 42m httpd httpd:2.2. app=httpd
mtomcat 4d mtomcat liujixiao/java-demo:latest app=mtomcat
mynginx 9d mynginx nginx:latest name=mynginx
mytomcat 9d mytomcat tomcat: app=mytomcat
nginx 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 11m httpd httpd:2.2. app=httpd,pod-template-hash=
httpd-cb5f5c5cb 38s httpd httpd:2.2. app=httpd,pod-template-hash=
httpd-fbdc45d78 42m httpd httpd:2.2. app=httpd,pod-template-hash=
mtomcat-7d6b5577b9 4d mtomcat tomcat: app=mtomcat,pod-template-hash=
mtomcat-b8d9c579 3d mtomcat liujixiao/java-demo:latest app=mtomcat,pod-template-hash=
mynginx-7659f869f4 9d mynginx nginx:latest name=mynginx,pod-template-hash=
mytomcat-698fb6789 9d mytomcat tomcat: app=mytomcat,pod-template-hash=
nginx-5bc649579b 5d nginx nginx:latest name=nginx,pod-template-hash=
nginx-fd7dc4fbc 16d nginx nginx:latest name=nginx,pod-template-hash=
二、回滚
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:
replicas:
selector:
matchLabels:
app: httpd
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd:2.4.
imagePullPolicy: IfNotPresent #默认值,本地有则使用本地镜像,不拉取
ports:
- containerPort:
httpd-v2.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: httpd
spec:
revisionHistoryLimit:
replicas:
selector:
matchLabels:
app: httpd
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd:2.4.
imagePullPolicy: IfNotPresent #默认值,本地有则使用本地镜像,不拉取
ports:
- containerPort:
httpd-v3.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: httpd
spec:
revisionHistoryLimit:
replicas:
selector:
matchLabels:
app: httpd
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd:2.4.
imagePullPolicy: IfNotPresent #默认值,本地有则使用本地镜像,不拉取
ports:
- containerPort:
②通过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 16s httpd httpd:2.4. 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 27s httpd httpd:2.4. 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 36s httpd httpd:2.4. app=httpd
--record的作用是将当前命令记录到revision 记录中,这样我们就可以知道每个revison对应的是哪个配置文件,
通过kubectl rollout history deployment httpd查看历史记录,如下图:
[root@k8s-node1 rolling]# kubectl rollout history deployment httpd
deployments "httpd"
REVISION CHANGE-CAUSE
kubectl apply --filename=httpd-v1.yaml --record=true
kubectl apply --filename=httpd-v2.yaml --record=true
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 17m httpd httpd:2.4. app=httpd
回滚版本
kubectl rollout undo deployment httpd --to-revision=
查看回滚后版本
[root@k8s-node1 rolling]# kubectl get deployment httpd -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 19m httpd httpd:2.4. app=httpd
此时再查看历史记录也会发生相应的变化:
[root@k8s-node1 rolling]# kubectl rollout history deployment httpd
deployments "httpd"
REVISION CHANGE-CAUSE
kubectl apply --filename=httpd-v2.yaml --record=true
kubectl apply --filename=httpd-v3.yaml --record=true
kubectl apply --filename=httpd-v1.yaml --record=true
CHANGE-CAUSE就是--record的结果,这里可以通过CHANGE-CAUSE知道每个revision的具体含义,所以执行kubect apply时加上--record参数。