k8s-pod版本更新

pod版本更新⭐️⭐️

在实际应用中,升级是一个常见的场景,Deployment能够很方便的支撑应用升级。

Deployment可以设置不同的升级策略,有如下两种。

RollingUpdate:滚动升级,即逐步创建新Pod再删除旧Pod,为默认策略。
Recreate:替换升级,即先把当前Pod删掉再重新创建Pod。
Deployment的升级可以是声明式的,也就是说只需要修改Deployment的YAML定义即可,

比如使用kubectl edit命令将上面Deployment中的镜像修改为nginx:alpine

修改完成后再查询ReplicaSet和Pod,发现创建了一个新的ReplicaSet,Pod也重新创建了。

# 注意,是修改Deployment控制器,实现对pod的更新

[root@k8s-master-10 ~]#kubectl edit deployments.apps nginx-deployment 

     34     spec:
     35       containers:
     36       - image: nginx:latest

deployment和RS和pod关系

image

查看deployment更新事件

[root@k8s-master-10 ~]#kubectl -n yuchaoit describe deployments.apps nginx-deployment 

# 创建了新的RS控制器
NewReplicaSet:   nginx-deployment-5b78bc88d4 (2/2 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  34m    deployment-controller  Scaled up replica set nginx-deployment-64bcc7b77f to 2
  Normal  ScalingReplicaSet  4m53s  deployment-controller  Scaled up replica set nginx-deployment-5b78bc88d4 to 1
  Normal  ScalingReplicaSet  4m32s  deployment-controller  Scaled down replica set nginx-deployment-64bcc7b77f to 1
  Normal  ScalingReplicaSet  4m32s  deployment-controller  Scaled up replica set nginx-deployment-5b78bc88d4 to 2
  Normal  ScalingReplicaSet  4m12s  deployment-controller  Scaled down replica set nginx-deployment-64bcc7b77f to 0

查看rs更新事件

# 旧的RS,已经不负责pod管理
[root@k8s-master-10 ~]#kubectl -n yuchaoit describe rs nginx-deployment-64bcc7b77f 

# 新的RS,创建新的pod
[root@k8s-master-10 ~]#kubectl -n yuchaoit describe rs nginx-deployment-5b78bc88d4

查看pod更新事件

此时的pod,也是基于新的RS控制器创建了

[root@k8s-master-10 ~]#kubectl -n yuchaoit describe pod nginx-deployment-5b78bc88d4-

# pod和RS控制器关系
Controlled By:  ReplicaSet/nginx-deployment-5b78bc88d4

deployment更新镜像原理图

image

默认是滚动更新,逐步创建新pod、然后删除旧pod.

# 命令
[root@k8s-master-10 ~]#kubectl -n yuchaoit describe deployments.apps nginx-deployment 

# 滚动更新事件
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  42m   deployment-controller  Scaled up replica set nginx-deployment-64bcc7b77f to 2
  Normal  ScalingReplicaSet  12m   deployment-controller  Scaled up replica set nginx-deployment-5b78bc88d4 to 1
  Normal  ScalingReplicaSet  12m   deployment-controller  Scaled down replica set nginx-deployment-64bcc7b77f to 1
  Normal  ScalingReplicaSet  12m   deployment-controller  Scaled up replica set nginx-deployment-5b78bc88d4 to 2
  Normal  ScalingReplicaSet  11m   deployment-controller  Scaled down replica set nginx-deployment-64bcc7b77f to 0

查看滚动更新状态

[root@k8s-master-10 ~]# kubectl -n yuchaoit  rollout status deployment nginx-deployment 
deployment "nginx-deployment" successfully rolled out

蓝绿更新原理

image

其他更新镜像命令

1.直接修改yaml文件,修改镜像(镜像就是容器运行的交付标准)

# 语法
# 结尾跟上容器名,修改镜像即可
[root@k8s-master-10 ~]#kubectl set image -f deployment-nginx.yml nginx-containers=nginx:1.21.5
deployment.apps/nginx-deployment image updated
[root@k8s-master-10 ~]#


2.还有可以直接选择控制器,进行镜像修改
[root@k8s-master-10 ~]#kubectl -n yuchaoit set image deployment nginx-deployment nginx-containers=nginx:1.15.0
deployment.apps/nginx-deployment image updated

pod回滚上一个版本

回滚也称为回退,即当发现升级出现问题时,让应用回到老的版本。

Deployment可以非常方便的回滚到老版本。

例如上面升级的新版镜像有问题,可以执行kubectl rollout undo命令进行回滚。

Deployment之所以能如此容易的做到回滚,是因为Deployment是通过ReplicaSet控制Pod的

升级后之前ReplicaSet都一直存在,Deployment回滚做的就是使用之前的ReplicaSet再次把Pod创建出来。

Deployment中保存ReplicaSet的数量可以使用revisionHistoryLimit参数限制,默认值为10。
[root@k8s-master-10 ~]#kubectl -n yuchaoit describe deployments.apps nginx-deployment 


[root@k8s-master-10 ~]#kubectl -n yuchaoit rollout undo deployment nginx-deployment 
deployment.apps/nginx-deployment rolled back

# 查看回滚上一个控制器的版本
[root@k8s-master-10 ~]#kubectl -n yuchaoit describe deployments.apps nginx-deployment 

# 最新pod信息
[root@k8s-master-10 ~]#kubectl -n yuchaoit get po -owide

# 查看RS,很明显,回到上一个版本了
[root@k8s-master-10 ~]#kubectl -n yuchaoit get rs -owide
NAME                          DESIRED   CURRENT   READY   AGE     CONTAINERS         IMAGES         SELECTOR
nginx-deployment-5b48487b68   0         0         0       6m22s   nginx-containers   nginx:1.21.5   app=nginx,pod-template-hash=5b48487b68
nginx-deployment-5b78bc88d4   2         2         2       45m     nginx-containers   nginx:latest   app=nginx,pod-template-hash=5b78bc88d4
nginx-deployment-64bcc7b77f   0         0         0       74m     nginx-containers   nginx:1.14.0   app=nginx,pod-template-hash=64bcc7b77f
[root@k8s-master-10 ~]#

pod指定版本记录回滚⭐️⭐️

全流程⭐️

# 清理旧环境,删除Deployment,也会删除对应的RS控制器记录
[root@k8s-master-10 ~]#kubectl delete -f deployment-nginx.yml 
deployment.apps "nginx-deployment" deleted


# 修改镜像版本
[root@k8s-master-10 ~]#grep image deployment-nginx.yml 
      - image: nginx:1.14.0
        imagePullPolicy: IfNotPresent


# 创建第一版pod,记录版本,添加参数 --record
[root@k8s-master-10 ~]#kubectl create -f deployment-nginx.yml --record
deployment.apps/nginx-deployment created

# 测试版本
[root@k8s-master-10 ~]#curl -s  10.2.2.32 -I |grep Server
Server: nginx/1.14.0



# 更新第二版 1.15.0
[root@k8s-master-10 ~]#kubectl -n yuchaoit set image deployment nginx-deployment nginx-containers=nginx:1.15.0
deployment.apps/nginx-deployment image updated

# 验证
[root@k8s-master-10 ~]#curl -s  10.2.1.24 -I |grep Server
Server: nginx/1.15.0


# 更新第三版1.16.0
kubectl -n yuchaoit set image deployment nginx-deployment nginx-containers=nginx:1.16.0

# 验证
[root@k8s-master-10 ~]#curl 10.2.1.25 -s -I |grep Server
Server: nginx/1.16.0

# 查看控制器所有的历史版本
[root@k8s-master-10 ~]#kubectl rollout history deployment nginx-deployment -n yuchaoit 
deployment.apps/nginx-deployment 
REVISION  CHANGE-CAUSE
1         kubectl create --filename=deployment-nginx.yml --record=true
2         kubectl create --filename=deployment-nginx.yml --record=true
3         kubectl create --filename=deployment-nginx.yml --record=true


# 查看指定历史版本的信息
[root@k8s-master-10 ~]#kubectl rollout history deployment nginx-deployment -n yuchaoit --revision=1



# 回到指定版本
[root@k8s-master-10 ~]#kubectl rollout undo deployment nginx-deployment -n yuchaoit --to-revision=1
deployment.apps/nginx-deployment rolled back
[root@k8s-master-10 ~]#kubectl -n yuchaoit get po -owide
NAME                                READY   STATUS    RESTARTS   AGE   IP          NODE          NOMINATED NODE   READINESS GATES
nginx-deployment-64bcc7b77f-6xb95   1/1     Running   0          5s    10.2.1.26   k8s-node-11   <none>           <none>
nginx-deployment-64bcc7b77f-jxwzd   1/1     Running   0          5s    10.2.2.35   k8s-node-12   <none>           <none>
[root@k8s-master-10 ~]#curl -s -I 10.2.2.35 |grep Server
Server: nginx/1.14.0
[root@k8s-master-10 ~]#

扩缩容deployment

[root@k8s-master-10 ~]#kubectl -n yuchaoit scale deployment nginx-deployment --replicas=5
deployment.apps/nginx-deployment scaled
[root@k8s-master-10 ~]#kubectl -n yuchaoit get po -owide -w
NAME                                READY   STATUS    RESTARTS   AGE   IP          NODE          NOMINATED NODE   READINESS GATES
nginx-deployment-64bcc7b77f-6xb95   1/1     Running   0          72s   10.2.1.26   k8s-node-11   <none>           <none>
nginx-deployment-64bcc7b77f-fgbdq   1/1     Running   0          3s    10.2.1.27   k8s-node-11   <none>           <none>
nginx-deployment-64bcc7b77f-jbf9d   1/1     Running   0          3s    10.2.2.36   k8s-node-12   <none>           <none>
nginx-deployment-64bcc7b77f-jxwzd   1/1     Running   0          72s   10.2.2.35   k8s-node-12   <none>           <none>
nginx-deployment-64bcc7b77f-lrxrx   1/1     Running   0          3s    10.2.2.37   k8s-node-12   <none>           <none>

# 缩容
[root@k8s-master-10 ~]#kubectl -n yuchaoit scale deployment nginx-deployment --replicas=1
deployment.apps/nginx-deployment scaled

[root@k8s-master-10 ~]#kubectl -n yuchaoit get po -owide  
NAME                                READY   STATUS    RESTARTS   AGE    IP          NODE          NOMINATED NODE   READINESS GATES
nginx-deployment-64bcc7b77f-6xb95   1/1     Running   0          108s   10.2.1.26   k8s-node-11   <none>           <none>
[root@k8s-master-10 ~]#
posted @ 2023-10-21 23:14  村尚chun叔  阅读(279)  评论(0编辑  收藏  举报