Sub topic 2
Topology keys: zones, nodes and custom labels
This chapter assumes you know Pods, Deployments, labels and selectors, and that you can use kubectl to inspect resources. It covers topology keys. These are the node labels that define the failure domains the scheduler spreads pods across.
What a topology key is
A topology key is the name of a node label. Nodes that share the same value for that label belong to the same topology domain. The scheduler groups nodes by label value and treats each group as one failure domain.
For example, if three nodes carry topology.kubernetes.io/zone=us-east-1a and two carry topology.kubernetes.io/zone=us-east-1b, the key topology.kubernetes.io/zone produces two domains. Later chapters use these domains to count pods and calculate skew.
Well-known keys
Kubernetes defines standard labels. Cloud providers and kubelet normally set them for you.
| Topology key | Domain it creates | Typical value |
|---|---|---|
| `topology.kubernetes.io/zone` | An availability zone | `us-east-1a` |
| `topology.kubernetes.io/region` | A region | `us-east-1` |
| `kubernetes.io/hostname` | A single node | `worker-3` |
Because every node has a unique kubernetes.io/hostname value, each node is its own domain. Spreading on this key spreads pods across nodes.
Inspecting the labels on your nodes
Check which labels your nodes actually have before you write a constraint.
kubectl get nodes -L topology.kubernetes.io/zone,kubernetes.io/hostnameThe -L flag adds a column for each label. An empty cell means the node lacks that label.
Custom keys
You can define your own domains by labeling nodes. A rack, a server room, or a hardware group are common examples.
kubectl label node worker-1 worker-2 example.com/rack=rack-a
kubectl label node worker-3 worker-4 example.com/rack=rack-bYou can then use example.com/rack as a topologyKey. Use a prefix you control, such as your domain name, so your label does not clash with Kubernetes labels.
topologySpreadConstraints:
- maxSkew: 1
topologyKey: example.com/rack
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: webHow the scheduler uses the key
Common errors
- A node without the label does not belong to any domain. With
DoNotSchedule, the scheduler will not place pods on nodes that lack the topology key. These nodes sit unused by that workload. - A typo in the key creates no domains. Pods stay Pending, because no node matches.
- A key with too many distinct values, such as a per-node custom label, makes every node its own domain. This is equivalent to spreading by hostname.
Exercise
Your cluster has six nodes in three zones, and you label two of them example.com/rack=rack-a and leave the other four unlabeled. You set topologyKey: example.com/rack with whenUnsatisfiable: DoNotSchedule. What happens to the four unlabeled nodes?
Show answer
They are not part of any domain for this key, so the scheduler will not place the constrained pods on them. All pods land on the two labeled nodes. Label every eligible node, or choose a key that all nodes have.