Sub topic 7
Keeping rolling updates balanced with matchLabelKeys
This chapter assumes you can write a topologySpreadConstraints block and calculate skew by hand. It also assumes you know that a Deployment manages pods through ReplicaSets.
The problem: two generations of pods
A rolling update runs old and new pods side by side. Both generations carry the label app: web, so a constraint with labelSelector: app=web counts them together.
The scheduler places each new pod so that the combined count stays within maxSkew. The old pods are about to be deleted, though. When they are gone, the new pods alone may be badly unbalanced. For example, you could end up with four pods in one zone and two in another.
The fix: matchLabelKeys
matchLabelKeys is a list of label keys. For each key, the scheduler reads the value from the pod being scheduled. It adds that key and value to the labelSelector as an extra condition.
The Deployment controller puts a pod-template-hash label on every pod. The value is different for each ReplicaSet revision. Using that key makes each revision count only its own pods.
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: web
matchLabelKeys:
- pod-template-hashWhat to expect
- Each revision is balanced alone. Old pods are ignored when a new pod is placed, so the new ReplicaSet spreads as if it were a fresh install.
- Final state is even. When the old pods are removed, the new pods already satisfy
maxSkew. - Rollbacks work the same way. A rollback reuses an old ReplicaSet, and its hash selects that revision's pods.
Common errors
- The key is missing from the pod. If a pod has no label with that key, the scheduler ignores the key. A bare Pod has no
pod-template-hash, so the setting does nothing there. - Using a key that is not unique per revision. A label such as
apphas the same value in every revision, so it narrows nothing. - Repeating the key in
labelSelector. Do not list a key in bothlabelSelectorandmatchLabelKeys. The API server rejects this. - Expecting a rebalance.
matchLabelKeysonly changes what is counted at scheduling time. It does not move running pods.
Check the result
After a rollout, count pods per zone for the current revision:
kubectl get pods -l app=web -o wide
kubectl get nodes -L topology.kubernetes.io/zoneMatch the NODE column of the pods to the zone label of the nodes. The counts should differ by at most maxSkew.
Exercise
A Deployment uses a zone constraint with labelSelector app=web and no matchLabelKeys. During a rolling update, why might the new pods end up unevenly spread once the old pods are gone? How does adding pod-template-hash to matchLabelKeys fix this?
Show answer
The selector counts old and new pods together, so the scheduler balances the combined total. The old pods are then deleted, which can leave the new pods unbalanced. With pod-template-hash in matchLabelKeys, only pods from the same ReplicaSet revision are counted. The new pods are balanced among themselves, so the result stays even after the old pods are removed.