K8S Gateway AB测试、蓝绿发布、金丝雀(灰度)发布

[复制链接]
发表于 2025-8-31 05:02:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
假设有如下三个节点的 K8S 集群:
​
   k8s31master 是控制节点
  k8s31node1、k8s31node2 是工作节点
  容器运行时是 containerd
   一、场景分析

阅读本文,默认您已经安装了 K8S Gateway。
关于 AB 测试、金丝雀发布,可以看这篇文章。
 二、实验预备



  • 镜像下载
  1. # 在各个工作节点下载
  2. [root@k8s31node1 ~]# ctr -n=k8s.io images pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/openresty/openresty:latest
  3. [root@k8s31node1 ~]# ctr -n=k8s.io images tag  swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/openresty/openresty:latest  docker.io/openresty/openresty:latest
  4. [root@k8s31node2 ~]# ctr -n=k8s.io images pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/openresty/openresty:latest
  5. [root@k8s31node2 ~]# ctr -n=k8s.io images tag  swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/openresty/openresty:latest  docker.io/openresty/openresty:latest
复制代码


  •  部署 v1
  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4.   name: nginx-v1
  5. spec:
  6.   replicas: 1
  7.   selector:
  8.     matchLabels:
  9.       app: nginx
  10.       version: v1
  11.   template:
  12.     metadata:
  13.       labels:
  14.         app: nginx
  15.         version: v1
  16.     spec:
  17.       containers:
  18.       - name: nginx
  19.         image: "openresty/openresty:latest"
  20.         imagePullPolicy: IfNotPresent
  21.         ports:
  22.         - name: http
  23.           protocol: TCP
  24.           containerPort: 80
  25.         volumeMounts:
  26.         - mountPath: /usr/local/openresty/nginx/conf/nginx.conf
  27.           name: config
  28.           subPath: nginx.conf
  29.       volumes:
  30.       - name: config
  31.         configMap:
  32.           name: nginx-v1
  33. ---
  34. apiVersion: v1
  35. kind: ConfigMap
  36. metadata:
  37.   labels:
  38.     app: nginx
  39.     version: v1
  40.   name: nginx-v1
  41. data:
  42.   nginx.conf: |-
  43.     worker_processes  1;
  44.     events {
  45.         accept_mutex on;
  46.         multi_accept on;
  47.         use epoll;
  48.         worker_connections  1024;
  49.     }
  50.     http {
  51.         ignore_invalid_headers off;
  52.         server {
  53.             listen 80;
  54.             location / {
  55.                 access_by_lua '
  56.                     local header_str = ngx.say("nginx-v1")
  57.                 ';
  58.             }
  59.         }
  60.     }
  61. ---
  62. apiVersion: v1
  63. kind: Service
  64. metadata:
  65.   name: nginx-v1
  66. spec:
  67.   type: ClusterIP
  68.   ports:
  69.   - port: 80
  70.     protocol: TCP
  71.     name: http
  72.   selector:
  73.     app: nginx
  74.     version: v1
复制代码
  该 yml 定义了三个资源 ConfigMap、Deployment、Service。
  

  • ConfigMap 定义了一个 nginx.conf 设置文件,使用 lua 脚本输出 nginx-v1。
  • Deployment 定义了一个 Pod,内里运行 openresty 它是一个封装了 nginx+lua 的 web 服务器。Pod 有两个标签 app: nginx、version: v1。
  • Service 代理了 Deployment 运行的 Pod。
  部署 v2

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4.   name: nginx-v2
  5. spec:
  6.   replicas: 1
  7.   selector:
  8.     matchLabels:
  9.       app: nginx
  10.       version: v2
  11.   template:
  12.     metadata:
  13.       labels:
  14.         app: nginx
  15.         version: v2
  16.     spec:
  17.       containers:
  18.       - name: nginx
  19.         image: "openresty/openresty:latest"
  20.         imagePullPolicy: IfNotPresent
  21.         ports:
  22.         - name: http
  23.           protocol: TCP
  24.           containerPort: 80
  25.         volumeMounts:
  26.         - mountPath: /usr/local/openresty/nginx/conf/nginx.conf
  27.           name: config
  28.           subPath: nginx.conf
  29.       volumes:
  30.       - name: config
  31.         configMap:
  32.           name: nginx-v2
  33. ---
  34. apiVersion: v1
  35. kind: ConfigMap
  36. metadata:
  37.   labels:
  38.     app: nginx
  39.     version: v2
  40.   name: nginx-v2
  41. data:
  42.   nginx.conf: |-
  43.     worker_processes  1;
  44.     events {
  45.         accept_mutex on;
  46.         multi_accept on;
  47.         use epoll;
  48.         worker_connections  1024;
  49.     }
  50.     http {
  51.         ignore_invalid_headers off;
  52.         server {
  53.             listen 80;
  54.             location / {
  55.                 access_by_lua '
  56.                     local header_str = ngx.say("nginx-v2")
  57.                 ';
  58.             }
  59.         }
  60.     }
  61. ---
  62. apiVersion: v1
  63. kind: Service
  64. metadata:
  65.   name: nginx-v2
  66. spec:
  67.   type: ClusterIP
  68.   ports:
  69.   - port: 80
  70.     protocol: TCP
  71.     name: http
  72.   selector:
  73.     app: nginx
  74.     version: v2
复制代码

三、AB 测试

1)创建 gateway

  1. apiVersion: gateway.networking.k8s.io/v1
  2. kind: Gateway
  3. metadata:
  4.   name: nginx-gateway
  5.   namespace: default
  6. spec:
  7.   gatewayClassName: nginx
  8.   listeners:
  9.   - name: http
  10.     protocol: HTTP
  11.     port: 80
复制代码
 2)创建 httproute

  1. apiVersion: gateway.networking.k8s.io/v1
  2. kind: HTTPRoute
  3. metadata:
  4.   name: abtest-route
  5.   namespace: default
  6. spec:
  7.   parentRefs:
  8.     - name: nginx-gateway
  9.   hostnames:
  10.     - "abtest.example.com"
  11.   rules:
  12.     - matches:
  13.         - path:
  14.             type: PathPrefix
  15.             value: /
  16.           headers:
  17.             - name: "version"
  18.               value: "v1"
  19.       backendRefs:
  20.         - name: nginx-v1
  21.           kind: Service
  22.           port: 80
  23.     - matches:
  24.         - path:
  25.             type: PathPrefix
  26.             value: /
  27.           headers:
  28.             - name: "version"
  29.               value: "v2"  
  30.       backendRefs:
  31.         - name: nginx-v2
  32.           kind: Service
  33.           port: 80
复制代码
  

  • parentRefs:绑定我们新建的 gateway。
  • hostnames:定义访问的主机名。
  • rules.matches:定义路由规则,PathPrefix 表现路径前缀匹配。headers 对请求头进行匹配。
  • backendRefs:定义后端服务以及服务端口。
   3)测试

  1. curl -H "Host: abtest.example.com" -H "version: v1" http://192.168.40.20:30185/
  2. curl -H "Host: abtest.example.com" -H "version: v2" http://192.168.40.20:30185/
复制代码

    30185 为 nginx-gateway-controller 80 映射端口。
  

   四、金丝雀发布

 1)创建 httproute

  1. apiVersion: gateway.networking.k8s.io/v1
  2. kind: HTTPRoute
  3. metadata:
  4.   name: canary-route
  5.   namespace: default
  6. spec:
  7.   parentRefs:
  8.     - name: nginx-gateway
  9.   hostnames:
  10.     - "canary.example.com"
  11.   rules:
  12.     - matches:
  13.         - path:
  14.             type: PathPrefix
  15.             value: /
  16.       backendRefs:
  17.         - name: nginx-v1
  18.           kind: Service
  19.           port: 80
  20.           weight: 10
  21.         - name: nginx-v2
  22.           kind: Service
  23.           port: 80
  24.           weight: 90
复制代码
   backendRefs.weight 定义流量分发的权重。
   2)测试

  1. for i in {1..20}; do curl -H "Host: canary.example.com" http://192.168.40.20:30185/; done;
复制代码

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表