当前位置: 首页 > article >正文

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的知识,请随时提问。祝你在金丝雀发布中取得成功!


http://www.kler.cn/a/148657.html

相关文章:

  • 微服务架构面试内容整理-API 网关-Gateway
  • Appium配置2024.11.12
  • 【真题笔记】21年系统架构设计师案例理论点总结
  • 系统架构设计师论文:大数据Lambda架构
  • 算法演练----24点游戏
  • PostgreSQL中的COPY命令:高效数据导入与导出
  • 经典滑动窗口试题(二)
  • 【LeetCode刷题-链表】--86.分隔链表
  • LLM、ChatGPT与多模态必读论文150篇
  • 使用opencv将sRGB格式的图片转换为Adobe-RGB格式【sRGB】【Adobe-RGB】
  • 数据挖掘 朴素贝叶斯
  • tp6框架 万级数据入库 php函数优化
  • 如何解决 Java 中的 IllegalArgumentException 异常?
  • Windows10系统卸载服务和删除服务
  • 使用STM32 HAL库驱动光电传感器的设计和优化
  • Python算法——Merkle树
  • 09-详解JSR303规范及其对应的校验框架的使用
  • Python与设计模式--中介者模式
  • 国家对于新消费新经济有哪些新旨意?
  • VScode集成python开发环境和基本插件下载配置
  • 【沐风老师】3DMAX拼图建模工具MaxPuzzle2D插件使用方法详解
  • 视频字幕处理+AI绘画,Runway 全功能超详细使用教程(4)
  • 学习MySQL先有全局观,细说其发展历程及特点
  • 学习笔记-瑞吉外卖项目实战(一)
  • 食谱菜谱大全API接口
  • 设计模式——RBAC 模型详解