Istio & Service Mesh
Secure, observe, and control every byte of east-west traffic in your Kubernetes cluster. Master Istio mTLS enforcement, progressive delivery via VirtualService weight-shifting, circuit breakers with DestinationRule, Ambient Mesh sidecar-less architecture, and Kiali for real-time traffic topology during incidents.
Lesson objective
- What it teaches
- Istio Architecture · mTLS Zero-Trust Enforcement · Traffic Management: Canary & Circuit Breaker · Production Service Mesh Buildouts 18.3b · Hands-On Labs · Troubleshooting
- Why it matters
- A required lesson for Observability Engineer. Recommended for SRE, Platform Engineer, DevSecOps Engineer, Container/Kubernetes Engineer, DevOps / Platform Architect. See the role paths
- What you demonstrate
-
- Simulated lab: Attacker tries to call payments-api from compromised pod. Recorded as lab evidence.
- Predict 2 results this lesson prints. You mark each one yourself.
- Optional, real environment: 7 hands-on tasks on your own machine or an external lab site (checklist). Not tracked as evidence.
Real Incident: Lateral Movement Contained by mTLS
Scenario: A compromised CI/CD runner had AWS credentials and a valid Kubernetes service account token. Without mTLS, it could call any service in the cluster over HTTP.
With Istio PeerAuthentication STRICT mode:
# Attacker tries to call payments-api from compromised pod attacker$ curl http://payments-api.production.svc.cluster.local/v1/transfer \ -H "Authorization: Bearer stolen-token" \ -d '{"amount": 99999, "to": "attacker-account"}' upstream connect error or disconnect/reset before headers. reset reason: connection failure transport failure reason: TLS error: 268435581... # The connection was REJECTED because the compromised pod's # certificate was not in the payments-api's SPIFFE trust bundle # Istio enforced mTLS — no valid cert, no connection. # Check Istio access logs (what was attempted) $ kubectl logs -n production deploy/payments-api -c istio-proxy | \ grep "PEER_CERT_VERIFY_FAILED"
Lesson: Network policies alone are not enough. mTLS with SPIFFE certificate identity provides cryptographic proof of service identity. Enable STRICT mode across all production namespaces.
Istio Architecture 18.1
Pilot + Citadel + Galley] end subgraph "Data Plane (Sidecar Mode)" Pod1["Service A\napp container\nEnvoy sidecar"] Pod2["Service B\napp container\nEnvoy sidecar"] end subgraph "Data Plane (Ambient Mode)" ZTunnel[ztunnel DaemonSet
L4 mTLS per node] Waypoint[Waypoint Proxy
L7 per namespace] end subgraph "Observability" Prometheus2[Prometheus
Istio metrics] Kiali[Kiali
Traffic topology] Jaeger[Jaeger/Tempo
Distributed traces] end Istiod -->|certs + config| Pod1 Istiod -->|certs + config| Pod2 Istiod -->|certs + config| ZTunnel Pod1 -->|mTLS| Pod2 ZTunnel --> Waypoint Pod1 -->|metrics| Prometheus2 Prometheus2 --> Kiali Pod1 -->|traces| Jaeger
Sidecar Mode
Envoy proxy injected as sidecar into every Pod. Full L7 visibility. High resource overhead (~50MB/pod). Best for: existing clusters, full HTTP/gRPC observability, request-level policies.
Ambient Mode (Istio 1.22+)
No sidecars. ztunnel DaemonSet handles L4 mTLS. Optional Waypoint proxy for L7. 90% less memory overhead. Best for: new clusters, cost-sensitive environments, clusters with thousands of pods.
mTLS Zero-Trust Enforcement 18.2
# Step 1: Enable PERMISSIVE mesh-wide (allows both mTLS and plaintext)
# Use this first to verify all traffic switches to mTLS
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: istio-system # mesh-wide scope
spec:
mtls:
mode: PERMISSIVE
---
# Step 2: After verifying no plaintext traffic in Kiali,
# switch to STRICT (breaks plaintext connections)
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: istio-system
spec:
mtls:
mode: STRICT
---
# Namespace-level override (more granular)
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: production-strict
namespace: production
spec:
mtls:
mode: STRICT
# Port-specific exception for health probes
portLevelMtls:
8080:
mode: PERMISSIVE # liveness probe from kubelet (no cert)# Only payments-api service account can call fraud-service
# All other callers get 403 Forbidden
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: fraud-service-policy
namespace: production
spec:
selector:
matchLabels:
app: fraud-service
action: ALLOW
rules:
- from:
- source:
# SPIFFE identity — cryptographic, not IP-based
principals:
- cluster.local/ns/production/sa/payments-api
to:
- operation:
methods: ["POST"]
paths: ["/v1/fraud/check"]
---
# Deny all ingress except from ingress gateway
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: deny-all-except-gateway
namespace: production
spec:
action: DENY
rules:
- from:
- source:
notPrincipals:
- cluster.local/ns/istio-system/sa/istio-ingressgateway-service-account# Check mTLS status between two services $ istioctl x check-inject -n production NAMESPACE POD INJECTED production payments-api-7d9f8b true production fraud-service-5c4d3a true # Verify mTLS is working (connection is encrypted) $ istioctl authn tls-check payments-api.production.svc.cluster.local HOST STATUS SERVER CLIENT payments-api.production.svc.cluster.local mTLS STRICT mTLS # Check proxy config for specific pod $ istioctl proxy-config listeners payments-api-7d9f8b -n production # Real-time access log from Envoy sidecar $ kubectl logs payments-api-7d9f8b -n production -c istio-proxy --tail=50
Traffic Management: Canary & Circuit Breaker 18.3
# Progressive canary: send 10% traffic to v2
# No DNS changes, no load balancer rules — pure Envoy routing
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: payments-api
namespace: production
spec:
hosts:
- payments-api
http:
# Header-based testing: QA team always gets v2
- match:
- headers:
x-canary-user:
exact: "true"
route:
- destination:
host: payments-api
subset: v2
# Weight-based: 90/10 split
- route:
- destination:
host: payments-api
subset: v1
weight: 90
- destination:
host: payments-api
subset: v2
weight: 10
---
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: payments-api
namespace: production
spec:
host: payments-api
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
trafficPolicy:
connectionPool:
http:
h2UpgradePolicy: UPGRADE# Circuit breaker: eject unhealthy endpoints
# If >50% requests fail, eject host for 5 minutes
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: inventory-service
namespace: production
spec:
host: inventory-service
trafficPolicy:
connectionPool:
tcp:
maxConnections: 100
http:
http1MaxPendingRequests: 50
http2MaxRequests: 100
maxRetries: 3
outlierDetection:
# Eject if >5 errors in 30s consecutive interval
consecutiveGatewayErrors: 5
interval: 30s
# Keep ejected for 5 minutes
baseEjectionTime: 5m
# Never eject more than 50% of endpoints
maxEjectionPercent: 50
# Also detect 5xx errors (not just gateway errors)
consecutive5xxErrors: 10# Chaos engineering: inject 500ms delay for 20% of requests
# Use this to test timeout handling before incidents happen
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: inventory-service-chaos
namespace: production
spec:
hosts:
- inventory-service
http:
- fault:
delay:
percentage:
value: 20.0
fixedDelay: 500ms
abort:
percentage:
value: 5.0 # 5% of requests return HTTP 503
httpStatus: 503
route:
- destination:
host: inventory-service# Local rate limiting via EnvoyFilter
# Limits: 100 req/s per source IP on the /v1/search endpoint
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: search-rate-limit
namespace: production
spec:
workloadSelector:
labels:
app: search-api
configPatches:
- applyTo: HTTP_FILTER
match:
context: SIDECAR_INBOUND
listener:
filterChain:
filter:
name: envoy.filters.network.http_connection_manager
patch:
operation: INSERT_BEFORE
value:
name: envoy.filters.http.local_ratelimit
typed_config:
'@type': type.googleapis.com/udpa.type.v1.TypedStruct
type_url: type.googleapis.com/envoy.extensions.filters.http.local_ratelimit.v3.LocalRateLimit
value:
stat_prefix: local_rate_limiter
token_bucket:
max_tokens: 100
tokens_per_fill: 100
fill_interval: 1s
filter_enabled:
runtime_key: local_rate_limit_enabled
default_value:
numerator: 100
denominator: HUNDREDProduction Service Mesh Buildouts 18.3b
Istio Ambient Mesh: ztunnel & Waypoint Installation, Scoped Workload Enrollment, and Cleanup
Sidecar architectures impose significant resource overhead (~50MB memory per pod) and require application restarts for upgrades. According to Istio Ambient Mesh Getting Started Guide, Istio Ambient Mode (GA in Istio v1.22+) splits mesh duties into two layers: a lightweight node-level ztunnel DaemonSet providing zero-trust L4 mutual TLS without sidecars, and dedicated per-namespace Waypoint Envoy proxies enforcing L7 traffic routing and authorization policies.
# 1. Install Istio with Ambient profile (Requires Kubernetes v1.28+ and Istio v1.22+)
istioctl install --set profile=ambient --set values.cni.ambient.dnsCapture=true -y
# 2. Verify ambient data plane daemonsets are running across cluster nodes
kubectl get ds -n istio-system ztunnel
kubectl get ds -n istio-system istio-cni-node
# 3. Enroll production namespace into Ambient L4 transport (mTLS via ztunnel)
# Transparent: Workload pods require zero restarts and no Envoy sidecar injection
kubectl label namespace production istio.io/dataplane-mode=ambient
# 4. Verify secure zero-trust L4 mutual TLS tunnels established by ztunnel
istioctl ztunnel-config connections -n production
# 5. Deploy L7 Waypoint Proxy for layer 7 traffic management and HTTP policies
istioctl waypoint apply --namespace production --name production-waypoint --enroll-namespace
# 6. Verify Waypoint proxy deployment status and Envoy listener configurations
istioctl waypoint status -n production
istioctl proxy-config listeners deploy/production-waypoint -n production
# 7. Safe Cleanup & Workload De-enrollment (Teardown sequence)
# Delete L7 waypoint proxy (positional name) and remove ambient namespace label
istioctl waypoint delete production-waypoint --namespace production
kubectl label namespace production istio.io/dataplane-mode-
# Optional: Full mesh uninstallation (ephemeral lab clusters only; removes mesh cluster-wide)
# istioctl uninstall --purge -y| Ambient Mesh Component | OSI Layer | Placement & Scope | Resource Impact |
|---|---|---|---|
| ztunnel | Layer 4 (mTLS / SPIFFE) | Per-node DaemonSet | ~15MB per node |
| Waypoint Proxy | Layer 7 (HTTP / Authz) | Per-namespace or per-service | Auto-scaled Deployment |
Flagger Automated Canary Analysis: Metric Thresholds, Iterative Traffic Shifting, and Rollback Drill
Manual VirtualService weight adjustments are error-prone during rapid release cycles. According to Flagger Istio Progressive Delivery Documentation (Flagger v1.36+ / Istio v1.22+), Flagger automates progressive traffic shifting, real-time Prometheus metric analysis, and instant rollback. (Simulated offline exercise; no cluster credentials, paid resources, or live cloud runs are used; explicitly unexecuted when no live cluster exists).
apiVersion: flagger.app/v1beta1
kind: Canary
metadata:
name: payment-api
namespace: production
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: payment-api
service:
port: 8080
targetPort: 8080
gateways:
- istio-system/public-gateway
hosts:
- payment.production.internal
analysis:
interval: 1m
threshold: 5 # max failed checks before rollback
maxWeight: 50 # maximum canary traffic weight (50%)
stepWeight: 10 # increase weight by 10% each minute (10%, 20%, 30%...)
metrics:
- name: request-success-rate
thresholdRange:
min: 99 # minimum acceptable success rate percentage (99%)
interval: 1m
- name: request-duration
thresholdRange:
max: 500 # maximum acceptable P99 latency in milliseconds
interval: 1m
webhooks:
- name: smoke-test
type: pre-rollout
url: http://flagger-loadtester.production/
timeout: 30s # must exceed duration of blocking command to prevent premature timeout
metadata:
type: bash # blocking execution mode waits for command exit before advancing rollout
cmd: "hey -z 15s -q 10 -c 2 http://payment-api-canary.production:8080/healthz"
Automated Rollback Sequence: If the canary deployment breaches request-success-rate < 99% or request-duration > 500ms for 5 consecutive check intervals, Flagger aborts the release, resets VirtualService routing weights back to primary (100% primary, 0% canary), and marks the canary status as Failed.
Multi-Cluster Trust Topology, Shared Root CA, and Production Istio PromQL Alerts
Federating services across multiple Kubernetes clusters requires cryptographic identity trust and cross-cluster routing. According to Istio Multi-Primary Multi-Cluster Guide and Istio Standard Metrics Reference:
# 1. Multi-Cluster Trust Prerequisite: Shared Root CA Intermediate Secret
# Both clusters provision the 'cacerts' Secret in istio-system from intermediate certs signed by common Root CA
kubectl create secret generic cacerts -n istio-system \
--from-file=ca-cert.pem \
--from-file=ca-key.pem \
--from-file=root-cert.pem \
--from-file=cert-chain.pem
---
# 2. Production Prometheus Alert Rules for Mesh-wide Error Rates & SLO Latency
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
name: istio-mesh-alerts
namespace: istio-system
spec:
groups:
- name: istio-mesh-slo
rules:
- alert: IstioHighErrorRate
expr: |
sum(rate(istio_requests_total{reporter="destination",response_code=~"5.*"}[5m]))
/
sum(rate(istio_requests_total{reporter="destination"}[5m])) > 0.01
for: 2m
labels:
severity: critical
annotations:
summary: "Mesh-wide 5xx error rate exceeds 1% SLO threshold"
- alert: IstioP99LatencyBreach
expr: |
histogram_quantile(0.99,
sum(rate(istio_request_duration_milliseconds_bucket{reporter="destination"}[5m])) by (le, destination_workload)
) > 500
for: 3m
labels:
severity: warning
annotations:
summary: "Workload P99 latency exceeds 500ms"
- alert: IstioPilotSyncDrift
expr: |
sum(pilot_xds_push_errors{type="cds"}) > 0
for: 5m
labels:
severity: warning
annotations:
summary: "Istiod Pilot reporting CDS push errors to Envoy proxies"// Deterministic Ambient Mesh Workload Evaluator
export function evaluateAmbientWorkload(podLabels, namespaceLabels) {
const isNamespaceAmbient = namespaceLabels?.['istio.io/dataplane-mode'] === 'ambient';
const isPodExcluded = podLabels?.['istio.io/dataplane-mode'] === 'none';
const hasWaypoint = Boolean(namespaceLabels?.['istio.io/use-waypoint'] || podLabels?.['istio.io/use-waypoint']);
if (isNamespaceAmbient && !isPodExcluded) {
return { enrolled: true, layer: hasWaypoint ? 'L7-Waypoint' : 'L4-ztunnel' };
}
return { enrolled: false, layer: 'none' };
}
// Deterministic Flagger Rollback Evaluator
export function evaluateFlaggerRollback(currentWeight, failedChecks, maxThreshold = 5, stepWeight = 10, maxWeight = 50) {
if (failedChecks >= maxThreshold) {
return { action: 'rollback', trafficToPrimary: 100, trafficToCanary: 0, status: 'Failed' };
}
const nextCanary = Math.min(currentWeight + stepWeight, maxWeight);
return { action: 'promote', trafficToPrimary: 100 - nextCanary, trafficToCanary: nextCanary, status: 'Progressing' };
}
// Deterministic Mesh PromQL Error Rate Evaluator
export function evaluateMeshErrorRate(fiveXxCount, totalCount, threshold = 0.01) {
if (!totalCount || totalCount <= 0) return { alert: false, rate: 0 };
const rate = fiveXxCount / totalCount;
return { alert: rate > threshold, rate };
}Hands-On Labs 18.4
Service Mesh Labs
- Install Istio on kind with istioctl, enable auto-injection on production namespace
- Deploy bookinfo app, verify all pods have istio-proxy sidecar running
- Enable STRICT mTLS mesh-wide, verify with istioctl authn tls-check
- Create canary VirtualService — 90/10 split between v1/v2, verify with curl loop
- Apply outlier detection DestinationRule, manually kill pods, watch circuit breaker trip
- Install Kiali, observe real-time service graph during load test with k6
- Write AuthorizationPolicy that only allows payments-api → fraud-service, deny all others
Troubleshooting 18.5
503 after enabling mTLS STRICT
Non-injected pods (e.g., monitoring tools, jobs without sidecar) cannot connect. Add istio-injection: enabled label to their namespace, or create a PeerAuthentication exception for that port. Also check external services that call into the cluster — they need TLS termination at the ingress gateway.
VirtualService weight not taking effect
Check: (1) DestinationRule subsets match the actual pod labels exactly. (2) VirtualService hosts field uses the correct service name. (3) VirtualService is in the same namespace as the service. Debug with istioctl proxy-config routes <pod> to see what Envoy sees.
Envoy sidecar using too much CPU/memory
Consider migrating to Ambient Mesh (Istio 1.22+) which eliminates sidecars. Short-term: set resource limits on the sidecar via ProxyConfig CR or meshConfig.defaultConfig.proxyMetadata. Reduce logging verbosity with istioctl proxy-config log <pod> --level warning.