kubectl rollout 实现金丝雀发布的流量控制策略

介绍

金丝雀发布是一种流量控制策略,用于逐步引入新版本应用程序,以降低潜在的风险。在Kubernetes中,我们可以利用kubectl rollout pausekubectl rollout resume命令,结合修改Deployment的maxSurgemaxUnavailable字段,来实现金丝雀发布的流量控制。本文将详细介绍如何执行这些步骤,并通过多次迭代来完成金丝雀发布。

步骤

以下是实现金丝雀发布的主要步骤:

步骤一:创建Deployment

首先,创建一个Deployment对象来定义原始的工作负载,例如使用nginx容器的Deployment。以下是一个示例的Deployment定义:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 10%
      maxUnavailable: 0
  replicas: 10
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.18-alpine

 观察Pod的状态,可以使用以下命令:

kubectl get po
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-6956d7b747-6lwfk   1/1     Running   0          4m55s
nginx-deployment-6956d7b747-942vx   1/1     Running   0          4m55s
nginx-deployment-6956d7b747-drm62   1/1     Running   0          4m55s
nginx-deployment-6956d7b747-gggkb   1/1     Running   0          4m55s
nginx-deployment-6956d7b747-mkw86   1/1     Running   0          4m55s
nginx-deployment-6956d7b747-sf75t   1/1     Running   0          4m55s
nginx-deployment-6956d7b747-vzqxd   1/1     Running   0          4m55s
nginx-deployment-6956d7b747-x5mqb   1/1     Running   0          4m55s
nginx-deployment-6956d7b747-xm7x5   1/1     Running   0          4m55s
nginx-deployment-6956d7b747-z6h7r   1/1     Running   0          4m55s

 

步骤二:暂停滚动更新

在进行金丝雀发布之前,可以使用以下命令暂停滚动更新:

kubectl rollout pause deploy/nginx-deployment

 

步骤三:修改新版本

将原始工作负载中的镜像版本修改为新的版本,例如将nginx镜像版本修改为nginx:1.19-alpine

使用以下命令打开再关闭滚动更新,以启动新版本的金丝雀发布:

kubectl rollout resume deploy/nginx-deployment && kubectl rollout pause deploy/nginx-deployment

观察Pod的状态:

NAME READY STATUS RESTARTS AGE
nginx-deployment-55844bc688-86l24 1/1 Running 0 4s
nginx-deployment-6956d7b747-6lwfk 1/1 Running 0 5m11s
nginx-deployment-6956d7b747-942vx 1/1 Running 0 5m11s
nginx-deployment-6956d7b747-drm62 1/1 Running 0 5m11s
nginx-deployment-6956d7b747-gggkb 1/1 Running 0 5m11s
nginx-deployment-6956d7b747-mkw86 1/1 Running 0 5m11s
nginx-deployment-6956d7b747-sf75t 1/1 Running 0 5m11s
nginx-deployment-6956d7b747-vzqxd 1/1 Running 0 5m11s
nginx-deployment-6956d7b747-x5mqb 1/1 Running 0 5m11s
nginx-deployment-6956d7b747-xm7x5 1/1 Running 0 5m11s
nginx-deployment-6956d7b747-z6h7r 1/1 Running 0 5m11s

步骤四:放量

修改原始工作负载的Deployment对象,将maxSurge的值修改为期望的金丝雀发布期间的最大并发副本数。例如,将maxSurge的值修改为30%。

继续使用以下命令打开再关闭滚动更新,以启动新版本的金丝雀发布:

kubectl rollout resume deploy/nginx-deployment && kubectl rollout pause deploy/nginx-deployment

观察Pod的状态:

NAME READY STATUS RESTARTS AGE
nginx-deployment-55844bc688-679rq 1/1 Running 0 3s
nginx-deployment-55844bc688-86l24 1/1 Running 0 91s
nginx-deployment-55844bc688-clpb9 1/1 Running 0 3s
nginx-deployment-55844bc688-v54s6 1/1 Running 0 3s
nginx-deployment-6956d7b747-942vx 1/1 Running 0 6m38s
nginx-deployment-6956d7b747-drm62 1/1 Running 0 6m38s
nginx-deployment-6956d7b747-mkw86 1/1 Running 0 6m38s
nginx-deployment-6956d7b747-r9v9f 1/1 Running 0 47s
nginx-deployment-6956d7b747-sf75t 1/1 Running 0 6m38s
nginx-deployment-6956d7b747-vzqxd 1/1 Running 0 6m38s
nginx-deployment-6956d7b747-x5mqb 1/1 Running 0 6m38s
nginx-deployment-6956d7b747-xm7x5 1/1 Running 0 6m38s
nginx-deployment-6956d7b747-z6h7r 1/1 Running 0 6m38s

 

步骤六:放全量

根据需要,可以再次修改maxSurge的值,将其增加到60%或其他期望的并发副本数。​
使用以下命令打开再关闭滚动更新,以启动新版本的金丝雀发布:

kubectl rollout resume deploy/nginx-deployment && kubectl rollout pause deploy/nginx-deployment

观察Pod的状态:

NAME READY STATUS RESTARTS AGE
nginx-deployment-55844bc688-2hlvp 1/1 Running 0 38s
nginx-deployment-55844bc688-679rq 1/1 Running 0 95s
nginx-deployment-55844bc688-6whh8 1/1 Running 0 38s
nginx-deployment-55844bc688-86l24 1/1 Running 0 3m3s
nginx-deployment-55844bc688-99mpz 1/1 Running 0 38s
nginx-deployment-55844bc688-clpb9 1/1 Running 0 95s
nginx-deployment-55844bc688-ffpbs 1/1 Running 0 38s
nginx-deployment-55844bc688-gl2k6 1/1 Running 0 38s
nginx-deployment-55844bc688-qp6lx 1/1 Running 0 52s
nginx-deployment-55844bc688-v54s6 1/1 Running 0 95s
nginx-deployment-55844bc688-vdfgg 1/1 Running 0 38s
nginx-deployment-6956d7b747-drm62 1/1 Running 0 8m10s
nginx-deployment-6956d7b747-mkw86 1/1 Running 0 8m10s
nginx-deployment-6956d7b747-vzqxd 1/1 Running 0 8m10s
nginx-deployment-6956d7b747-x5mqb 1/1 Running 0 8m10s
nginx-deployment-6956d7b747-z6h7r 1/1 Running 0 8m10s

步骤八:恢复正常

当金丝雀发布完成后,可以使用以下命令恢复正常的滚动更新:

kubectl rollout resume deploy/nginx-deployment

观察Pod的状态:

NAME READY STATUS RESTARTS AGE
nginx-deployment-55844bc688-2hlvp 1/1 Running 0 55s
nginx-deployment-55844bc688-679rq 1/1 Running 0 112s
nginx-deployment-55844bc688-6whh8 1/1 Running 0 55s
nginx-deployment-55844bc688-86l24 1/1 Running 0 3m20s
nginx-deployment-55844bc688-99mpz 1/1 Running 0 55s
nginx-deployment-55844bc688-clpb9 1/1 Running 0 112s
nginx-deployment-55844bc688-ffpbs 1/1 Running 0 55s
nginx-deployment-55844bc688-gl2k6 1/1 Running 0 55s
nginx-deployment-55844bc688-qp6lx 1/1 Running 0 69s
nginx-deployment-55844bc688-v54s6 1/1 Running 0 112s

结论

金丝雀发布是一种有效的流量控制策略,可以在引入新版本应用程序时减少风险。通过使用Kubernetes的kubectl rollout pausekubectl rollout resume命令,结合修改Deployment的maxSurgemaxUnavailable字段,我们可以灵活控制金丝雀发布的流量分布。通过反复迭代和监控新版本的性能和稳定性,可以逐步引入新版本,并确保对生产环境的影响最小化。金丝雀发布为我们提供了一种可靠而可控的方式来升级应用程序,同时保持用户体验的稳定性。

希望本文对你理解如何利用kubectl rollout pausekubectl rollout resume命令以及修改Deployment的maxSurgemaxUnavailable字段来实现金丝雀发布的流量控制有所帮助。通过这些步骤,你可以逐步引入新版本,并在验证过程中保持对应用程序的控制。金丝雀发布使得应用程序的升级过程更加可靠和安全,为团队提供了更好的升级策略。

如果你对金丝雀发布还有其他问题或者想要了解更多关于Kubernetes的知识,请随时提问。祝你在金丝雀发布中取得成功!

你可能感兴趣的:(k8s,基础运维,linux,运维,服务器)