K8s Node Affinity Explained

TheTansihTheTansih
2 min read

๐Ÿš€ Intro: Why Pods Need Preference?

Imagine youโ€™re allocating rooms to guests in a hotel:

  • Some want sea view ๐ŸŒŠ

  • Some are okay with any room, but prefer corner ones ๐Ÿ›๏ธ

Similarly, in Kubernetes โ€” Pods sometimes need to run on specific nodes.

Thatโ€™s where Node Affinity comes in.


๐ŸŽฏ What is Node Affinity?

Node Affinity tells the Kubernetes scheduler:

"Hey, please place my Pod only on a node that matches certain labels."

It gives us control over Pod placement.

You define the labels that a Pod's node must (or should) have.

๐Ÿงฑ Types of Node Affinity:

TypeWhat it means
requiredDuringSchedulingIgnoredDuringExecutionHard Rule โ†’ Must follow
preferredDuringSchedulingIgnoredDuringExecutionSoft Rule โ†’ Best effort

๐Ÿ› ๏ธ Example 1: Hard Rule

I want this pod to run only on SSD-based nodes:

If no node has disktype=ssd, the pod wonโ€™t run ๐Ÿšซ


โœจ Example 2: Soft Rule

I prefer my pod to run in zone us-east-1a:

If zone us-east-1a is available, great! If not, no problem โ€” pod will still run.

๐Ÿ” Node Affinity vs Node Selector

FeatureNode SelectorNode Affinity
SyntaxBasicFlexible
ExpressionsโŒ Noโœ… Yes
Soft PreferenceโŒ Noโœ… Yes

๐Ÿ“Œ CKA Exam Tip

Expect YAML-based MCQs like:

  • Which node will this pod schedule on?

  • What happens if no node matches?

  • Difference between required vs preferred?


๐Ÿ’ญ My Thoughts

Node Affinity feels like setting boundaries for your pods โ€”
"Run where youโ€™re most productive" ๐Ÿ˜„
CKA syllabus has so much depth, but these concepts really help you think like a schedule

0
Subscribe to my newsletter

Read articles from TheTansih directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

TheTansih
TheTansih