1
---
2
name: istio-traffic-management
3
description: Configure Istio traffic management including routing, load balancing, circuit breakers, and canary deployments. Use when implementing service mesh traffic policies, progressive delivery, or resilience patterns.
4
---
5
6
# Istio Traffic Management
7
8
Comprehensive guide to Istio traffic management for production service mesh deployments.
9
10
## When to Use This Skill
11
12
- Configuring service-to-service routing
13
- Implementing canary or blue-green deployments
14
- Setting up circuit breakers and retries
15
- Load balancing configuration
16
- Traffic mirroring for testing
17
- Fault injection for chaos engineering
18
19
## Core Concepts
20
21
### 1. Traffic Management Resources
22
23
| Resource | Purpose | Scope |
24
| ------------------- | ----------------------------- | ------------- |
25
| **VirtualService** | Route traffic to destinations | Host-based |
26
| **DestinationRule** | Define policies after routing | Service-based |
27
| **Gateway** | Configure ingress/egress | Cluster edge |
28
| **ServiceEntry** | Add external services | Mesh-wide |
29
30
### 2. Traffic Flow
31
32
```
33
Client → Gateway → VirtualService → DestinationRule → Service
34
(routing) (policies) (pods)
35
```
36
37
## Templates
38
39
### Template 1: Basic Routing
40
41
```yaml
42
apiVersion: networking.istio.io/v1beta1
43
kind: VirtualService
44
metadata:
45
name: reviews-route
46
namespace: bookinfo
47
spec:
48
hosts:
49
- reviews
50
http:
51
- match:
52
- headers:
53
end-user:
54
exact: jason
55
route:
56
- destination:
57
host: reviews
58
subset: v2
59
- route:
60
- destination:
61
host: reviews
62
subset: v1
63
---
64
apiVersion: networking.istio.io/v1beta1
65
kind: DestinationRule
66
metadata:
67
name: reviews-destination
68
namespace: bookinfo
69
spec:
70
host: reviews
71
subsets:
72
- name: v1
73
labels:
74
version: v1
75
- name: v2
76
labels:
77
version: v2
78
- name: v3
79
labels:
80
version: v3
81
```
82
83
### Template 2: Canary Deployment
84
85
```yaml
86
apiVersion: networking.istio.io/v1beta1
87
kind: VirtualService
88
metadata:
89
name: my-service-canary
90
spec:
91
hosts:
92
- my-service
93
http:
94
- route:
95
- destination:
96
host: my-service
97
subset: stable
98
weight: 90
99
- destination:
100
host: my-service
101
subset: canary
102
weight: 10
103
---
104
apiVersion: networking.istio.io/v1beta1
105
kind: DestinationRule
106
metadata:
107
name: my-service-dr
108
spec:
109
host: my-service
110
trafficPolicy:
111
connectionPool:
112
tcp:
113
maxConnections: 100
114
http:
115
h2UpgradePolicy: UPGRADE
116
http1MaxPendingRequests: 100
117
http2MaxRequests: 1000
118
subsets:
119
- name: stable
120
labels:
121
version: stable
122
- name: canary
123
labels:
124
version: canary
125
```
126
127
### Template 3: Circuit Breaker
128
129
```yaml
130
apiVersion: networking.istio.io/v1beta1
131
kind: DestinationRule
132
metadata:
133
name: circuit-breaker
134
spec:
135
host: my-service
136
trafficPolicy:
137
connectionPool:
138
tcp:
139
maxConnections: 100
140
http:
141
http1MaxPendingRequests: 100
142
http2MaxRequests: 1000
143
maxRequestsPerConnection: 10
144
maxRetries: 3
145
outlierDetection:
146
consecutive5xxErrors: 5
147
interval: 30s
148
baseEjectionTime: 30s
149
maxEjectionPercent: 50
150
minHealthPercent: 30
151
```
152
153
### Template 4: Retry and Timeout
154
155
```yaml
156
apiVersion: networking.istio.io/v1beta1
157
kind: VirtualService
158
metadata:
159
name: ratings-retry
160
spec:
161
hosts:
162
- ratings
163
http:
164
- route:
165
- destination:
166
host: ratings
167
timeout: 10s
168
retries:
169
attempts: 3
170
perTryTimeout: 3s
171
retryOn: connect-failure,refused-stream,unavailable,cancelled,retriable-4xx,503
172
retryRemoteLocalities: true
173
```
174
175
### Template 5: Traffic Mirroring
176
177
```yaml
178
apiVersion: networking.istio.io/v1beta1
179
kind: VirtualService
180
metadata:
181
name: mirror-traffic
182
spec:
183
hosts:
184
- my-service
185
http:
186
- route:
187
- destination:
188
host: my-service
189
subset: v1
190
mirror:
191
host: my-service
192
subset: v2
193
mirrorPercentage:
194
value: 100.0
195
```
196
197
### Template 6: Fault Injection
198
199
```yaml
200
apiVersion: networking.istio.io/v1beta1
201
kind: VirtualService
202
metadata:
203
name: fault-injection
204
spec:
205
hosts:
206
- ratings
207
http:
208
- fault:
209
delay:
210
percentage:
211
value: 10
212
fixedDelay: 5s
213
abort:
214
percentage:
215
value: 5
216
httpStatus: 503
217
route:
218
- destination:
219
host: ratings
220
```
221
222
### Template 7: Ingress Gateway
223
224
```yaml
225
apiVersion: networking.istio.io/v1beta1
226
kind: Gateway
227
metadata:
228
name: my-gateway
229
spec:
230
selector:
231
istio: ingressgateway
232
servers:
233
- port:
234
number: 443
235
name: https
236
protocol: HTTPS
237
tls:
238
mode: SIMPLE
239
credentialName: my-tls-secret
240
hosts:
241
- "*.example.com"
242
---
243
apiVersion: networking.istio.io/v1beta1
244
kind: VirtualService
245
metadata:
246
name: my-vs
247
spec:
248
hosts:
249
- "api.example.com"
250
gateways:
251
- my-gateway
252
http:
253
- match:
254
- uri:
255
prefix: /api/v1
256
route:
257
- destination:
258
host: api-service
259
port:
260
number: 8080
261
```
262
263
## Load Balancing Strategies
264
265
```yaml
266
apiVersion: networking.istio.io/v1beta1
267
kind: DestinationRule
268
metadata:
269
name: load-balancing
270
spec:
271
host: my-service
272
trafficPolicy:
273
loadBalancer:
274
simple: ROUND_ROBIN # or LEAST_CONN, RANDOM, PASSTHROUGH
275
---
276
# Consistent hashing for sticky sessions
277
apiVersion: networking.istio.io/v1beta1
278
kind: DestinationRule
279
metadata:
280
name: sticky-sessions
281
spec:
282
host: my-service
283
trafficPolicy:
284
loadBalancer:
285
consistentHash:
286
httpHeaderName: x-user-id
287
# or: httpCookie, useSourceIp, httpQueryParameterName
288
```
289
290
## Best Practices
291
292
### Do's
293
294
- **Start simple** - Add complexity incrementally
295
- **Use subsets** - Version your services clearly
296
- **Set timeouts** - Always configure reasonable timeouts
297
- **Enable retries** - But with backoff and limits
298
- **Monitor** - Use Kiali and Jaeger for visibility
299
300
### Don'ts
301
302
- **Don't over-retry** - Can cause cascading failures
303
- **Don't ignore outlier detection** - Enable circuit breakers
304
- **Don't mirror to production** - Mirror to test environments
305
- **Don't skip canary** - Test with small traffic percentage first
306
307
## Debugging Commands
308
309
```bash
310
# Check VirtualService configuration
311
istioctl analyze
312
313
# View effective routes
314
istioctl proxy-config routes deploy/my-app -o json
315
316
# Check endpoint discovery
317
istioctl proxy-config endpoints deploy/my-app
318
319
# Debug traffic
320
istioctl proxy-config log deploy/my-app --level debug
321
```
322