skip to content
Astro Cactus

Kubernetes Service Mesh with Istio

/ 3 min read

Understanding Service Mesh and Istio

A service mesh provides infrastructure layer that handles service-to-service communication in microservices architectures. Istio is one of the most popular service mesh implementations for Kubernetes.

Installing Istio

1. Basic Installation

Terminal window
# Download Istio
curl -L https://istio.io/downloadIstio | sh -
# Add istioctl to PATH
export PATH=$PWD/istio-1.20.0/bin:$PATH
# Install Istio with demo profile
istioctl install --set profile=demo -y

2. Custom Installation

apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
namespace: istio-system
name: istio-control-plane
spec:
profile: demo
components:
egressGateways:
- name: istio-egressgateway
enabled: true
ingressGateways:
- name: istio-ingressgateway
enabled: true
pilot:
enabled: true
values:
global:
proxy:
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 200m
memory: 256Mi

Core Concepts

1. Sidecar Injection

apiVersion: v1
kind: Namespace
metadata:
name: bookinfo
labels:
istio-injection: enabled
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: reviews
namespace: bookinfo
spec:
replicas: 3
selector:
matchLabels:
app: reviews
template:
metadata:
labels:
app: reviews
spec:
containers:
- name: reviews
image: docker.io/istio/examples-bookinfo-reviews-v1:1.16.2
ports:
- containerPort: 9080

2. Virtual Services

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews-route
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: jason
route:
- destination:
host: reviews
subset: v2
- route:
- destination:
host: reviews
subset: v1

3. Destination Rules

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews-destination
spec:
host: reviews
trafficPolicy:
loadBalancer:
simple: RANDOM
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
trafficPolicy:
loadBalancer:
simple: ROUND_ROBIN

Traffic Management

1. Gateway Configuration

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: bookinfo-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "bookinfo.example.com"

2. Traffic Splitting

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews-split
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
weight: 80
- destination:
host: reviews
subset: v2
weight: 20

Security Features

1. Authentication Policy

apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: istio-system
spec:
mtls:
mode: STRICT

2. Authorization Policy

apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: reviews-viewer
namespace: bookinfo
spec:
selector:
matchLabels:
app: reviews
rules:
- from:
- source:
principals: ["cluster.local/ns/default/sa/bookinfo-productpage"]
to:
- operation:
methods: ["GET"]

Observability

1. Distributed Tracing

apiVersion: telemetry.istio.io/v1alpha1
kind: Telemetry
metadata:
name: mesh-default
namespace: istio-system
spec:
tracing:
- randomSamplingPercentage: 100.0
customTags:
my-tag:
literal:
value: "123"

2. Metrics Collection

apiVersion: telemetry.istio.io/v1alpha1
kind: Telemetry
metadata:
name: mesh-metrics
namespace: istio-system
spec:
metrics:
- providers:
- name: prometheus
overrides:
- match:
metric: REQUEST_COUNT
mode: CLIENT_AND_SERVER
tagOverrides:
response_code:
operation: UPSERT
value: "response.code"

Advanced Features

1. Circuit Breaking

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews-circuit-breaker
spec:
host: reviews
trafficPolicy:
outlierDetection:
consecutive5xxErrors: 1
interval: 1s
baseEjectionTime: 3m
maxEjectionPercent: 100

2. Fault Injection

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews-fault
spec:
hosts:
- reviews
http:
- fault:
delay:
percentage:
value: 10.0
fixedDelay: 5s
route:
- destination:
host: reviews
subset: v1

3. Rate Limiting

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: filter-ratelimit
namespace: istio-system
spec:
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.ratelimit
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.ratelimit.v3.RateLimit
domain: productpage-ratelimit
rate_limit_service:
grpc_service:
envoy_grpc:
cluster_name: rate_limit_cluster
transport_api_version: V3

Best Practices

1. Resource Management

apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
components:
pilot:
k8s:
resources:
requests:
cpu: 500m
memory: 2048Mi
limits:
cpu: 1000m
memory: 4096Mi

2. Monitoring Setup

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: istio-component-monitor
namespace: istio-system
spec:
selector:
matchLabels:
istio: pilot
endpoints:
- port: http-monitoring

Troubleshooting

Common Issues

  1. Sidecar Injection Issues
Terminal window
kubectl get namespace -L istio-injection
kubectl describe pod <pod-name> -n <namespace>
  1. Traffic Routing Issues
Terminal window
istioctl analyze
istioctl proxy-status
  1. Security Policy Issues
Terminal window
istioctl authn tls-check <pod-name>.<namespace>

Performance Tuning

1. Proxy Configuration

apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
meshConfig:
defaultConfig:
concurrency: 2
proxyMetadata:
ISTIO_META_HTTP10: "1"

2. Resource Optimization

apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
values:
pilot:
cpu:
targetAverageUtilization: 80
global:
proxy:
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 200m
memory: 256Mi

Conclusion

Istio provides a powerful service mesh solution for Kubernetes, offering advanced traffic management, security, and observability features. Understanding and properly implementing these features is crucial for building robust microservices architectures.

Series Navigation