关于 istio-traffic-management
这是用于配置 Istio 服务网格流量管理的实用技能,面向处理服务间流量的运维人员,涵盖路由、部署策略、弹性策略、流量测试和验证步骤。
主要功能
- 01VirtualService 与 DestinationRule 模式
- 02金丝雀与蓝绿路由
- 03重试、超时与断路器
- 04流量镜像与故障注入
- 05Gateway 与负载均衡配置
- 06istioctl 验证与调试
适用场景
- 01在服务版本之间路由流量
- 02以受控流量测试金丝雀发布
- 03配置重试和断路器
- 04在测试环境镜像流量或注入故障
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill istio-traffic-management安装后在 Claude Code 中输入/istio-traffic-management确认已加载。
详细介绍
Istio 流量管理
面向生产服务网格部署的 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
资源
限制
- 仅在任务明确符合上述范围时使用此技能。
- 不要将输出视为针对特定环境进行验证、测试或专家审查的替代品。
- 如果缺少所需输入、权限、安全边界或成功标准,请暂停并请求澄清。
应用模板前,请验证已安装的 CRD/API 版本、命名空间、子集标签和网关密钥的作用范围,然后运行可用的架构/分析检查,并部署有界金丝雀版本,同时准备回滚方案。故障注入和调试日志记录需要在获授权的环境中进行,并限定持续时间。流量镜像会复制请求:请隔离镜像目标中的写操作和通知,编辑敏感数据,并从低于 100% 的流量比例开始。仅对语义上可安全重放的操作进行重试。
评论
支持 Markdown