K8s Node Affinity Explained

๐ 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:
Type | What it means |
requiredDuringSchedulingIgnoredDuringExecution | Hard Rule โ Must follow |
preferredDuringSchedulingIgnoredDuringExecution | Soft 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
Feature | Node Selector | Node Affinity |
Syntax | Basic | Flexible |
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
Subscribe to my newsletter
Read articles from TheTansih directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
