|NO.Z.00135|——————————|CloudNative|——|KuberNetes&资源调度.V05|——|deployment.v03|回滚|

一、Deployment回滚
### --- 回滚到上一个版本:查看资源的历史版本

~~~     # 回滚到上一个版本
~~~     模拟镜像版本地址写错了,回滚到之前的版本
~~~     其实是没有这个镜像地址,改为之前的版本;但是可能时间久了,你不知道之前的版本是多少。
~~~     就可以使用deployment回滚,回滚有两种情况:时间回滚到上个版本;或者回滚到指定的版本。
### --- 查看deployment的版本记录

[root@k8s-master01 ~]# kubectl rollout history deploy nginx         
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl set image deploy nginx nginx=nginx:1.15.3 --record=true
3         kubectl set image deploy nginx nginx=nginx:1.15.4 --record=true
4         kubectl set image deploy nginx nginx=nginx:huigun --record=true 

二、将资源回滚到上一个版本

### --- 将pod回滚到上个版本

[root@k8s-master01 ~]# kubectl rollout undo deploy nginx
deployment.apps/nginx rolled back
### --- 查看更新后pod的状态

[root@k8s-master01 ~]# kubectl get po                                               //执行完成,最后一个版本被删除掉
NAME                     READY   STATUS    RESTARTS   AGE       
nginx-6cdd5dd489-rsf6k   1/1     Running   0          17m
nginx-6cdd5dd489-sfdzr   1/1     Running   0          15m
### --- 查看镜像版本号

[root@k8s-master01 ~]# kubectl get deploy nginx -oyaml |grep nginx
    kubernetes.io/change-cause: kubectl set image deploy nginx nginx=nginx:1.15.4           //版本回滚到1.15.4
    app: nginx
              k:{"name":"nginx"}:
              k:{"name":"nginx"}:
  name: nginx
      app: nginx
        app: nginx
      - image: nginx:1.15.4
        name: nginx
    message: ReplicaSet "nginx-6cdd5dd489" has successfully progressed. 

三、回滚到指定版本:查看pod的历史版本

### --- 若是经过了多次更新发布, 

[root@k8s-master01 ~]#  kubectl set image deploy nginx nginx=nginx:huigunzhunbei-1 --record
deployment.apps/nginx image updated
[root@k8s-master01 ~]#  kubectl set image deploy nginx nginx=nginx:huigunzhunbei-2 --record
deployment.apps/nginx image updated
[root@k8s-master01 ~]#  kubectl set image deploy nginx nginx=nginx:huigunzhunbei-3 --record
deployment.apps/nginx image updated
[root@k8s-master01 ~]#  kubectl set image deploy nginx nginx=nginx:huigunzhunbei-4 --record
deployment.apps/nginx image updated  
### --- 查看历史记录
~~~     假设我们不记得上一次的版本是多少,或者需要回滚到指定的版本

[root@k8s-master01 ~]# kubectl rollout history deploy nginx
deployment.apps/nginx 
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl set image deploy nginx nginx=nginx:1.15.3 --record=true
4         kubectl set image deploy nginx nginx=nginx:huigun --record=true
5         kubectl set image deploy nginx nginx=nginx:1.15.4 --record=true
6         kubectl set image deploy nginx nginx=nginx:huigunzhunbei-1 --record=true
7         kubectl set image deploy nginx nginx=nginx:huigunzhunbei-2 --record=true
8         kubectl set image deploy nginx nginx=nginx:huigunzhunbei-3 --record=true
9         kubectl set image deploy nginx nginx=nginx:huigunzhunbei-4 --record=true
### --- 首先查看指定版本的详细信息;确定这个版本是否是我们所需要的。

[root@k8s-master01 ~]# kubectl rollout history deploy nginx --revision=5
deployment.apps/nginx with revision #5
Pod Template:
  Labels:   app=nginx
    pod-template-hash=6cdd5dd489
  Annotations:  kubernetes.io/change-cause: kubectl set image deploy nginx nginx=nginx:1.15.4 --record=true
  Containers:
   nginx:
    Image:  nginx:1.15.4                                                            //查看到前一个更改是做了镜像的更改,这个镜像版本刚好是我们所需要的
    Port:   <none>
    Host Port:  <none>
    Environment:    <none>
    Mounts: <none>
  Volumes:  <none> 

四、回滚到指定版本;回滚deployment

### --- 模拟现在的镜像镜像地址不小心写错了,pod启动不起来

[root@k8s-master01 ~]# kubectl get po
NAME                     READY   STATUS             RESTARTS   AGE
nginx-5788dd5dcd-zt5t7   0/1     ImagePullBackOff   0          4m20s                //现在查看这个pod的状态是不正常的;因为镜像是没有拉取到的
[root@k8s-master01 ~]# kubectl describe po nginx-5788dd5dcd-zt5t7 
Events:
  Type     Reason     Age                    From               Message
  ----     ------     ----                   ----               -------
  Normal   Scheduled  5m38s                  default-scheduler  Successfully assigned default/nginx-5788dd5dcd-zt5t7 to k8s-master03
  Normal   Pulling    3m57s (x4 over 5m35s)  kubelet            Pulling image "nginx:huigunzhunbei-4"
  Warning  Failed     3m55s (x4 over 5m31s)  kubelet            Failed to pull image "nginx:huigunzhunbei-4": rpc error: code = Unknown desc = Error response from daemon: manifest for nginx:huigunzhunbei-4 not found: manifest unknown: manifest unknown
  Warning  Failed     3m55s (x4 over 5m31s)  kubelet            Error: ErrImagePull
  Normal   BackOff    3m41s (x6 over 5m31s)  kubelet            Back-off pulling image "nginx:huigunzhunbei-4"
  Warning  Failed     34s (x20 over 5m31s)   kubelet            Error: ImagePullBackOff
### --- 回滚到第5个版本
~~~     若是写错镜像,镜像版本是拉取不下来,但是旧的还是一直都在,旧的还是在提供工作。
~~~     只有你新的起来了,旧的才会被删除掉,这个k8s的保护机制。

[root@k8s-master01 ~]# kubectl rollout undo deploy nginx --to-revision=5
deployment.apps/nginx rolled back
[root@k8s-master01 ~]# kubectl get deploy -oyaml 
      spec:
        containers:
        - image: nginx:1.15.4
message: ReplicaSet "nginx-6cdd5dd489" has successfully progressed. // 它的ReplicaSet又回到nginx-6cdd5dd489这个版本

 

 

 

 

 

 

 

 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(16)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

导航

统计

点击右上角即可分享
微信分享提示