✦ For everyone, free.

Practical knowledge for real and everyday life

Home

Kubernetes Zone Aware Placement

Kubernetes Zone Aware Placement ensures workloads run in specific zones, optimizing availability and performance across cloud infrastructure.

Kubernetes Zone Aware Placement is the application of Kubernetes' scheduling and storage mechanisms specifically to reason about and control which availability zone a Pod, and the storage it depends on, ends up running in — a concern distinct from generic node-level placement because zones typically represent physically separate failure domains with independent power, networking, and infrastructure, and often carry meaningfully different network latency and cost characteristics between them. Zone-aware placement draws on several of Kubernetes' more general placement primitives (node labels, topology spread constraints, volume topology) applied consistently around the specific topology key topology.kubernetes.io/zone, rather than being a separate mechanism of its own.

Getting zone-aware placement right is central to building genuinely fault-tolerant systems on Kubernetes, since a workload spread only across nodes within a single zone remains fully exposed to that zone's failure, however well-distributed it is at the node level.


The Zone Topology Label

Automatic Zone Labeling

Cloud provider integrations automatically label nodes with topology.kubernetes.io/zone (and the related topology.kubernetes.io/region) reflecting the physical availability zone each node runs in, giving every zone-aware placement rule a consistent, reliable label to reference without manual tagging.

kubectl get nodes -L topology.kubernetes.io/zone

Spreading Workloads Across Zones

Topology Spread Constraints for Zone Balance

The most direct mechanism for zone-aware replica distribution is a topologySpreadConstraints entry using topology.kubernetes.io/zone as the topologyKey, bounding how unevenly a workload's replicas may be distributed across the zones available in the cluster.

topologySpreadConstraints:
  - maxSkew: 1
    topologyKey: "topology.kubernetes.io/zone"
    whenUnsatisfiable: DoNotSchedule
    labelSelector:
      matchLabels:
        app: codartium-api

Pod Anti-Affinity as an Alternative

For simpler cases, zone-scoped pod anti-affinity achieves a similar (though less precisely tunable) result, discouraging or preventing replicas of the same workload from concentrating in a single zone.

affinity:
  podAntiAffinity:
    preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          labelSelector:
            matchLabels:
              app: codartium-api
          topologyKey: "topology.kubernetes.io/zone"

Zone-Aware Storage

Volume Binding Constraints

Persistent volumes provisioned by many cloud storage classes are inherently zone-scoped — an EBS volume, for instance, exists physically within a single zone and can only be attached to a node within that same zone. StorageClass objects using volumeBindingMode: WaitForFirstConsumer delay volume provisioning until a Pod referencing the claim is actually scheduled, allowing the volume to be provisioned in the same zone the scheduler ultimately chose for the Pod, rather than an arbitrary or mismatched zone.

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: codartium-zonal-ssd
provisioner: ebs.csi.aws.com
volumeBindingMode: WaitForFirstConsumer

Consequences of Zone-Bound Storage on Rescheduling

A Pod bound to a zone-specific volume cannot be rescheduled to a node in a different zone without the volume itself being migrated or recreated — this is an important constraint to account for when reasoning about failover behavior, since a zone outage affecting the Pod also affects its bound storage, and recovery in a different zone typically requires restoring from a backup or replica rather than simply rescheduling the same volume elsewhere.


Zone-Aware Traffic Routing

Topology-Aware Routing for Services

Beyond Pod placement itself, Kubernetes Services can be configured with topology-aware routing hints, preferring to route traffic to endpoints within the same zone as the originating client, reducing cross-zone network traffic and its associated latency and cost — this complements zone-aware Pod placement by ensuring the benefit of spreading replicas across zones also extends to how traffic is actually distributed among them.


Balancing Fault Tolerance and Cost

The Cross-Zone Traffic Tradeoff

Spreading replicas across zones for fault tolerance inherently increases the likelihood of cross-zone communication between cooperating services, which most cloud providers bill for and which carries higher latency than same-zone communication — zone-aware placement design is therefore a deliberate tradeoff between resilience (more zones) and both cost and latency (fewer zones, or zone-aware routing to minimize the actual cross-zone traffic even while replicas remain spread).


Example

apiVersion: apps/v1
kind: Deployment
metadata:
  name: codartium-zone-aware-api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: codartium-zone-aware-api
  template:
    metadata:
      labels:
        app: codartium-zone-aware-api
    spec:
      topologySpreadConstraints:
        - maxSkew: 1
          topologyKey: "topology.kubernetes.io/zone"
          whenUnsatisfiable: DoNotSchedule
          labelSelector:
            matchLabels:
              app: codartium-zone-aware-api
      containers:
        - name: api
          image: codartium/api:latest