The Challenge of Cross-Zone Traffic in Kubernetes
Applications often span multiple zones in a Kubernetes cluster to ensure high availability and fault tolerance. However, this can lead to inefficient cross-zone traffic, increasing latency and higher costs. This challenge necessitates a solution that optimizes traffic flow within the cluster while maintaining reliability and performance.
Introducing Topology Aware Routing (TAR): A Kubernetes Solution
Topology-aware routing (TAR) is a feature in Kubernetes that addresses the challenge of cross-zone traffic. TAR enhances traffic routing by leveraging the cluster’s topology, ensuring pods handle requests in the same zone whenever possible. This reduces cross-zone data transfer, lowering costs and improving performance.
Setting Up the Demo Environment: Applications and Services
To demonstrate the benefits of TAR, we’ll set up a demo environment with multiple applications and services distributed across different zones. Here’s an outline of our setup:
- Create a Kubernetes Cluster: Ensure the cluster spans various zones.
- Deploy Applications: Deploy two sample applications (App1 and App2) across the zones.
- Expose Services: Expose these applications using standard Kubernetes services.
# Create a multi-zone Kubernetes cluster (example with GKE)
gcloud container clusters create demo-cluster –num-nodes=3 –zone=us-central1-a –additional-zones=us-central1-b,us-central1-c
# Deploy App1 and App2
kubectl apply -f app1-deployment.yaml
kubectl apply -f app2-deployment.yaml
# Expose services
kubectl expose deployment app1 –type=LoadBalancer –name=app1-service
kubectl expose deployment app2 –type=LoadBalancer –name=app2-service
Observing Random Traffic Distribution with Standard Services
Initially, traffic distribution is random without TAR, and pods handle requests in any zone, often resulting in cross-zone traffic. We can observe this behavior by generating traffic and monitoring the network flow.
# Generate traffic to App1 and App2 services
curl http://<app1-service-ip>
curl http://<app2-service-ip>
Using monitoring tools like Prometheus and Grafana, we can visualize the traffic distribution and identify the extent of cross-zone traffic.
Enabling Topology Aware Routing for Optimized Traffic Flow
Enabling TAR in Kubernetes requires modifying the service configuration to include topology-aware routing settings. This can be done by updating the service manifest files.
# app1-service.yaml
apiVersion: v1
kind: Service
metadata:
name: app1-service
spec:
selector:
app: app1
ports:
– protocol: TCP
port: 80
targetPort: 8080
topologyKeys:
– “kubernetes.io/hostname”
– “topology.kubernetes.io/zone”
Apply the updated service configuration:
kubectl apply -f app1-service.yaml
kubectl apply -f app2-service.yaml
Validating Zone-Aware Traffic Routing with TAR
After enabling TAR, we can validate that the traffic is routed within the same zone. Generate traffic again and monitor the network flow to see the difference in traffic distribution.
# Generate traffic to App1 and App2 services
curl http://<app1-service-ip>
curl http://<app2-service-ip>
Using monitoring tools, we should observe a significant reduction in cross-zone traffic, indicating that pods are now handling requests in the same zone.
Scenario: Traffic Routing with a Single App2 Container
To further illustrate the benefits of TAR, consider a scenario where App2 has only one pod in a specific zone. With TAR enabled, requests to App2 will be routed to the available pod, ensuring optimal traffic flow without unnecessary cross-zone data transfer.
# Scale down App2 to a single pod in a specific zone
kubectl scale deployment app2 –replicas=1
kubectl get pods -o wide
Conclusion: The Benefits of Topology Aware Routing
Topology-aware routing (TAR) is a powerful feature in Kubernetes that optimizes traffic flow within a cluster by leveraging zone-aware routing. By reducing cross-zone traffic, TAR enhances application performance and lowers costs, making it an essential tool for managing distributed applications in Kubernetes.
References
Exploring the effect of Topology Aware Hints on network traffic in Amazon Elastic Kubernetes Service
Optimize AZ traffic costs using Amazon EKS, Karpenter, and Istio