vlambda博客
学习文章列表

k8s容器之deployment控制器镜像更新

点击上方“IT那活儿”,关注后了解更多内容,不管IT什么活儿,干就完了!!!



Deployment
  • 支持ReplicaSet的所有功能;
  • 支持发布的停止、继续;
  • 支持滚动升级和回滚版本。



创建deployment
创建pc-deployment.yaml,内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
 name: pc-deployment
 namespace: dev
spec:
 replicas: 3
 selector:
   matchLabels:
     app: nginx-pod
 template:
   metadata:
     labels:
       app: nginx-pod
   spec:
     containers:
     - name: nginx
       image: nginx:1.17.1
yaml
# 创建deployment:
[root@k8s-master01 ~]# kubectl create -f pc-deployment.yaml --record=true
deployment.apps/pc-deployment created
# 查看deployment;
# UP-TO-DATE 最新版本的pod的数量;
# AVAILABLE  当前可用的pod的数量。
[root@k8s-master01 ~]# kubectl get deploy pc-deployment -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
pc-deployment 3/3     3            3           15s
# 查看rs;
# 发现rs的名称是在原来deployment的名字后面添加了一个10位数的随机串。
[root@k8s-master01 ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-6696798b78 3         3         3       23s
# 查看pod:
[root@k8s-master01 ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-6696798b78-d2c8n 1/1     Running 0          107s
pc-deployment-6696798b78-smpvp 1/1     Running 0          107s
pc-deployment-6696798b78-wvjd8 1/1     Running 0          107s




扩缩容
# 变更副本数量为5个:
[root@k8s-master01 ~]# kubectl scale deploy pc-deployment --replicas=5  -n dev
deployment.apps/pc-deployment scaled
# 查看deployment:
[root@k8s-master01 ~]# kubectl get deploy pc-deployment -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
pc-deployment 5/5     5            5           2m
# 查看pod:
[root@k8s-master01 ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-6696798b78-d2c8n 1/1     Running 0          4m19s
pc-deployment-6696798b78-jxmdq 1/1     Running 0          94s
pc-deployment-6696798b78-mktqv 1/1     Running 0          93s
pc-deployment-6696798b78-smpvp 1/1     Running 0          4m19s
pc-deployment-6696798b78-wvjd8 1/1     Running 0          4m19s
# 编辑deployment的副本数量,修改spec:replicas: 4即可。
[root@k8s-master01 ~]# kubectl edit deploy pc-deployment -n dev
deployment.apps/pc-deployment edited
# 查看pod:
[root@k8s-master01 ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-6696798b78-d2c8n 1/1     Running 0          5m23s
pc-deployment-6696798b78-jxmdq 1/1     Running 0          2m38s
pc-deployment-6696798b78-smpvp 1/1     Running 0          5m23s
pc-deployment-6696798b78-wvjd8 1/1     Running 0          5m23s




镜像更新
deployment支持两种更新策略:重建更新和滚动更新,可以通过strategy指定策略类型,支持两个属性:
1. 重建更新
编辑pc-deployment.yaml,在spec节点下添加更新策略:
apiVersion: apps/v1
kind: Deployment
metadata:
 name: pc-deployment
 namespace: dev
spec:
 strategy: # 策略
   type: Recreate # 重建更新
 replicas: 3
 selector:
   matchLabels:
     app: nginx-pod
 template:
   metadata:
     labels:
       app: nginx-pod
   spec:
     containers:
     - name: nginx
       image: nginx:1.17.1
yaml
# 变更镜像:
[root@k8s-master01 ~]# kubectl set image deployment pc-deployment nginx=nginx:1.17.2 -n dev
deployment.apps/pc-deployment image updated
# 观察升级过程:
[root@k8s-master01 ~]# kubectl get pods -n dev -w
NAME READY STATUS RESTARTS AGE
pc-deployment-5d89bdfbf9-65qcw 1/1     Running 0          31s
pc-deployment-5d89bdfbf9-w5nzv 1/1     Running 0          31s
pc-deployment-5d89bdfbf9-xpt7w 1/1     Running 0          31s

pc-deployment-5d89bdfbf9-xpt7w 1/1     Terminating 0          41s
pc-deployment-5d89bdfbf9-65qcw 1/1     Terminating 0          41s
pc-deployment-5d89bdfbf9-w5nzv 1/1     Terminating 0          41s

pc-deployment-675d469f8b-grn8z 0/1     Pending 0          0s
pc-deployment-675d469f8b-hbl4v 0/1     Pending 0          0s
pc-deployment-675d469f8b-67nz2 0/1     Pending 0          0s

pc-deployment-675d469f8b-grn8z 0/1     ContainerCreating 0          0s
pc-deployment-675d469f8b-hbl4v 0/1     ContainerCreating 0          0s
pc-deployment-675d469f8b-67nz2 0/1     ContainerCreating 0          0s

pc-deployment-675d469f8b-grn8z 1/1     Running 0          1s
pc-deployment-675d469f8b-67nz2 1/1     Running 0          1s
pc-deployment-675d469f8b-hbl4v 1/1     Running 0          2s


2. 滚动更新
2.1 编辑pc-deployment.yaml,在spec节点下添加更新策略
apiVersion: apps/v1
kind: Deployment
metadata:
 name: pc-deployment
 namespace: dev
spec:
 strategy: # 策略
   type: RollingUpdate # 滚动更新策略
   rollingUpdate:
     maxSurge: 25% 
     maxUnavailable: 25%
 replicas: 3
 selector:
   matchLabels:
     app: nginx-pod
 template:
   metadata:
     labels:
       app: nginx-pod
   spec:
     containers:
     - name: nginx
       image: nginx:1.17.2
yaml
2.2 创建deploy进行验证
# 变更镜像:
[root@k8s-master01 ~]# kubectl set image deployment pc-deployment nginx=nginx:1.17.3 -n dev 
deployment.apps/pc-deployment image updated
# 观察升级过程:
[root@k8s-master01 ~]# kubectl get pods -n dev -w
NAME READY STATUS RESTARTS AGE
pc-deployment-c848d767-8rbzt 1/1     Running 0          31m
pc-deployment-c848d767-h4p68 1/1     Running 0          31m
pc-deployment-c848d767-hlmz4 1/1     Running 0          31m
pc-deployment-c848d767-rrqcn 1/1     Running 0          31m

pc-deployment-966bf7f44-226rx 0/1     Pending 0          0s
pc-deployment-966bf7f44-226rx 0/1     ContainerCreating 0          0s
pc-deployment-966bf7f44-226rx 1/1     Running 0          1s
pc-deployment-c848d767-h4p68 0/1     Terminating 0          34m

pc-deployment-966bf7f44-cnd44 0/1     Pending 0          0s
pc-deployment-966bf7f44-cnd44 0/1     ContainerCreating 0          0s
pc-deployment-966bf7f44-cnd44 1/1     Running 0          2s
pc-deployment-c848d767-hlmz4 0/1     Terminating 0          34m

pc-deployment-966bf7f44-px48p 0/1     Pending 0          0s
pc-deployment-966bf7f44-px48p 0/1     ContainerCreating 0          0s
pc-deployment-966bf7f44-px48p 1/1     Running 0          0s
pc-deployment-c848d767-8rbzt 0/1     Terminating 0          34m

pc-deployment-966bf7f44-dkmqp 0/1     Pending 0          0s
pc-deployment-966bf7f44-dkmqp 0/1     ContainerCreating 0          0s
pc-deployment-966bf7f44-dkmqp 1/1     Running 0          2s
pc-deployment-c848d767-rrqcn 0/1     Terminating 0          34m
# 至此,新版本的pod创建完毕,就版本的pod销毁完毕。
# 中间过程是滚动进行的,也就是边销毁边创建。
2.3 镜像更新中rs的变化
# 查看rs,发现原来的rs的依旧存在,只是pod数量变为了0,而后又新产生了一个rs,pod数量为4:
[root@k8s-master01 ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-6696798b78 0         0         0       7m37s
pc-deployment-6696798b11 0         0         0       5m37s
pc-deployment-c848d76789 4         4         4       72s




版本回退
deployment支持版本升级过程中的暂停、继续功能以及版本回退等诸多功能。
kubectl rollout:版本升级相关功能,支持下面的选项:
  • status 显示当前升级状态;
  • history 显示 升级历史记录;
  • pause 暂停版本升级过程;
  • resume 继续已经暂停的版本升级过程;
  • restart 重启版本升级过程;
  • undo 回滚到上一级版本(可以使用--to-revision回滚到指定版本)。
# 重新创建deploy,--record  记录deployment更新。
[root@master ~]# kubectl create -f pc-deployment.yaml --record
# 查看当前升级版本的状态:
[root@k8s-master01 ~]# kubectl rollout status deploy pc-deployment -n dev
deployment "pc-deployment" successfully rolled out
# 查看升级历史记录:
[root@k8s-master01 ~]# kubectl rollout history deploy pc-deployment -n dev
deployment.apps/pc-deployment
REVISION CHANGE-CAUSE
1         kubectl create --filename=pc-deployment.yaml --record=true
2         kubectl create --filename=pc-deployment.yaml --record=true
3         kubectl create --filename=pc-deployment.yaml --record=true
# 可以发现有三次版本记录,说明完成过两次升级。
# 版本回滚。
# 这里直接使用--to-revision=1回滚到了1版本, 如果省略这个选项,就是回退到上个版本,就是2版本。
[root@k8s-master01 ~]# kubectl rollout undo deployment pc-deployment --to-revision=1 -n dev
deployment.apps/pc-deployment rolled back
# 查看发现,通过nginx镜像版本可以发现到了第一版。
[root@k8s-master01 ~]# kubectl get deploy -n dev -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES
pc-deployment 4/4     4            4           74m   nginx nginx:1.17.1
# 查看rs,发现第一个rs中有4个pod运行,后面两个版本的rs中pod为运行。
# 其实deployment之所以可是实现版本的回滚,就是通过记录下历史rs来实现的。
# 一旦想回滚到哪个版本,只需要将当前版本pod数量降为0,然后将回滚版本的pod提升为目标数量就可以了。
[root@k8s-master01 ~]# kubectl get rs -n dev
NAME DESIRED CURRENT READY AGE
pc-deployment-6696798b78 4         4         4       78m
pc-deployment-966bf7f44 0         0         0       37m
pc-deployment-c848d767 0         0         0       71m




金丝雀发布(灰度发布)
Deployment控制器支持控制更新过程中的控制,如“暂停(pause)”或“继续(resume)”更新操作。
# 更新deployment的版本,并配置暂停deployment:
[root@k8s-master01 ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.4 -n dev && kubectl rollout pause deployment pc-deployment -n dev
deployment.apps/pc-deployment image updated
deployment.apps/pc-deployment paused
#观察更新状态:
[root@k8s-master01 ~]# kubectl rollout status deploy pc-deployment -n dev 
Waiting for deployment "pc-deployment" rollout to finish: 2 out of 4 new replicas have been updated...
# 监控更新的过程,可以看到已经新增了一个资源,但是并未按照预期的状态去删除一个旧的资源,就是因为使用了pause暂停命令。
[root@k8s-master01 ~]# kubectl get rs -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES
pc-deployment-5d89bdfbf9 3         3         3       19m     nginx nginx:1.17.1   
pc-deployment-675d469f8b 0         0         0       14m     nginx nginx:1.17.2   
pc-deployment-6c9f56fcfb 2         2         2       3m16s nginx nginx:1.17.4   
[root@k8s-master01 ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-5d89bdfbf9-rj8sq 1/1     Running 0          7m33s
pc-deployment-5d89bdfbf9-ttwgg 1/1     Running 0          7m35s
pc-deployment-5d89bdfbf9-v4wvc 1/1     Running 0          7m34s
pc-deployment-6c9f56fcfb-996rt 1/1     Running 0          3m31s
pc-deployment-6c9f56fcfb-j2gtj 1/1     Running 0          3m31s
# 确保更新的pod没问题了,继续更新:
[root@k8s-master01 ~]# kubectl rollout resume deploy pc-deployment -n dev
deployment.apps/pc-deployment resumed
# 查看最后的更新情况:
[root@k8s-master01 ~]# kubectl get rs -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES
pc-deployment-5d89bdfbf9 0         0         0       21m     nginx nginx:1.17.1   
pc-deployment-675d469f8b 0         0         0       16m     nginx nginx:1.17.2   
pc-deployment-6c9f56fcfb 4         4         4       5m11s nginx nginx:1.17.4   

[root@k8s-master01 ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-deployment-6c9f56fcfb-7bfwh 1/1     Running 0          37s
pc-deployment-6c9f56fcfb-996rt 1/1     Running 0          5m27s
pc-deployment-6c9f56fcfb-j2gtj 1/1     Running 0          5m27s
pc-deployment-6c9f56fcfb-rf84v 1/1     Running 0          37s






本文作者:刘川陵

本文来源:IT那活儿(上海新炬王翦团队)