第三章:流量管理¶
Istio 的流量管理功能是其核心能力之一,通过 VirtualService、DestinationRule 等资源实现精细化的流量控制。
流量管理资源¶
VirtualService¶
定义请求如何路由到服务:
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
DestinationRule¶
定义目标服务的策略和子集:
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
trafficPolicy:
connectionPool:
tcp:
maxConnections: 100
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
Gateway¶
控制网格边界流量:
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: bookinfo-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "bookinfo.example.com"
请求路由¶
基于版本的路由¶
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
weight: 100
基于请求头的路由¶
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: jason
route:
- destination:
host: reviews
subset: v2
- route:
- destination:
host: reviews
subset: v1
基于 URL 的路由¶
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: productpage
spec:
hosts:
- productpage
http:
- match:
- uri:
prefix: /api/v1
route:
- destination:
host: productpage-v1
- match:
- uri:
prefix: /api/v2
route:
- destination:
host: productpage-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
渐进式发布流程¶
# 第一阶段:10% 流量到 v2
weight: 90 (v1) / 10 (v2)
# 第二阶段:30% 流量到 v2
weight: 70 (v1) / 30 (v2)
# 第三阶段:50% 流量到 v2
weight: 50 (v1) / 50 (v2)
# 第四阶段:100% 流量到 v2
weight: 0 (v1) / 100 (v2)
自动化金丝雀发布(Flagger)¶
apiVersion: flagger.app/v1beta1
kind: Canary
metadata:
name: reviews
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: reviews
progressDeadlineSeconds: 600
service:
port: 9080
analysis:
interval: 1m
threshold: 5
maxWeight: 50
stepWeight: 10
metrics:
- name: request-success-rate
thresholdRange:
min: 99
interval: 1m
- name: request-duration
thresholdRange:
max: 500
interval: 1m
故障注入¶
延迟注入¶
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- fault:
delay:
percentage:
value: 100
fixedDelay: 7s
route:
- destination:
host: reviews
subset: v1
中止注入¶
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- fault:
abort:
percentage:
value: 50
httpStatus: 500
route:
- destination:
host: reviews
subset: v1
超时与重试¶
设置超时¶
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v2
timeout: 10s
配置重试¶
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v2
retries:
attempts: 3
perTryTimeout: 2s
retryOn: gateway-error,connect-failure,refused-stream
熔断器¶
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: reviews
spec:
host: reviews
trafficPolicy:
connectionPool:
tcp:
maxConnections: 100
http:
h2UpgradePolicy: UPGRADE
http1MaxPendingRequests: 100
http2MaxRequests: 1000
outlierDetection:
consecutive5xxErrors: 5
interval: 30s
baseEjectionTime: 30s
maxEjectionPercent: 50
流量镜像¶
将生产流量复制到测试环境:
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: reviews
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
weight: 100
mirror:
host: reviews
subset: v2
mirrorPercentage:
value: 100
TCP 路由¶
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: tcp-echo
spec:
hosts:
- tcp-echo
tcp:
- match:
- port: 31400
route:
- destination:
host: tcp-echo
port:
number: 9000
小结¶
Istio 的流量管理功能非常强大,本章介绍了:
- 请求路由:基于版本、Header、URL 的路由
- 金丝雀发布:渐进式灰度发布
- 故障注入:测试系统韧性
- 超时重试:提高系统可靠性
- 熔断器:防止级联故障
- 流量镜像:安全测试新版本
下一章我们将学习 Istio 的安全功能。