liveBook · Manning

Affinity

As explained in Acing the Certified Kubernetes Administrator Exam, Second Edition

Node affinity is a Kubernetes scheduling technique that expresses preferences or requirements for placing pods on nodes.

Node affinity lets the Kubernetes scheduler express required or preferred node placement through node labels. Unlike a simple node selector, it can prefer labeled nodes while still allowing scheduling elsewhere when the preferred nodes are unavailable. section 4.1.2 section 4.3

Choose the rule type according to the placement guarantee required:

  • Use requiredDuringSchedulingIgnoredDuringExecution for a hard rule; the scheduler cannot place the pod unless the node-label requirement is satisfied. section 4.1.2 section 4.3
  • Use preferredDuringSchedulingIgnoredDuringExecution for a soft preference; the scheduler may choose another node when no matching node is available. section 4.1.2 section 4.3
  • Assign labels to nodes, then use affinity expressions in the pod YAML to influence placement in a changing cluster. section 4.1.2 section 9.3.3

Create and verify an affinity pod

Generate a starter manifest with kubectl run affinity --image nginx --dry-run=client -o yaml > affinity.yaml. Add affinity.nodeAffinity and the requiredDuringSchedulingIgnoredDuringExecution or preferredDuringSchedulingIgnoredDuringExecution rule beneath spec, then apply it with kubectl apply -f affinity.yaml. Verify placement using kubectl get po -o wide; the resulting output shows the pod's assigned node. section 4.1.2

Figure 4.8 Affinity prefers the pod be scheduled to a node with the label SSD but will schedule to node LINUX if none exist.
figure

Visualizes preferred placement on a labeled SSD node with fallback scheduling to another node. section 4.1.2

How Acing the Certified Kubernetes Administrator Exam, Second Edition covers it

How deep it goes
Explains hard and soft rules, weights, YAML placement, and verification, but does not provide the complete affinity manifest.
Examples
Uses an nginx pod manifest, affinity.yaml, kubectl apply, and kubectl get po -o wide output.
What it assumes
Assumes familiarity with pods, nodes, labels, scheduling, and basic kubectl YAML workflows.
What you can do after
Operate/configure pod placement with node labels, affinity YAML, kubectl, and observable scheduling results.
How it connects
Connects node labels, node selectors, node names, pod affinity, and scheduling.

Also explained in

Compare how 6 books explain affinity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Acing the Certified Kubernetes Administrator Exam, Second Edition. The section links point to the parts of the book it draws on.