如何在 Kubernetes 上通过 Istio 按 5% 流量灰度新模型?

解读

面试官想确认三件事:

  1. 你是否真正在生产环境用过 Kubernetes + Istio,而不是只跑过 demo;
  2. 能否把“PyTorch 模型”当成一种长周期、有状态、可热更新的微服务来治理;
  3. 对灰度发布核心指标(流量比例、可观测、可回滚)是否有闭环思维。
    国内大厂(阿里、字节、百度)普遍把“5% 灰度”当红线:比例一旦超过 5%,监控告警必须秒级联动,否则 P0 事故。因此答案里必须给出“可观测 + 可回滚”的落地细节,而不是只贴 yaml。

知识点

  1. Kubernetes 工作负载对象:Deployment / StatefulSet 的 label、PodTemplate、revisionHistoryLimit。
  2. Istio 流量治理三件套:VirtualService、DestinationRule、Gateway。
  3. 流量拆分策略:weighted routing + header-based canary + mirror traffic 的区别与组合。
  4. PyTorch 模型服务的容器化要点:
    – 单进程单卡推理时, readinessProbe 必须检测模型文件 md5 与显存占用;
    – 多卡多副本时,Pod 反亲和 + topologySpreadConstraints 防止同一节点 GPU 爆满。
  5. 国内云原生环境差异:
    – 阿里 MSE、腾讯 TCM 对 Istio CRD 有白名单,字段不能随意加;
    – 内部镜像仓库需加 vpc-internal 域名,否则 Istio sidecar 注入会拉镜像失败。
  6. 可观测三板斧:
    – Prometheus 抓 istio_request_total{destination_canary_version="v2"};
    – Grafana 看 P99 延迟、GPU Util、显存碎片;
    – Loki 收集 stderr,正则匹配 “CUDA out of memory” 立即触发 rollback。
  7. 回滚策略:
    – kubectl rollout undo 会重建 Pod,显存重新加载慢;
    – 更优方案是 VirtualService 权重瞬间改 0,老 Deployment 保持热备,秒级回滚。

答案

下面给出一条可直接落地、经过阿里 ACK 生产验证的最小闭环路径,假设模型服务名叫 torch-svc,当前稳定版本 v1,新模型镜像 registry.cn-hangzhou.aliyuncs.com/ai/torchserve:v2。

步骤 1:构建“无侵入”版本标识
在 CI 阶段把 GitCommit 写进容器 ENV:
DOCKER_BUILDKIT=1 docker build --build-arg GIT_COMMIT=$(git rev-parse --short HEAD) -t torchserve:v2 .
这样 Pod 启动后,/proc/1/environ 里自带版本号,方便监控打标签。

步骤 2:部署 v2 副本,但先不接收流量
apiVersion: apps/v1
kind: Deployment
metadata:
name: torch-svc-v2
labels:
app: torch-svc
version: v2
spec:
replicas: 1
selector:
matchLabels:
app: torch-svc
version: v2
template:
metadata:
labels:
app: torch-svc
version: v2
annotations:
sidecar.istio.io/inject: "true"
spec:
containers:
- name: model
image: registry.cn-hangzhou.aliyuncs.com/ai/torchserve:v2
resources:
limits:
nvidia.com/gpu: "1"
readinessProbe:
httpGet:
path: /ping
port: 8080
initialDelaySeconds: 30
periodSeconds: 5
timeoutSeconds: 3
failureThreshold: 2
livenessProbe:
httpGet:
path: /ping
port: 8080
initialDelaySeconds: 60
periodSeconds: 10
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: version
operator: In
values: ["v1"]
topologyKey: kubernetes.io/hostname

步骤 3:Istio 流量规则,精准 5%
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: torch-svc-dr
spec:
host: torch-svc
subsets:

  • name: v1
    labels:
    version: v1
  • name: v2
    labels:
    version: v2

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: torch-svc-vs
spec:
hosts:

  • torch-svc
    http:
  • match:
    • headers:
      canary:
      exact: "true"
      route:
    • destination:
      host: torch-svc
      subset: v2
  • route:
    • destination:
      host: torch-svc
      subset: v1
      weight: 95
    • destination:
      host: torch-svc
      subset: v2
      weight: 5

步骤 4:监控与告警
Prometheus 规则:
groups:

  • name: torch-svc-canary
    interval: 15s
    rules:
    • alert: TorchCanaryHighLatency
      expr: |
      histogram_quantile(0.99,
      sum(rate(istio_request_duration_milliseconds_bucket{destination_service_name="torch-svc",destination_canary_version="v2"}[2m])) by (le))

      200
      for: 1m
      labels:
      severity: critical
      annotations:
      summary: "v2 延迟超 200ms,立即回滚"

步骤 5:灰度验收与回滚
验收通过:逐步把 weight 调到 10→25→50→100,同时把 v1 副本缩到 0。
验收失败:kubectl patch vs torch-svc-vs --type merge -p '{"spec":{"http":[{"route":[{"destination":{"host":"torch-svc","subset":"v1"},"weight":100}]}]}}'
因老 Pod 仍在,无需重新拉镜像,回滚耗时 < 3 秒。

拓展思考

  1. 如果模型需要 A/B 实验而不是简单灰度,可以把“用户 ID 哈希”放在 VirtualService 的 match 里,利用 consistent hash 保证同一用户始终打到同一版本,避免样本污染。
  2. 国内部分金融客户要求“流量双录”,可在 Istio 里启用 envoy.access_log 的 GRPC_ACCESS_LOG,把请求体、响应码、GPU 显存快照一起发到 Kafka,供审计。
  3. 当单次推理耗时 > 500 ms 时,5% 流量也可能把 GPU 利用率瞬间打满,此时可结合 KEDA 的 prometheus 触发器,让 v2 副本数随 P99 延迟自动伸缩,实现“弹性灰度”。