Sub topic 9
Which nodes count: affinity and taint policies
This chapter assumes you can write a topologySpreadConstraints block and calculate skew by hand. It also assumes you know how node affinity, nodeSelector, and taints with tolerations work.
Which nodes count
The scheduler calculates skew from a set of eligible domains. A domain with no pods still counts as zero, so it can pull the minimum down. Two fields decide which nodes contribute domains and pods to that calculation.
| Field | Values | Default | Effect |
|---|---|---|---|
| `nodeAffinityPolicy` | `Honor`, `Ignore` | `Honor` | `Honor` counts only nodes that match the pod's `nodeSelector` and required node affinity. `Ignore` counts every node. |
| `nodeTaintsPolicy` | `Honor`, `Ignore` | `Ignore` | `Honor` skips nodes whose taints the pod does not tolerate. `Ignore` counts them anyway. |
The nodeTaintsPolicy default matters. Suppose a zone contains only tainted nodes, such as dedicated GPU nodes, and your pod cannot tolerate the taint. With Ignore, that zone still counts as a domain with zero pods. The skew then looks permanently high, and a DoNotSchedule constraint can leave pods Pending. Setting Honor removes those nodes from the calculation.
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
nodeAffinityPolicy: Honor
nodeTaintsPolicy: Honor
labelSelector:
matchLabels:
app: webThis constraint measures balance only across zones that hold nodes the pod can actually use.
Setting cluster-wide default constraints
You can give the scheduler a default spread behavior for pods that do not declare their own. Defaults go in the PodTopologySpread plugin arguments of the scheduler configuration.
apiVersion: kubescheduler.config.k8s.io/v1
kind: KubeSchedulerConfiguration
profiles:
- schedulerName: default-scheduler
pluginConfig:
- name: PodTopologySpread
args:
defaultingType: List
defaultConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnywayRules for defaults:
- They apply only to pods with no
topologySpreadConstraintsof their own. - Leave
labelSelectorout. The scheduler builds it from the Service, ReplicaSet, StatefulSet, or ReplicationController that owns the pod. A pod outside all of these gets no default spreading. defaultingType: Listmakes your list replace the built-in defaults. The default value,System, uses the built-in constraints:maxSkew: 3on zone andmaxSkew: 5on hostname, bothScheduleAnyway.- To turn defaults off, set
defaultingType: Listwith an emptydefaultConstraintslist.
Exercise
A Deployment spreads across three zones with DoNotSchedule and maxSkew: 1. Zone c contains only nodes tainted dedicated=gpu:NoSchedule, and the pods do not tolerate that taint. New pods stay Pending after zones a and b each hold one pod more than zone c. What is the cause, and which change fixes it?
Show answer
With the default nodeTaintsPolicy: Ignore, zone c still counts as a domain with zero pods, so placing another pod in a or b would make skew 2. The pods cannot go to c because of the taint. Setting nodeTaintsPolicy: Honor removes the tainted nodes from the calculation, so zone c is no longer counted and skew is measured only between a and b.