liveBook · Manning

Spread topology

As explained in Kubernetes for Developers

A spread topology is a scheduling constraint that distributes Pods across specified topology domains such as zones or nodes.

A spread topology keeps replicated Pods from landing in one failure domain. Without it, three replicas can fit on one node and disappear together when that node fails. Kubernetes can distribute them across nodes, zones, or other provider-defined domains. section 8.2.1

Configure a workload with topology spread constraints to control placement:

  • Set topologySpreadConstraints in the Pod template. section 8.2.1
  • Choose topologyKey, such as kubernetes.io/hostname, to make each uniquely labeled node a separate spreading domain. section 8.2.1
  • Verify that every node has the label named by topologyKey; otherwise the configuration cannot work as intended. section 8.2.1

Three-replica deployment

A timeserver Deployment with three replicas uses kubernetes.io/hostname, maxSkew: 1, and whenUnsatisfiable: ScheduleAnyway. Running it on three Minikube nodes and inspecting kubectl get pods -o wide shows the resulting placement. section 8.2.1

Listing 8.9 Chapter08/8.2.1_TopologySpread/deploy_topology.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: timeserver
spec:
  replicas: 3
  selector:
    matchLabels:
      pod: timeserver-pod
  template:
    metadata:
      labels:
        pod: timeserver-pod
    spec:
      topologySpreadConstraints:               #1
      - maxSkew: 1                             #2
        topologyKey: kubernetes.io/hostname    #3
        whenUnsatisfiable: ScheduleAnyway      #4
        labelSelector:                         #5
          matchLabels:                         #5
            pod: timeserver-pod                #5
      containers:
      - name: timeserver-container
        image: docker.io/wdenniss/timeserver:5
        resources:
          requests:
            cpu: 200m
            memory: 250Mi
#1 The topology constraint added
#2 The maximum number of replica imbalance
#3 The node label to use for the topology
#4 The behavior to use when it’s not possible to satisfy the topology requirement
#5 Another label selector set to this template’s metadata label

Inspect the complete Deployment manifest, including the topology spread fields and matching labels. section 8.2.1

How Kubernetes for Developers covers it

How deep it goes
Explains the scheduling problem, configuration fields, label prerequisite, and a three-replica deployment, without exhaustive policy details.
Examples
Uses a three-replica timeserver Deployment, three Minikube nodes, kubectl get pods -o wide, and a topology spread YAML manifest.
What it assumes
Assumes familiarity with Pods, replicas, nodes, and resource requests while emphasizing the required topology labels.
What you can do after
Operate/configure it by adding topologySpreadConstraints and verifying placement across labeled nodes.
How it connects
Connects topology spread with Pods, replicas, nodes, zones, failure domains, node labels, and high availability.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes for Developers. The section links point to the parts of the book it draws on.