金丝雀发布——VirtualService 流量分割

知识库
知识库文档
/tech-stacks/istio/examples/金丝雀发布——VirtualService 流量分割.md

文档

Istio 例程:基于权重的金丝雀发布

目标

使用 Istio VirtualService + DestinationRule 实现 v1(80%) + v2(20%) 流量分割。

前置:部署两个版本

# app-v1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-v1
  labels:
    app: myapp
    version: v1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      version: v1
  template:
    metadata:
      labels:
        app: myapp
        version: v1
    spec:
      containers:
      - name: app
        image: hashicorp/http-echo
        args:
        - "-text=Hello from v1.0.0"
        - "-listen=:8080"
        ports:
        - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  selector:
    app: myapp
  ports:
  - port: 80
    targetPort: 8080
    name: http
# app-v2.yaml(同上,改 version: v2 和 -text="Hello from v2.0.0")

Istio 流量规则

# istio-routes.yaml
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: myapp-dr
spec:
  host: myapp
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: myapp-vs
spec:
  hosts:
  - myapp
  http:
  - route:
    - destination:
        host: myapp
        subset: v1
      weight: 80
    - destination:
        host: myapp
        subset: v2
      weight: 20

部署与验证

# 1. 部署应用
kubectl apply -f app-v1.yaml -f app-v2.yaml

# 2. 应用 Istio 规则
kubectl apply -f istio-routes.yaml

# 3. 测试流量分配
kubectl run -it --rm test --image=curlimages/curl --restart=Never -- sh
# 在 Pod 内执行:
for i in $(seq 1 20); do curl -s http://myapp; echo; done

# 预期输出:
# Hello from v1.0.0  (约 16 次)
# Hello from v2.0.0  (约 4 次)

进阶:基于 Header 的 A/B 测试

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: myapp-ab
spec:
  hosts:
  - myapp
  http:
  - match:
    - headers:
        x-canary:
          exact: "true"
    route:
    - destination:
        host: myapp
        subset: v2
  - route:
    - destination:
        host: myapp
        subset: v1

测试:

curl -H "x-canary: true" http://myapp   # → v2.0.0
curl http://myapp                        # → v1.0.0

核心资源关系

Gateway(入口)
  └── VirtualService(路由规则)
        └── DestinationRule(目标策略)
              ├── subset v1(灰度)
              └── subset v2(正式)

信息

路径
/tech-stacks/istio/examples/金丝雀发布——VirtualService 流量分割.md
更新时间
2026/5/31