Affinity, taints and tolerations
Separate requirements from permission
The catalog team wants accelerator-pool machines for a special worker. A node label describes the pool; a Pod nodeSelector requires that label. A NoSchedule taint on accelerator nodes discourages ordinary workloads by blocking new Pods without a matching toleration. These mechanisms work together: the label selects, the toleration removes a particular obstacle. No GPU is actually requested in this teaching example; device resource allocation is a later lesson.
Read a combined placement policy
The static manifest selects accelerator nodes, tolerates the dedicated taint, and expresses a preference for fast disks. In the synthetic node table, plain is excluded by the required pool label; gpu-a and gpu-b pass; gpu-c still has an unmatched maintenance taint. The fast disk preference contributes to scoring but does not guarantee gpu-a wins. No labels or taints should be changed on real nodes for this example.
apiVersion: v1kind: Podmetadata: name: catalog-accelerator namespace: schedule-study labels: app: catalog-workerspec: automountServiceAccountToken: false restartPolicy: Never activeDeadlineSeconds: 120 securityContext: runAsNonRoot: true runAsUser: 10001 seccompProfile: type: RuntimeDefault containers: - name: worker image: example.invalid/bytemind/worker:offline resources: requests: cpu: 400m memory: 256Mi limits: cpu: 500m memory: 384Mi securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true capabilities: drop: - ALL nodeSelector: lab.example/pool: accelerator affinity: nodeAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 60 preference: matchExpressions: - key: lab.example/disk operator: In values: - fast tolerations: - key: lab.example/dedicated operator: Equal value: accelerator effect: NoScheduleExpress hard and soft affinity
nodeSelector is an exact label map; all its entries must match. Required node affinity supports expression-based requirements such as In; multiple required nodeSelectorTerms are alternatives, and expressions within a term are combined. If nodeSelector and required affinity are both configured, both must pass. Preferred affinity contributes to ranking instead of excluding nodes. IgnoredDuringExecution means changing a matching label later does not itself require eviction under that affinity rule.
Place relative to other Pods
Pod affinity asks to place workloads near selected Pods; anti-affinity expresses separation. A topologyKey identifies the node label used to define proximity, such as kubernetes.io/hostname. Here the partial preferred anti-affinity rule favors a different host from catalog-worker Pods in the same namespace. The weight is a preference, not a probability. Hard anti-affinity can make a new replica unschedulable; topology labels must be consistent. Namespace selectors and admission limits are separate details to verify.
affinity: podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 80 podAffinityTerm: labelSelector: matchLabels: app: catalog-worker topologyKey: kubernetes.io/hostnameKnow what each taint effect means
NoSchedule prevents new scheduling without a match; PreferNoSchedule expresses avoidance rather than a hard ban; NoExecute also affects existing Pods and can cause eviction, subject to tolerations and their duration. Multiple unmatched taints can matter. A toleration does not reserve a node or override resource constraints. Avoid wide Exists tolerations unless the workload has a justified need; node placement labels and taints alone are not authorization boundaries.
Practice
Use affinity-cases.json. Remove the selector in your worksheet while keeping the toleration: plain becomes eligible too. Restore the selector and remove the toleration: none of the accelerator nodes pass. Compare both outcomes before reviewing the answer key.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Affinity, taints and tolerations — knowledge check
Apply scheduling concepts using synthetic offline scenarios.
What does a matching NoSchedule toleration do?
Which field expresses a hard exact label requirement?
gpu-c has two NoSchedule taints and only one is tolerated. What follows?
What does preferred Pod anti-affinity express?
Which effect can also evict existing Pods that do not tolerate it?