k8s deployment发布失败回滚

1. deployment发布失败回滚

  • 查看历史发布版本

    kubectl rollout history deployment/web
    
  • 回滚上一个版本

    kubectl rollout undo deployment/web
    
  • 回滚历史指定版本

    kubectl rollout undo deployment/web --to-revision=2 
    
  • 注:回滚是重新部署某一次部署时的状态,即当时版本所有配置

  • 示例:yaml配置文件

    [root@k8s-master deployment]# cat web.yaml 
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: web
      namespace: default
      annotations:       # 记录回滚参数
        kubernetes.io/change-cause: "web.v1-nginx-1.19"   #记录到revision中的内容,记录版本号
    spec:
      replicas: 9 # Pod副本预期数量
      revisionHistoryLimit: 10 # RS历史版本保存数量
      selector:
        matchLabels:
          app: web
      strategy:
        rollingUpdate:
          maxSurge: 25%             # 滚动更新过程最大pod副本数
          maxUnavailable: 25%       # 滚动更新过程中最大不可用pod副本数,
        type: RollingUpdate
      template:
        metadata:
          labels:
            app: web # Pod副本的标签
        spec:
          containers:
          - name: web
            image: nginx:1.19
            readinessProbe:          # 健康检查,如果失败,将杀死容器,来重启
              httpGet:
                port: 80
                path: /index.html
              initialDelaySeconds: 10 #启动容器后多少秒健康检查
              periodSeconds: 10 #以后间隔多少秒检查一次
    
            livenessProbe:   # 存活检查,失败就会剔除 service 
              httpGet:
                port: 80
                path: /index.html
    
    
  • 查看history历史记录

    [root@k8s-master deployment]# kubectl rollout history deployment web
    deployment.apps/web 
    REVISION  CHANGE-CAUSE
    1         web.v1-nginx-1.16
    2         web.v1-nginx-1.18
    3         web.v1-nginx-1.19
    

2. 案例操作

  • 查询发布历史版本

    [root@k8s-master deployment]# kubectl rollout history deployment web
    deployment.apps/web 
    REVISION  CHANGE-CAUSE
    1         web.v1-nginx-1.16
    2         web.v1-nginx-1.18
    3         web.v1-nginx-1.19
    
    
    
  • 回滚到上一个版本

    [root@k8s-master deployment]# curl -I http://192.168.0.202:30082
    HTTP/1.1 200 OK
    Server: nginx/1.19.5
    Date: Tue, 01 Dec 2020 14:20:33 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Tue, 24 Nov 2020 13:02:03 GMT
    Connection: keep-alive
    ETag: "5fbd044b-264"
    Accept-Ranges: bytes
    
    # 回滚到上一个版本
    [root@k8s-master deployment]# kubectl rollout  undo deployment web
    deployment.apps/web rolled back
    
    
    [root@k8s-master deployment]# curl -I http://192.168.0.202:30082
    HTTP/1.1 200 OK
    Server: nginx/1.18.0
    Date: Tue, 01 Dec 2020 14:21:39 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Tue, 21 Apr 2020 14:09:01 GMT
    Connection: keep-alive
    ETag: "5e9efe7d-264"
    Accept-Ranges: bytes
    
    [root@k8s-master deployment]# kubectl rollout history deployment web
    deployment.apps/web 
    REVISION  CHANGE-CAUSE
    1         web.v1-nginx-1.16
    3         web.v1-nginx-1.19
    4         web.v1-nginx-1.18
    
    • 注释:
      1. 发现已经回滚到nginx版本1.18版本
      2. 我们发现2的版本号,被4的版本号替换,3的版本号是1.19.5的nginx版本
  • 回滚到指定版本

    [root@k8s-master deployment]# kubectl rollout undo deployment web  --to-revision=3 
    deployment.apps/web rolled back
    
    [root@k8s-master deployment]# curl -I http://192.168.0.202:30082
    HTTP/1.1 200 OK
    Server: nginx/1.19.5
    Date: Tue, 01 Dec 2020 14:25:12 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Tue, 24 Nov 2020 13:02:03 GMT
    Connection: keep-alive
    ETag: "5fbd044b-264"
    Accept-Ranges: bytes
    
    [root@k8s-master deployment]# kubectl rollout history deployment web
    deployment.apps/web 
    REVISION  CHANGE-CAUSE
    1         web.v1-nginx-1.16
    4         web.v1-nginx-1.18
    5         web.v1-nginx-1.19
    
    • 注释:在这里,有没有发现,我们上面的实例代码里,进行配置后,版本号记录更改,都有记录
上一篇:【K8s任务】确定 Pod 失败的原因


下一篇:k8s deployment控制器的介绍