Hands-on Lab·Certified Kubernetes Administrator
Node Affinity: Required and Preferred
nodeSelector is all or nothing. Node affinity adds expressions and, more importantly, a soft form. Run a required rule that pins every replica to one node, then a preferred rule whose condition matches nothing at all and watch all four replicas schedule anyway.
Scheduling and Placement Guide 40 of 103 Intermediate
- Kubernetes1.36.4
- Cluster4 nodes
- Runtimecontainerd 2.2.6
- CNICalico v3.32.1
- TimeAbout 30 min
Node names are this lab's. The placement outcomes are the real scheduler results from this run.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| CKA1001 | 192.168.0.175 | Ubuntu 26.04 LTS | Control Plane Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE01 | 192.168.0.176 | Ubuntu 26.04 LTS | Worker Node (zone=a) | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE02 | 192.168.0.177 | Ubuntu 26.04 LTS | Worker Node (zone=b) | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE03 | 192.168.0.178 | Ubuntu 26.04 LTS | Worker Node (unlabelled) | 2 Core | 4 GB | 50 GB |
This guide includes
Use this when nodeSelector is too blunt, and a node should be preferred rather than required. This matters because a required rule leaves Pods Pending when nothing matches while a preferred rule schedules anyway, and choosing the wrong one is how workloads silently strand.
- labelling two nodes and deliberately leaving a third one bare
- using
requiredDuringSchedulingIgnoredDuringExecutionas a nodeSelector with better grammar - writing a preferred rule that matches nothing at all and watching it schedule anyway
- seeing why that unlabelled third node is the part of the setup that proves the difference
Before you start
- At least three workers, so a spread is visible.
- The Labels and Selectors guide. Affinity is built on the same expressions.
- Permission to label nodes.
-
Label two nodes, leave one bare
-
Required affinity behaves like a nodeSelector with better grammar
-
Preferred affinity that matches nothing, and schedules anyway
-
Clean up