Welcome to from-docker-to-kubernetes

Kubernetes Topology Aware Routing

Advanced guide to implementing zone-aware traffic management, latency-based optimization, and multi-region failover configurations for global service deployments

Introduction to Kubernetes Topology Aware Routing

Zone-aware Traffic Distribution

Route service traffic to pods in the same zone as the client

Latency Optimization

Minimize network latency by preferring local endpoints

Cross-zone Traffic Reduction

Lower inter-zone data transfer costs and bandwidth usage

Failure Isolation

Improve resilience by containing failures within topology boundaries

This comprehensive guide explores the architecture, implementation patterns, and operational best practices for topology-aware routing in Kubernetes, helping you optimize application performance and reduce costs in distributed environments.

Topology Aware Routing Architecture

Core Concepts

Kubernetes Topology Aware Routing builds on several foundational components:

┌─────────────────────────────────────────────────────────────────────┐
│                                                                     │
│                    Kubernetes Cluster                               │
│                                                                     │
│   ┌─────────────────┐         ┌─────────────────┐                   │
│   │                 │         │                 │                   │
│   │   Zone A        │         │   Zone B        │                   │
│   │                 │         │                 │                   │
│   │  ┌─────┐ ┌─────┐│         │  ┌─────┐ ┌─────┐│                   │
│   │  │Pod A│ │Pod B││         │  │Pod C│ │Pod D││                   │
│   │  └─────┘ └─────┘│         │  └─────┘ └─────┘│                   │
│   │                 │         │                 │                   │
│   └────────┬────────┘         └────────┬────────┘                   │
│            │                           │                            │
│   ┌────────▼───────────────────────────▼────────┐                   │
│   │                                             │                   │
│   │        Topology Aware Service               │                   │
│   │                                             │                   │
│   └─────────────────────┬───────────────────────┘                   │
│                         │                                           │
│                         │                                           │
│   ┌─────────────────────▼───────────────────────┐                   │
│   │                                             │                   │
│   │        EndpointSlice with Topology Hints    │                   │
│   │                                             │                   │
│   └─────────────────────────────────────────────┘                   │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

Topology Key Concepts

  • Topology Keys: Labels that identify infrastructure topology (zone, region, etc.)

  • EndpointSlice: Enhanced endpoint grouping with topology information

  • Service Topology: Service configuration that respects topology constraints

  • Topology Aware Hints: Feature gate that enables preferential local routing

  • Service Traffic Policy: Controls how traffic is distributed across endpoints

Enabling Topology Aware Routing

Prerequisites

Node Topology Labels

Verify your nodes have the required topology labels:

# Check node topology labels
kubectl get nodes --show-labels | grep topology.kubernetes.io

# Example output:
# worker-1   Ready    <none>   90d   v1.24.6   topology.kubernetes.io/zone=us-east-1a,topology.kubernetes.io/region=us-east-1
# worker-2   Ready    <none>   90d   v1.24.6   topology.kubernetes.io/zone=us-east-1b,topology.kubernetes.io/region=us-east-1
# worker-3   Ready    <none>   90d   v1.24.6   topology.kubernetes.io/zone=us-east-1c,topology.kubernetes.io/region=us-east-1

Add topology labels if they're missing:

# Add topology labels to nodes
kubectl label nodes worker-1 topology.kubernetes.io/zone=us-east-1a topology.kubernetes.io/region=us-east-1
kubectl label nodes worker-2 topology.kubernetes.io/zone=us-east-1b topology.kubernetes.io/region=us-east-1
kubectl label nodes worker-3 topology.kubernetes.io/zone=us-east-1c topology.kubernetes.io/region=us-east-1

Feature Gate Configuration

Enable the Topology Aware Hints feature gate in your cluster:

For kube-apiserver, kube-controller-manager, and kube-scheduler, add:

--feature-gates=TopologyAwareHints=true

For a kubeadm-managed cluster, modify the cluster configuration:

apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
featureGates:
  TopologyAwareHints: true

Implementing Zone-Aware Routing

Basic Topology Aware Service

Create a service with topology awareness enabled:

Zone-first Topology
apiVersion: v1
kind: Service
metadata:
  name: topology-aware-service
spec:
  selector:
    app: myapp
  ports:
  - port: 80
    targetPort: 8080
  topologyKeys:
    - "topology.kubernetes.io/zone"
    - "topology.kubernetes.io/region"
    - "*"