In this episode, we'll discuss Service Mesh for managing service-to-service communication in Kubernetes. We'll learn about Istio, Linkerd, traffic management, security, and observability features of service meshes.

Note
If you want to read the previous episode, you can click the Episode 42 thumbnail below
In the previous episode, we explored Observability, which enables you to understand what's happening inside your Kubernetes cluster. Now we'll dive into Service Mesh, which manages service-to-service communication.
Note: Here I'll be using a Kubernetes Cluster installed through K3s.
As microservices grow, managing communication between services becomes complex. Service Mesh is a dedicated infrastructure layer that handles service-to-service communication. Think of it like a traffic controller for your microservices - it manages routing, security, and observability without requiring changes to application code.
Service Mesh is a configurable infrastructure layer that handles inter-service communication in a microservices architecture.
Service Mesh (East-West Routing or Service-to-Service) vs Gateway API (North-South Routing or Public Facing)
Service Mesh Observability
Kubernetes Deployment Strategies1. Traffic Management
Control how traffic flows between services.
2. Security
Enforce mutual TLS and authorization policies.
3. Observability
Automatic metrics, logs, and traces for service communication.
4. Resilience
Circuit breaking, retries, and timeouts.
5. Load Balancing
Intelligent load balancing across service instances.
6. Canary Deployments
Gradually roll out new versions.
7. Compliance
Meet security and compliance requirements.
┌─────────────────────────────────────┐
│ Control Plane │
│ - Policy management │
│ - Configuration │
│ - Certificate management │
└──────────────┬──────────────────────┘
│
┌──────────────▼──────────────────────┐
│ Data Plane │
│ - Sidecar proxies │
│ - Service-to-service routing │
│ - Traffic management │
└─────────────────────────────────────┘Control Plane
Manages configuration and policies.
Data Plane
Executes traffic management and security policies.
Istio is the most popular service mesh for Kubernetes.
# Download Istio
curl -L https://istio.io/downloadIstio | sh -
cd istio-1.19.0
# Install Istio
./bin/istioctl install --set profile=demo -y
# Enable sidecar injection
kubectl label namespace default istio-injection=enabledIstiod
Control plane component managing configuration.
Envoy Proxy
Sidecar proxy handling traffic.
Ingress Gateway
Entry point for external traffic.
Egress Gateway
Exit point for external traffic.
apiVersion: v1
kind: Namespace
metadata:
name: production
labels:
istio-injection: enabled
---
apiVersion: v1
kind: Pod
metadata:
name: app-pod
namespace: production
spec:
containers:
- name: app
image: myapp:latestRoutes traffic to services:
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: web-app
spec:
hosts:
- web-app
http:
- match:
- uri:
prefix: "/api"
route:
- destination:
host: web-app
port:
number: 8080
subset: v1
weight: 90
- destination:
host: web-app
port:
number: 8080
subset: v2
weight: 10Defines load balancing and connection pooling:
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: web-app
spec:
host: web-app
trafficPolicy:
connectionPool:
tcp:
maxConnections: 100
http:
http1MaxPendingRequests: 100
maxRequestsPerConnection: 2
loadBalancer:
simple: ROUND_ROBIN
outlierDetection:
consecutive5xxErrors: 5
interval: 30s
baseEjectionTime: 30s
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2Gradually shift traffic to new version:
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: web-app
spec:
hosts:
- web-app
http:
- match:
- headers:
user-agent:
regex: ".*Chrome.*"
route:
- destination:
host: web-app
subset: v2
- route:
- destination:
host: web-app
subset: v1
weight: 95
- destination:
host: web-app
subset: v2
weight: 5apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: web-app
spec:
hosts:
- web-app
http:
- route:
- destination:
host: web-app
retries:
attempts: 3
perTryTimeout: 2sapiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: web-app
spec:
hosts:
- web-app
http:
- route:
- destination:
host: web-app
timeout: 10sEnforce mutual TLS:
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
spec:
mtls:
mode: STRICTControl access between services:
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: web-app-policy
spec:
selector:
matchLabels:
app: web-app
rules:
- from:
- source:
principals: ["cluster.local/ns/default/sa/api-service"]
to:
- operation:
methods: ["GET"]
paths: ["/api/v1/*"]
- from:
- source:
principals: ["cluster.local/ns/default/sa/admin-service"]
to:
- operation:
methods: ["*"]Linkerd is a lightweight service mesh alternative to Istio.
# Install Linkerd CLI
curl https://run.linkerd.io/install | sh
# Install Linkerd
linkerd install | kubectl apply -f -
# Verify installation
linkerd check
# Enable automatic sidecar injection
kubectl annotate namespace default linkerd.io/inject=enabled| Aspect | Linkerd | Istio |
|---|---|---|
| Complexity | Simple | Complex |
| Resource Usage | Low | High |
| Features | Core features | Comprehensive |
| Learning Curve | Easy | Steep |
| Performance | Fast | Slower |
| Community | Growing | Large |
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app-blue
spec:
replicas: 3
selector:
matchLabels:
app: web-app
version: blue
template:
metadata:
labels:
app: web-app
version: blue
spec:
containers:
- name: web-app
image: myapp:1.0
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app-green
spec:
replicas: 3
selector:
matchLabels:
app: web-app
version: green
template:
metadata:
labels:
app: web-app
version: green
spec:
containers:
- name: web-app
image: myapp:2.0
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: web-app
spec:
hosts:
- web-app
http:
- route:
- destination:
host: web-app
subset: blue
weight: 100
- destination:
host: web-app
subset: green
weight: 0apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: web-app
spec:
host: web-app
trafficPolicy:
outlierDetection:
consecutive5xxErrors: 5
interval: 30s
baseEjectionTime: 30s
maxEjectionPercent: 50
minRequestVolume: 5apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: web-app
spec:
hosts:
- web-app
http:
- match:
- uri:
prefix: "/admin"
route:
- destination:
host: web-app
subset: admin
- match:
- uri:
prefix: "/api"
route:
- destination:
host: web-app
subset: api
- route:
- destination:
host: web-app
subset: webProblem: Using service mesh for simple deployments.
Solution: Start simple, add service mesh when needed:
# Start with basic Kubernetes
# Add service mesh when managing multiple servicesProblem: Unexpected performance degradation.
Solution: Monitor sidecar resource usage:
kubectl top pods -n default
kubectl describe pod pod-nameProblem: Services can't communicate due to TLS issues.
Solution: Verify mTLS configuration:
istioctl analyze
kubectl get peerauthentication -AProblem: Can't troubleshoot service mesh issues.
Solution: Enable observability:
# Install Kiali for visualization
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.19/samples/addons/kiali.yamlProblem: Service mesh doesn't replace network policies.
Solution: Use both service mesh and network policies:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: web-app-policy
spec:
podSelector:
matchLabels:
app: web-app
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector:
matchLabels:
app: api-service# Install observability addons
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.19/samples/addons/prometheus.yaml
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.19/samples/addons/grafana.yaml# Use canary deployments
weight: 90 # 90% to v1
weight: 10 # 10% to v2# Separate namespaces for different environments
namespaces:
- production
- staging
- development# Check sidecar resource consumption
kubectl top pods -n defaultoutlierDetection:
consecutive5xxErrors: 5
interval: 30smtls:
mode: STRICT# Service Mesh Policies
## mTLS
- Enabled: STRICT mode
- Certificate rotation: Automatic
## Traffic Management
- Canary: 10% to new version
- Timeout: 10s# Simulate service failure
kubectl delete pod pod-name
# Verify circuit breaker activates
kubectl logs pod-nameVisualize service mesh:
# Access Kiali
kubectl port-forward -n istio-system svc/kiali 20000:20000
# Open http://localhost:20000# Request rate
rate(istio_requests_total[5m])
# Error rate
rate(istio_requests_total{response_code=~"5.."}[5m])
# Latency
histogram_quantile(0.95, rate(istio_request_duration_milliseconds_bucket[5m]))Use Service Mesh When:
Don't Use Service Mesh When:
In episode 43, we've explored Service Mesh in Kubernetes in depth. We've learned about Istio, Linkerd, traffic management, security, and observability features.
Key takeaways:
Service Mesh is powerful for managing complex microservices architectures but requires careful planning and implementation.