从零到成熟模块详解

服务网格与网关 · 第 11 / 13 章

11金丝雀实操:用网格做 5% 灰度,看指标后全量或回滚

这一章把前面所有内容串成一次完整的灰度发布:部署两个版本 → 按 5% 切流 → 用网格指标判断 → 决定全量或回滚。判据是硬门槛:每档观察至少 10 分钟或 1000 个请求(取更长的那个),v2 的 5xx 率不超过 0.5%,p99 不超过 200ms,且与 v1 的差异不超过 20%。任何一条不满足就回滚,不讨论「再观察一会儿」。

11.1 准备:两个版本、一个 subset、一条灰度路由

先确认三件前置条件:集群里已装 Istio(istioctl version 有 control plane);orders 命名空间已打注入标签;两个版本的 Deployment 都带 version 标签——subset 是靠标签选实例的,标签少了就是 503 UH,这是灰度失败的第一大原因。

实操 A:部署 v1 / v2 与灰度配置,并验证 subset 真的选到了实例
# 前置:kubectl + istioctl;一个可写的测试集群
      $ istioctl install --set profile=minimal -y
      $ kubectl create namespace orders
      $ kubectl label namespace orders istio-injection=enabled

      # 1) 两个版本的 Deployment(用同一镜像的不同 tag 模拟;真实项目是两个镜像)
      $ kubectl -n orders create deployment orders-v1 --image=nginx:1.25-alpine --replicas=3
      $ kubectl -n orders create deployment orders-v2 --image=nginx:1.27-alpine --replicas=1
      $ kubectl -n orders set env deployment/orders-v1 VERSION=v1
      $ kubectl -n orders set env deployment/orders-v2 VERSION=v2
      # 关键:给 Pod 打上 version 标签(Deployment 的 selector 不会自动加)
      $ kubectl -n orders patch deployment orders-v1 -p \
          '{"spec":{"template":{"metadata":{"labels":{"version":"v1"}}}}}'
      $ kubectl -n orders patch deployment orders-v2 -p \
          '{"spec":{"template":{"metadata":{"labels":{"version":"v2"}}}}}'
      $ kubectl -n orders expose deployment orders-v1 --name=orders --port=80 --target-port=80
      # Service 的 selector 必须同时选中两个版本(改成 app 维度,而不是 version 维度)
      $ kubectl -n orders patch svc orders -p \
          '{"spec":{"selector":{"app":"orders-v1"}}}'

      # 2) 等待就绪,确认两个版本都有实例
      $ kubectl -n orders rollout status deploy/orders-v1
      $ kubectl -n orders rollout status deploy/orders-v2
      $ kubectl -n orders get pods -L version
      NAME                         READY   STATUS    RESTARTS   AGE   VERSION
      orders-v1-6b8c9d7f4-2xk9p    2/2     Running   0          41s   v1
      orders-v1-6b8c9d7f4-8m4qt    2/2     Running   0          41s   v1
      orders-v1-6b8c9d7f4-qz7vn    2/2     Running   0          41s   v1
      orders-v2-5d7f8b6c9-l2w4r    2/2     Running   0          39s   v2
      # 2/2 = 业务容器 + istio-proxy

      # 3) DestinationRule 定义 subsets,VirtualService 定义 95/5
      $ kubectl apply -f - <<'EOF'
      apiVersion: networking.istio.io/v1
      kind: DestinationRule
      metadata: { name: orders, namespace: orders }
      spec:
        host: orders.orders.svc.cluster.local
        subsets:
          - name: v1
            labels: { version: v1 }
          - name: v2
            labels: { version: v2 }
      ---
      apiVersion: networking.istio.io/v1
      kind: VirtualService
      metadata: { name: orders, namespace: orders }
      spec:
        hosts: [ "orders.orders.svc.cluster.local" ]
        http:
          - name: canary
            route:
              - destination: { host: orders.orders.svc.cluster.local, subset: v1 }
                weight: 95
              - destination: { host: orders.orders.svc.cluster.local, subset: v2 }
                weight: 5
            timeout: 3s
            retries:
              attempts: 1
              perTryTimeout: 1s
              retryOn: connect-failure,refused-stream,unavailable
      EOF
      $ sleep 3

      # 4) 验证 subset 选到了实例(数量必须与 Deployment 副本数一致)
      $ istioctl proxy-config endpoint deploy/orders-v1.orders -n orders | grep -c HEALTHY
      3
      $ istioctl proxy-config endpoint deploy/orders-v1.orders -n orders | grep -c v2
      1
      # 若输出 0,说明 version 标签没打上,或 subset 的 labels 写错了

预期与判读:第 4 步的两个数字是「subset 是否为空」的唯一判据。灰度发布最常见的失败模式就是 subset 为空,表现为 100% 的请求 503 且 response_flags=UH——因为没有任何实例匹配 subset 的标签。

11.2 5% 阶段的判读:三条查询 + 一张决策表

灰度期间只需要看四件事:比例对不对、错误率多少、延迟多少、样本够不够。前三条各一条 PromQL,第四条数请求数。

# ① 实际分流比例(目标 0.05,容差 ±0.01)
      sum(rate(istio_requests_total{destination_workload="orders",destination_version="v2",reporter="destination"}[1m]))
        /
      sum(rate(istio_requests_total{destination_workload="orders",reporter="destination"}[1m]))

      # ② v2 的 5xx 率(门槛 0.5%)
      sum(rate(istio_requests_total{destination_workload="orders",destination_version="v2",response_code=~"5..",reporter="destination"}[5m]))
        /
      sum(rate(istio_requests_total{destination_workload="orders",destination_version="v2",reporter="destination"}[5m]))

      # ③ v2 的 p99(门槛 200ms;直方图被裁剪过的环境改用入口侧的应用指标)
      histogram_quantile(0.99,
        sum(rate(istio_request_duration_milliseconds_bucket{destination_workload="orders",destination_version="v2",reporter="destination"}[5m])) by (le))

      # ④ 样本量(门槛 1000)
      sum(increase(istio_requests_total{destination_workload="orders",destination_version="v2",reporter="destination"}[10m]))
档位最短观察最小样本5xx 门槛p99 门槛不达标的动作
5%10 分钟1000 请求≤ 0.5% 且与 v1 差 ≤ 0.2pp≤ 200ms 且与 v1 差 ≤ 20%立刻回滚到 100/0
25%10 分钟5000 请求同上同上回滚到 100/0,不做「退回 5%」的折中
50%15 分钟10000 请求同上同上回滚到 100/0
100%观察 30 分钟—错误预算消耗 ≤ 2%—回滚到 50/50 并复盘
L4 上的灰度比例会抖:连接级负载均衡与请求级不是一回事

如果灰度是在 L4 上做的(ambient 但没配 waypoint、Nginx stream、TCP 代理),权重只在新连接建立时生效,长连接会把一个客户端整个钉在某个版本上。真实案例:40 个调用方 Pod 各持 2 条长连接(共 80 条),5% 的权重让 v2 拿到 4 条连接——某一分钟 v2 的请求占比实测 1.2%,下一分钟 14.8%,看着像流量在乱跳,实际是「按连接分」的必然结果。

三种处理方式:给这类服务加 waypoint 让流量走 L7(权重按请求生效);改用 header 路由(不受连接复用影响);或者把 maxRequestsPerConnection 设成 1 强制每个请求新建连接(代价是放弃连接复用,只适合低 QPS 场景)。千万不要在 L4 灰度下用「比例是否等于 5%」当验收判据。

11.3 全量与回滚:把回滚当成一次演练做完

实操 B:放量、验证、再完整回滚一次
# 前置:实操 A 已完成,5% 档位已观察满 10 分钟且指标达标

      # 1) 放量到 25%
      $ kubectl -n orders patch virtualservice orders --type=json \
          -p='[{"op":"replace","path":"/spec/http/0/route/0/weight","value":75},
               {"op":"replace","path":"/spec/http/0/route/1/weight","value":25}]'
      $ date +%H:%M:%S.%3N && sleep 3 && kubectl -n orders get vs orders \
          -o jsonpath='{.spec.http[0].route[*].weight}'
      14:22:07.412
      75 25

      # 2) 确认比例真的生效(等 10 分钟后再采信)
      $ curl -s localhost:9090/api/v1/query --data-urlencode \
          'query=sum(rate(istio_requests_total{destination_workload="orders",destination_version="v2",reporter="destination"}[1m])) / sum(rate(istio_requests_total{destination_workload="orders",reporter="destination"}[1m]))' \
          | jq -r '.data.result[].value[1]'
      0.2483
      # 0.2483 落在 0.25 ± 0.01 内 = 配置与流量一致

      # 3) 模拟一次「不达标」并执行回滚(这一步必须做,否则等于没练过)
      $ kubectl -n orders patch virtualservice orders --type=json \
          -p='[{"op":"replace","path":"/spec/http/0/route/0/weight","value":100},
               {"op":"replace","path":"/spec/http/0/route/1/weight","value":0}]'
      $ sleep 3
      $ istioctl -n orders proxy-config route deploy/orders-v1.orders -o json \
          | jq -r '.. | objects | select(has("weightedClusters")) | .weightedClusters[] | "\(.name) \(.weight)"'
      outbound|80|v1|orders.orders.svc.cluster.local 100
      outbound|80|v2|orders.orders.svc.cluster.local 0
      # 回滚生效时间 = 从 patch 到全部代理 SYNCED,实测 1.2–2.0 秒(240 Pod 规模)

      # 4) 回滚之后确认 v2 不再接流量(用 100 个请求验证)
      $ for i in $(seq 1 100); do
          curl -s -o /dev/null -w "%{http_code}\n" http://orders.orders.svc.cluster.local/
        done | sort | uniq -c
          100 200
      $ curl -s localhost:9090/api/v1/query --data-urlencode \
          'query=sum(rate(istio_requests_total{destination_workload="orders",destination_version="v2",reporter="destination"}[1m]))' \
          | jq -r '.data.result[].value[1]'
      0
      # v2 的请求速率归零 = 回滚完成

      # 5) 版本本身有问题时,回滚要同时处理 Deployment 层
      $ kubectl -n orders rollout undo deploy/orders-v1
      $ kubectl -n orders rollout status deploy/orders-v1

预期与判读:第 3 步是最有价值的一步。灰度演练的目标不是「发布成功」,而是「能在 2 秒内退回去」。回滚的判据有三个:权重回到 100/0、v2 的请求速率归零、监控上错误率在 1 分钟内回到基线。三个都拿到才算回滚完成——只把 YAML 改了不算。

案例:5% 的灰度跑了一小时,样本只有 312 个请求

一次订单服务灰度,配置写的是 5%,观察了整整一小时,页面上的错误率是 0%,结论写着「灰度通过,全量发布」。全量后 18 分钟,v2 的 5xx 率涨到 8.4%,回滚。

复盘时发现两件事:第一,灰度期间 v2 的样本累计只有 312 个请求(0% 的错误率在 312 个样本上的置信区间宽到毫无意义);第二,这 312 个请求全部来自同一个来源——调用方是 gRPC 长连接,60 个 Pod 每个只建了 1 条连接,其中 1 条被分到 v2,于是这个客户端承担了全部灰度流量,它恰好是内部测试工具,走的都是简单查询路径,根本触发不了出问题的那条分支。

整改三点:判据从「错误率 0%」改成「≥1000 个请求且来自 ≥10 个不同 source_workload」;用 sum by (source_workload) (increase(istio_requests_total{destination_version="v2"}[10m])) 检查样本来源的多样性;给 gRPC 服务加 waypoint 让权重按请求生效。「灰度通过了」这句话必须有样本量和来源分布支撑,否则它只是一句感受。

服务网格与网关 · 第 11 章 金丝雀实操:用网格做 5% 灰度,看指标后全量或回滚
模块目录 · 返回学习路径