Service Mesh 流量管理实战 - 金丝雀发布、蓝绿部署

前言

流量管理是 Service Mesh 的核心功能之一。本文详细介绍如何使用 Service Mesh 实现金丝雀发布、蓝绿部署、A/B 测试等高级流量管理策略。

一、金丝雀发布

1.1 什么是金丝雀发布

金丝雀发布是一种渐进式发布策略,先将少量流量导入新版本,验证稳定后再逐步扩大流量比例。

1.2 Istio 实现金丝雀发布

# 第 1 步:部署 v1 和 v2 版本
apiVersion: apps/v1
kind: Deployment
metadata:
  name: reviews-v1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: reviews
      version: v1
  template:
    metadata:
      labels:
        app: reviews
        version: v1
    spec:
      containers:
      - name: reviews
        image: reviews:v1

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: reviews-v2
spec:
  replicas: 3
  selector:
    matchLabels:
      app: reviews
      version: v2
  template:
    metadata:
      labels:
        app: reviews
        version: v2
    spec:
      containers:
      - name: reviews
        image: reviews:v2

# 第 2 步:配置 DestinationRule
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: reviews
spec:
  host: reviews
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2

# 第 3 步:配置 VirtualService(90% v1, 10% v2)
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
  - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
      weight: 90
    - destination:
        host: reviews
        subset: v2
      weight: 10

# 第 4 步:逐步调整流量比例
# 观察稳定后,调整为 50/50
# 最后调整为 0/100,完成发布

二、蓝绿部署

2.1 蓝绿部署原理

蓝绿部署同时运行两个完全相同的版本,通过切换流量实现快速发布和回滚。

2.2 Istio 实现蓝绿部署

# 初始状态:100% 流量到蓝版本(v1)
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: myapp
spec:
  hosts:
  - myapp
  http:
  - route:
    - destination:
        host: myapp
        subset: blue
      weight: 100

# 切换流量:100% 到绿版本(v2)
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: myapp
spec:
  hosts:
  - myapp
  http:
  - route:
    - destination:
        host: myapp
        subset: green
      weight: 100

# 快速回滚:切换回蓝版本
kubectl apply -f virtualservice-blue.yaml

三、A/B 测试

3.1 基于 Header 的 A/B 测试

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: ab-test
spec:
  hosts:
  - myapp
  http:
  # 匹配特定 Header,路由到 v2(测试版本)
  - match:
    - headers:
        x-user-type:
          exact: test
    route:
    - destination:
        host: myapp
        subset: v2
  
  # 其他流量路由到 v1(生产版本)
  - route:
    - destination:
        host: myapp
        subset: v1
      weight: 80
    - destination:
        host: myapp
        subset: v2
      weight: 20

3.2 基于用户的 A/B 测试

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: user-ab-test
spec:
  hosts:
  - myapp
  http:
  - match:
    - headers:
        cookie:
          regex: "^(.*?;)?(user-type=test)(;.*)?$"
    route:
    - destination:
        host: myapp
        subset: v2
  
  - route:
    - destination:
        host: myapp
        subset: v1

四、故障注入

4.1 延迟注入

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: fault-test
spec:
  hosts:
  - myapp
  http:
  - fault:
      delay:
        percentage:
          value: 10.0
        fixedDelay: 5s
    route:
    - destination:
        host: myapp
        subset: v1

4.2 中断注入

apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: abort-test
spec:
  hosts:
  - myapp
  http:
  - fault:
      abort:
        percentage:
          value: 5.0
        httpStatus: 500
    route:
    - destination:
        host: myapp
        subset: v1

五、熔断和限流

5.1 配置熔断

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: circuit-breaker
spec:
  host: myapp
  trafficPolicy:
    outlierDetection:
      consecutive5xxErrors: 5
      interval: 30s
      baseEjectionTime: 30s
      maxEjectionPercent: 50

5.2 配置限流

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: rate-limit
spec:
  host: myapp
  trafficPolicy:
    connectionPool:
      tcp:
        maxConnections: 100
      http:
        http1MaxPendingRequests: 100
        http2MaxRequests: 1000
        maxRequestsPerConnection: 10

总结

通过 Service Mesh 的流量管理能力,可以轻松实现金丝雀发布、蓝绿部署、A/B 测试等高级发布策略,提升发布的可靠性和灵活性。


Service Mesh 系列文章 4/10

发表回复

后才能评论