Kubernetes不带to-revision的kubectl rollout undo存在无限死循环问题

Posted cnskylee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kubernetes不带to-revision的kubectl rollout undo存在无限死循环问题相关的知识,希望对你有一定的参考价值。

第一步,编写首次部署所需的yaml文件,包括三块:命名空间定义、服务定义、部署定义,nginx的镜像版本为 1.16.1

内容如下:

apiVersion: v1
kind: Namespace
metadata:
  name: load-balance

---

kind: Service
apiVersion: v1
metadata:
  labels:
    app: nginx-1161
  name: nginx-1161
  namespace: load-balance
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30012
  selector:
    app: nginx-1161

---

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: nginx-1161
  name: nginx-1161
  namespace: load-balance
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-1161
  template:
    metadata:
      labels:
        app: nginx-1161
    spec:
      containers:
      - name: nginx
        image: nginx:1.16.1
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          protocol: TCP

# 执行创建
$ kubectl apply -f ./nginx-deployment.yaml 
namespace/load-balance created
service/nginx-1161 created
deployment.apps/nginx-1161 created

# 第一次创建的容器实例我们简称为“7d”
$ kubectl get pods -n load-balance
NAME                          READY   STATUS    RESTARTS   AGE
nginx-1161-7d57d5c7c4-prk5p   1/1     Running   0          22m
nginx-1161-7d57d5c7c4-s4pqk   1/1     Running   0          22m
nginx-1161-7d57d5c7c4-v42kt   1/1     Running   0          22m

# 测试访问Nginx的主页,成功返回Nginx欢迎页面的html代码
$ curl http://192.168.223.201:30012/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

第二步,执行第一次滚动升级,修改Nginx镜像的版本为1.18.0,增加滚动升级的模块配置

apiVersion: v1
kind: Namespace
metadata:
  name: load-balance

---

kind: Service
apiVersion: v1
metadata:
  labels:
    app: nginx-1161
  name: nginx-1161
  namespace: load-balance
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30012
  selector:
    app: nginx-1161

---

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: nginx-1161
  name: nginx-1161
  namespace: load-balance
spec:
  minReadySeconds: 5     # 增加配置
  strategy:              # 增加配置
    type: RollingUpdate  # 增加配置
    rollingUpdate:       # 增加配置
      maxSurge: 1        # 增加配置
      maxUnavailable: 1  # 增加配置
  replicas: 3
  selector:
    matchLabels:
      app: nginx-1161
  template:
    metadata:
      labels:
        app: nginx-1161
    spec:
      containers:
      - name: nginx
        image: nginx:1.18.0   # 配置修改
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          protocol: TCP

# 执行滚动升级
$ kubectl apply -f nginx-update-I-deployment.yaml
namespace/load-balance unchanged
service/nginx-1161 unchanged
deployment.apps/nginx-1161 configured

# 查看滚动升级的状态
$ kubectl rollout status deployment nginx-1161 -n load-balance
deployment "nginx-1161" successfully rolled out

# 查看副本集(rs,即ReplicaSet)
$  kubectl get rs -n load-balance
NAME                    DESIRED   CURRENT   READY   AGE
nginx-1161-56dd96d66c   3         3         3       84s
nginx-1161-7d57d5c7c4   0         0         0       26m

# 查看pods和服务
$ kubectl get pods,svc -n load-balance
NAME                              READY   STATUS    RESTARTS   AGE
pod/nginx-1161-56dd96d66c-724rg   1/1     Running   0          119s
pod/nginx-1161-56dd96d66c-cnkn6   1/1     Running   0          81s
pod/nginx-1161-56dd96d66c-zhq4c   1/1     Running   0          119s

NAME                 TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/nginx-1161   NodePort   10.96.92.254   <none>        80:30012/TCP   28m

第三步,执行第二次滚动升级,修改image的版本为 1.20.1

apiVersion: v1
kind: Namespace
metadata:
  name: load-balance

---

kind: Service
apiVersion: v1
metadata:
  labels:
    app: nginx-1161
  name: nginx-1161
  namespace: load-balance
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30012
  selector:
    app: nginx-1161

---

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: nginx-1161
  name: nginx-1161
  namespace: load-balance
spec:
  minReadySeconds: 5
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  replicas: 3
  selector:
    matchLabels:
      app: nginx-1161
  template:
    metadata:
      labels:
        app: nginx-1161
    spec:
      containers:
      - name: nginx
        image: nginx:1.20.1      # 配置修改
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
          protocol: TCP

$ kubectl apply -f nginx-update-II-deployment.yaml 
namespace/load-balance unchanged
service/nginx-1161 unchanged
deployment.apps/nginx-1161 configured

#  kubectl get rs -n load-balance
NAME                    DESIRED   CURRENT   READY   AGE
nginx-1161-56dd96d66c   2         2         2       3m46s
nginx-1161-7cccdf449f   2         2         0       17s
nginx-1161-7d57d5c7c4   0         0         0       29m

$ kubectl get rs -n load-balance
NAME                    DESIRED   CURRENT   READY   AGE
nginx-1161-56dd96d66c   2         2         2       4m2s
nginx-1161-7cccdf449f   2         2         1       33s
nginx-1161-7d57d5c7c4   0         0         0       29m

$ kubectl get rs -n load-balance
NAME                    DESIRED   CURRENT   READY   AGE
nginx-1161-56dd96d66c   1         1         1       4m8s
nginx-1161-7cccdf449f   3         3         1       39s
nginx-1161-7d57d5c7c4   0         0         0       29m

$ kubectl get rs -n load-balance
NAME                    DESIRED   CURRENT   READY   AGE
nginx-1161-56dd96d66c   1         1         1       4m13s
nginx-1161-7cccdf449f   3         3         2       44s
nginx-1161-7d57d5c7c4   0         0         0       29m

$  kubectl get rs -n load-balance
NAME                    DESIRED   CURRENT   READY   AGE
nginx-1161-56dd96d66c   0         0         0       4m15s
nginx-1161-7cccdf449f   3         3         2       46s
nginx-1161-7d57d5c7c4   0         0         0       29m

# 至此,滚动升级全部完成(数据抓取时间延后,非升级速度慢)
$ kubectl get rs -n load-balance
NAME                    DESIRED   CURRENT   READY   AGE
nginx-1161-56dd96d66c   0         0         0       26m
nginx-1161-7cccdf449f   3         3         3       23m
nginx-1161-7d57d5c7c4   0         0         0       52m

检查历史版本的详细信息:

$ kubectl rollout history  deployment nginx-1161 -n load-balance --revision=3
deployment.apps/nginx-1161 with revision #3
Pod Template:
  Labels:       app=nginx-1161
        pod-template-hash=7cccdf449f
  Containers:
   nginx:
    Image:      nginx:1.20.1
    Port:       80/TCP
    Host Port:  0/TCP
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>

$ kubectl rollout history  deployment nginx-1161 -n load-balance --revision=2
deployment.apps/nginx-1161 with revision #2
Pod Template:
  Labels:       app=nginx-1161
        pod-template-hash=56dd96d66c
  Containers:
   nginx:
    Image:      nginx:1.18.0
    Port:       80/TCP
    Host Port:  0/TCP
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>

$ kubectl rollout history  deployment nginx-1161 -n load-balance --revision=1
deployment.apps/nginx-1161 with revision #1
Pod Template:
  Labels:       app=nginx-1161
        pod-template-hash=7d57d5c7c4
  Containers:
   nginx:
    Image:      nginx:1.16.1
    Port:       80/TCP
    Host Port:  0/TCP
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>

测试回滚到上一个版本:

$ kubectl rollout undo deployment nginx-1161 -n load-balance --to-revision=2
deployment.apps/nginx-1161 rolled back

$ kubectl get rs -n load-balance
NAME                    DESIRED   CURRENT   READY   AGE
nginx-1161-56dd96d66c   3         3         3       135m
nginx-1161-7cccdf449f   0         0         0       131m
nginx-1161-7d57d5c7c4   0         0         0       160m

如果不指定--to-revision,则默认回滚到上一个版本

$ kubectl rollout undo deployment nginx-1161 -n load-balance
deployment.apps/nginx-1161 rolled back

$ kubectl get rs -n load-balance
NAME                    DESIRED   CURRENT   READY   AGE
nginx-1161-56dd96d66c   0         0         0       138m
nginx-1161-7cccdf449f   3         3         3       135m
nginx-1161-7d57d5c7c4   0         0         0       164m

个人理解的回滚机制:

        通过测试发现,不带 --to-revision 参数的 undo 回滚,会进入死循环。如上图所示,会始终在V2和V3版本之间来回的切换,而无法回滚到最早的 nginx-1161-7d57d5c7c4 版本。不知道这算不算K8S回滚机制存在的一个Bug呢?

以上是关于Kubernetes不带to-revision的kubectl rollout undo存在无限死循环问题的主要内容,如果未能解决你的问题,请参考以下文章

编写一个不带括号的列表的字典值

Kubernetes nodeport 并发连接限制

浅尝TensorFlow on Kubernetes

图说Kubernetes

c语言宏定义有无参数的区别

初识Kubernetes