Node affinity is a set of rules used by Kubernetes to schedule pods onto nodes based on node labels. It helps pods prefer or require certain nodes to run on.
Click to reveal answer
beginner
What does node anti-affinity do?
Node anti-affinity tells Kubernetes to avoid placing pods on nodes with certain labels. It helps spread pods across nodes to improve availability.
Click to reveal answer
intermediate
What are the two types of node affinity in Kubernetes?
The two types are requiredDuringSchedulingIgnoredDuringExecution (hard requirement) and preferredDuringSchedulingIgnoredDuringExecution (soft preference).
Click to reveal answer
intermediate
How does Kubernetes treat requiredDuringSchedulingIgnoredDuringExecution node affinity?
It treats it as a hard rule. Pods will only be scheduled on nodes that meet the specified label criteria.
Click to reveal answer
beginner
Give a simple example of node affinity in a pod spec.
Required node affinity uses the key requiredDuringSchedulingIgnoredDuringExecution with nodeSelectorTerms and matchExpressions to specify node labels.
Step 2: Check options for correct structure
"nodeAffinity": {"requiredDuringSchedulingIgnoredDuringExecution": {"nodeSelectorTerms": [{"matchExpressions": [{"key": "disktype", "operator": "In", "values": ["ssd"]}]}]}} matches the required node affinity syntax. "nodeAffinity": {"preferredDuringSchedulingIgnoredDuringExecution": [{"weight": 1, "preference": {"matchExpressions": [{"key": "disktype", "operator": "In", "values": ["ssd"]}]}}]} is preferred affinity, C is nodeSelector (different syntax), and D is pod affinity, not node affinity.
C. Using preferred instead of required anti-affinity allows scheduling on same node
D. topologyKey should be set to "zone" instead of hostname
Solution
Step 1: Understand pod anti-affinity types
Preferred anti-affinity suggests avoiding co-location but does not enforce it strictly. Required anti-affinity enforces pods not to be scheduled together.
Step 2: Analyze the pod spec anti-affinity type
The spec uses preferredDuringSchedulingIgnoredDuringExecution, which is a soft rule. This allows pods to be scheduled on the same node if needed.
Final Answer:
Using preferred instead of required anti-affinity allows scheduling on same node -> Option C
Quick Check:
Preferred anti-affinity is a soft rule [OK]
Hint: Preferred anti-affinity is soft; use required to enforce [OK]
Common Mistakes:
Confusing preferred and required anti-affinity
Assuming labelSelector syntax is wrong without checking
Thinking topologyKey must be zone for anti-affinity
5. You want to ensure pods with label app=frontend never run on the same node as pods with label app=backend. Which affinity configuration achieves this?
hard
A. Use podAntiAffinity with requiredDuringSchedulingIgnoredDuringExecution and labelSelector matching app=backend
B. Use nodeAffinity with requiredDuringSchedulingIgnoredDuringExecution and labelSelector matching app=backend
C. Use podAffinity with preferredDuringSchedulingIgnoredDuringExecution and labelSelector matching app=backend
D. Use nodeSelector with label app=backend
Solution
Step 1: Identify requirement to prevent co-location of pods
Pod anti-affinity prevents pods from running on the same node as other pods matching certain labels.
Step 2: Choose required anti-affinity for strict enforcement
Using requiredDuringSchedulingIgnoredDuringExecution enforces pods never co-locate on the same node.
Step 3: Match labelSelector to pods to avoid
The labelSelector should match pods with app=backend to prevent frontend pods from scheduling on same node.
Final Answer:
Use podAntiAffinity with requiredDuringSchedulingIgnoredDuringExecution and labelSelector matching app=backend -> Option A
Quick Check:
Pod anti-affinity + required = strict pod separation [OK]
Hint: Use required podAntiAffinity to strictly separate pods [OK]
Common Mistakes:
Using nodeAffinity instead of podAntiAffinity for pod separation
Using preferred instead of required anti-affinity for strict rules
Confusing podAffinity with podAntiAffinity
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.