关于 istio-traffic-management
面向生产环境 Istio 服务网格流量配置的指南,适用于管理路由与韧性的团队。内容包括资源说明、YAML 模板、最佳实践和调试命令。
主要功能
- 01VirtualService 与 DestinationRule 指南
- 02入口及外部服务配置
- 03金丝雀与蓝绿部署模式
- 04重试、超时和熔断器
- 05流量镜像与故障注入模板
- 06istioctl 调试命令
适用场景
- 01配置服务间流量路由
- 02测试金丝雀或蓝绿发布
- 03设置重试和熔断器
- 04为测试镜像流量或注入故障
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ wshobson/ agents --skill istio-traffic-management安装后在 Claude Code 中输入/istio-traffic-management确认已加载。
详细介绍
Istio 流量管理
面向生产服务网格部署的 Istio 流量管理综合指南。
何时使用此技能
- 配置服务到服务的路由
- 实施金丝雀或蓝绿部署
- 设置熔断器和重试
- 负载均衡配置
- 用于测试的流量镜像
- 用于混沌工程的故障注入
核心概念
1. 流量管理资源
| 资源 | 用途 | 作用范围 |
|---|---|---|
| VirtualService | 将流量路由到目标 | 基于主机 |
| DestinationRule | 在路由后定义策略 | 基于服务 |
| Gateway | 配置入口/出口 | 集群边缘 |
| ServiceEntry | 添加外部服务 | 网格范围 |
2. 流量流向
Client → Gateway → VirtualService → DestinationRule → Service
(routing) (policies) (pods)
模板
模板 1:基本路由
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: reviews-route
namespace: bookinfo
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: jason
route:
- destination:
host: reviews
subset: v2
- route:
- destination:
host: reviews
subset: v1
---
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: reviews-destination
namespace: bookinfo
spec:
host: reviews
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
模板 2:金丝雀部署
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: my-service-canary
spec:
hosts:
- my-service
http:
- route:
- destination:
host: my-service
subset: stable
weight: 90
- destination:
host: my-service
subset: canary
weight: 10
---
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: my-service-dr
spec:
host: my-service
trafficPolicy:
connectionPool:
tcp:
maxConnections: 100
http:
h2UpgradePolicy: UPGRADE
http1MaxPendingRequests: 100
http2MaxRequests: 1000
subsets:
- name: stable
labels:
version: stable
- name: canary
labels:
version: canary
模板 3:熔断器
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: circuit-breaker
spec:
host: my-service
trafficPolicy:
connectionPool:
tcp:
maxConnections: 100
http:
http1MaxPendingRequests: 100
http2MaxRequests: 1000
maxRequestsPerConnection: 10
maxRetries: 3
outlierDetection:
consecutive5xxErrors: 5
interval: 30s
baseEjectionTime: 30s
maxEjectionPercent: 50
minHealthPercent: 30
模板 4:重试与超时
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: ratings-retry
spec:
hosts:
- ratings
http:
- route:
- destination:
host: ratings
timeout: 10s
retries:
attempts: 3
perTryTimeout: 3s
retryOn: connect-failure,refused-stream,unavailable,cancelled,retriable-4xx,503
retryRemoteLocalities: true
模板 5:流量镜像
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: mirror-traffic
spec:
hosts:
- my-service
http:
- route:
- destination:
host: my-service
subset: v1
mirror:
host: my-service
subset: v2
mirrorPercentage:
value: 100.0
模板 6:故障注入
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: fault-injection
spec:
hosts:
- ratings
http:
- fault:
delay:
percentage:
value: 10
fixedDelay: 5s
abort:
percentage:
value: 5
httpStatus: 503
route:
- destination:
host: ratings
模板 7:入口网关
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: my-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 443
name: https
protocol: HTTPS
tls:
mode: SIMPLE
credentialName: my-tls-secret
hosts:
- "*.example.com"
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: my-vs
spec:
hosts:
- "api.example.com"
gateways:
- my-gateway
http:
- match:
- uri:
prefix: /api/v1
route:
- destination:
host: api-service
port:
number: 8080
负载均衡策略
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: load-balancing
spec:
host: my-service
trafficPolicy:
loadBalancer:
simple: ROUND_ROBIN # or LEAST_CONN, RANDOM, PASSTHROUGH
---
# Consistent hashing for sticky sessions
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: sticky-sessions
spec:
host: my-service
trafficPolicy:
loadBalancer:
consistentHash:
httpHeaderName: x-user-id
# or: httpCookie, useSourceIp, httpQueryParameterName
最佳实践
应做
- 从简单开始 - 逐步增加复杂性
- 使用子集 - 清晰地版本化你的服务
- 设置超时 - 始终配置合理的超时
- 启用重试 - 但要有退避和限制
- 监控 - 使用 Kiali 和 Jaeger 获得可见性
不应做
- 不要过度重试 - 可能导致级联故障
- 不要忽略异常值检测 - 启用熔断器
- 不要镜像到生产环境 - 镜像到测试环境
- 不要跳过金丝雀 - 先用小流量百分比进行测试
调试命令
# Check VirtualService configuration
istioctl analyze
# View effective routes
istioctl proxy-config routes deploy/my-app -o json
# Check endpoint discovery
istioctl proxy-config endpoints deploy/my-app
# Debug traffic
istioctl proxy-config log deploy/my-app --level debug
评论
支持 Markdown