流量分割
Envoy 提供强大的流量分割功能,支持多种流量控制策略。
权重路由
基本权重配置
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/"
route:
weighted_clusters:
clusters:
- name: service_v1
weight: 80
- name: service_v2
weight: 20
运行时权重配置
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/"
route:
weighted_clusters:
runtime_key_prefix: routing.example_service
clusters:
- name: service_v1
weight: 80
- name: service_v2
weight: 20
金丝雀发布
基于权重的金丝雀发布
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/"
route:
weighted_clusters:
clusters:
- name: stable_service
weight: 90
- name: canary_service
weight: 10
基于请求头的金丝雀发布
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/"
headers:
- name: "x-canary"
string_match:
exact: "true"
route:
cluster: canary_service
- match:
prefix: "/"
route:
cluster: stable_service
A/B 测试
基于用户 ID 的 A/B 测试
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/"
headers:
- name: "x-user-id"
string_match:
safe_regex:
google_re2: {}
regex: ".*[02468]$" # 偶数用户 ID
route:
cluster: variant_a_service
- match:
prefix: "/"
headers:
- name: "x-user-id"
string_match:
safe_regex:
google_re2: {}
regex: ".*[13579]$" # 奇数用户 ID
route:
cluster: variant_b_service
- match:
prefix: "/"
route:
cluster: default_service
基于百分比的 A/B 测试
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/"
runtime_fraction:
default_value:
numerator: 50
denominator: HUNDRED
runtime_key: routing.ab_test
route:
cluster: variant_a_service
- match:
prefix: "/"
route:
cluster: variant_b_service
蓝绿部署
基于权重的蓝绿切换
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/"
route:
weighted_clusters:
clusters:
- name: blue_service
weight: 100
- name: green_service
weight: 0
基于路由的蓝绿切换
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/blue"
route:
cluster: blue_service
prefix_rewrite: "/"
- match:
prefix: "/green"
route:
cluster: green_service
prefix_rewrite: "/"
- match:
prefix: "/"
route:
cluster: blue_service
流量镜像
请求镜像配置
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/"
route:
cluster: primary_service
request_mirror_policies:
- cluster: mirror_service
runtime_fraction:
default_value:
numerator: 10
denominator: HUNDRED
多镜像配置
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/"
route:
cluster: primary_service
request_mirror_policies:
- cluster: mirror_service_1
runtime_fraction:
default_value:
numerator: 10
denominator: HUNDRED
- cluster: mirror_service_2
runtime_fraction:
default_value:
numerator: 5
denominator: HUNDRED
基于条件的路由
基于时间的路由
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/"
headers:
- name: "x-time-window"
string_match:
exact: "peak"
route:
cluster: high_capacity_service
- match:
prefix: "/"
route:
cluster: normal_service
基于地理位置的路由
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/"
headers:
- name: "x-region"
string_match:
exact: "us-west"
route:
cluster: us_west_service
- match:
prefix: "/"
headers:
- name: "x-region"
string_match:
exact: "us-east"
route:
cluster: us_east_service
- match:
prefix: "/"
route:
cluster: default_service
流量控制策略
渐进式流量切换
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/"
runtime_fraction:
default_value:
numerator: 5
denominator: HUNDRED
runtime_key: routing.progressive_rollout
route:
cluster: new_service
- match:
prefix: "/"
route:
cluster: old_service
基于性能的路由
route_config:
virtual_hosts:
- name: example_vhost
domains: ["*"]
routes:
- match:
prefix: "/"
headers:
- name: "x-performance-tier"
string_match:
exact: "high"
route:
cluster: premium_service
- match:
prefix: "/"
route:
cluster: standard_service
最佳实践
1. 流量分割策略
- 使用权重路由进行渐进式发布
- 结合请求头进行精确控制
- 实施流量镜像进行测试
- 监控分割效果
2. 监控和调试
- 监控各版本的服务指标
- 设置告警阈值
- 记录流量分割日志
- 定期审查分割策略
3. 安全考虑
- 确保镜像流量不影响生产
- 验证新版本的安全性
- 实施回滚机制
- 监控异常流量
注意事项
- 流量分割会影响系统性能
- 需要确保各版本服务兼容性
- 配置变更需要谨慎测试
- 需要监控分割效果
流量分割为 Envoy 提供了强大的流量控制能力,合理使用可以实现复杂的部署策略。