使用 Argo Rollouts 实现金丝雀发布,10% 流量实验 5 分钟自动提升

解读

国内云原生面试中,这道题考察的是“能不能把 Node.js 服务无损、可观测、可回滚地灰度上线”。
面试官真正想听的是:

  1. 你熟悉 Argo Rollouts 的 CRD 模型,能用 YAML 把“10% / 5 min”翻译成 steps;
  2. 你能在 Node.js 侧配合:镜像多版本、健康检查、指标暴露、优雅退出;
  3. 你知道国内主流环境(阿里云 ACK、腾讯云 TKE、华为云 CCE)的 Ingress 或 Service Mesh 怎么接 Rollouts;
  4. 你清楚自动提升背后的“坑”:Prometheus 指标阈值、回滚窗口、Istio 版本冲突、镜像拉取限速。
    回答时先给“能跑起来的最小 YAML”,再讲“生产级加固”,最后补“观测与回滚”。

知识点

  1. Argo Rollouts CRD:Rollout、AnalysisTemplate、Experiment、Promotion。
  2. 流量分割方式:
    • 基于 Ingress-NGINX Canary 注解(国内最常用,云厂商都支持);
    • 基于 Istio/ASM 虚拟服务(高端场景,需网格授权);
    • 基于 Gateway API(新集群趋势)。
  3. Node.js 容器化要点:
    • 双阶段构建,distroless 或 18-alpine,减少 CVE;
    • 启动使用 node --experimental-loader 时,记得把 --max-old-space-size 显式写出,防止 OOM 被 Kill 误判;
    • 健康检查一定要暴露 /healthz,返回 200 且带 git-commit-sha,方便 Rollouts 做版本校验;
    • 优雅退出:监听 SIGTERM,关闭 server、断开 DB 连接,超时时间 <30s,与 terminationGracePeriodSeconds 对齐。
  4. 自动提升与回滚:
    • AnalysisTemplate 里用 prometheus 插件查 p99<500ms 且错误率<1%;
    • 国内集群多数开 kube-prometheus-stack,指标标签 service=canary;
    • 5 分钟是“最短等待”,不是“绝对通过”,需设置 abortScaleDownDelay: 30s 防止错版持续跑;
    • 给重要业务加人工卡点:设置 pause: {duration: 0} 并配合 Argo CD SyncWave,让 SRE 在钉钉审批后再点 promote。
  5. 合规与网络:
    • 国内镜像仓库需用 VPC 内网域名(如 registry-vpc.cn-hangzhou.aliyuncs.com),否则节点拉镜像走公网会被限速;
    • 若集群开 SLB 带宽限速,金丝雀 10% 流量可能突发 5 倍 QPS,需提前把 Pod 副本数算进 HPA 缓冲。

答案

  1. 目录结构
canary/
├── rollout.yaml
├── analysis.yaml
├── service.yaml
├── ingress.yaml
└── Dockerfile
  1. Node.js Dockerfile(生产示例)
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev && npm cache clean --force
COPY . .
RUN npm run build   # 若有 ts 编译

FROM node:18-alpine
ENV NODE_ENV=production
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package.json ./
EXPOSE 3000
# 健康检查
HEALTHCHECK --interval=5s --timeout=3s --start-period=10s \
  CMD node -e "http.get('http://localhost:3000/healthz',(r)=>r.statusCode===200?process.exit(0):process.exit(1))"
# 优雅退出
STOPSIGNAL SIGTERM
CMD ["node","dist/index.js"]
  1. rollout.yaml(核心,10% 5 分钟自动提升)
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
  name: node-app
  namespace: demo
spec:
  replicas: 10
  strategy:
    canary:
      stableService: node-app-stable
      canaryService: node-app-canary
      trafficRouting:
        nginx:
          stableIngress: node-app-ingress   # 指向集群已有的 Ingress
      steps:
      - setWeight: 10
      - pause: {duration: 5m}               # 等待 5 分钟
      - analysis:
          templates:
          - templateName: node-app-metrics
          args:
          - name: service
            value: node-app-canary
      - setWeight: 100
      - pause: {duration: 0}                # 人工卡点,可钉钉审批
  selector:
    matchLabels:
      app: node-app
  template:
    metadata:
      labels:
        app: node-app
    spec:
      terminationGracePeriodSeconds: 30
      containers:
      - name: node
        image: registry-vpc.cn-hangzhou.aliyuncs.com/abc/node-app:__VERSION__  # CI 替换
        ports:
        - containerPort: 3000
          name: http
        livenessProbe:
          httpGet: {path: /healthz, port: http}
          initialDelaySeconds: 10
        readinessProbe:
          httpGet: {path: /ready, port: http}
          initialDelaySeconds: 5
        resources:
          requests: {cpu: "200m", memory: "256Mi"}
          limits:   {cpu: "500m", memory: "512Mi"}
        lifecycle:
          preStop:
            exec:
              command: ["/bin/sh","-c","sleep 10"]  # 给 LB 摘流时间
  1. analysis.yaml
apiVersion: argoproj.io/v1alpha1
kind: AnalysisTemplate
metadata:
  name: node-app-metrics
spec:
  args:
  - name: service
  metrics:
  - name: p99-latency
    interval: 30s
    count: 6
    successCondition: result[0] <= 500
    provider:
      prometheus:
        address: http://kube-prometheus-stack-prometheus.monitoring.svc.cluster.local:9090
        query: |
          histogram_quantile(0.99,
            sum(rate(http_request_duration_seconds_bucket{service="{{args.service}}"}[1m])) by (le)
          )
  - name: error-rate
    interval: 30s
    count: 6
    successCondition: result[0] <= 0.01
    provider:
      prometheus:
        query: |
          sum(rate(http_requests_total{service="{{args.service}}",code=~"5.."}[1m])) /
          sum(rate(http_requests_total{service="{{args.service}}"}[1m]))
  1. 一键发布脚本(CI 片段,GitLab CI 为例)
# 变量 $CI_COMMIT_SHORT_SHA 已注入
sed "s/__VERSION__/${CI_COMMIT_SHORT_SHA}/" rollout.yaml | kubectl apply -f -
kubectl argo rollouts promote node-app -n demo  # 若需要人工审批,这步留到审批后执行
  1. 回滚演示
kubectl argo rollouts undo node-app -n demo --to-revision=2

Rollouts 会立即把权重切回 0%,并重建旧版本 Pod,保证 30s 内流量无损。

拓展思考

  1. 多地域灰度:华东集群 10% 流量通过后,再自动触发华南集群灰度,可用 Argo CD ApplicationSet + Rollout 的 clusterDecisionResource 实现“地域泳道”。
  2. Session 亲和:Node.js 服务若带登录态,10% 金丝雀需保证同一用户始终打到同一版本,可在 Ingress-NGINX 加 nginx.ingress.kubernetes.io/canary-by-cookie: "version=canary",让测试人员手动带 Cookie 验证。
  3. 镜像加速:国内节点可预先在 DaemonSet 里跑 Dragonfly P2P,把镜像预热到本地,避免 10% 突发时拉镜像造成 Pod 启动慢、HPA 误判。
  4. 混合发布:对写流量敏感业务,可把“流量灰度”改成“用户灰度”——利用 Argo Rollouts Experiment,同时拉起 stable/canary 两个 Deployment,通过用户 ID 分片写不同数据库,读流量再统一,做到“读写双重保险”。