📖 Scenario: You are managing a Kubernetes cluster for a small company. You want to control where your application pods run by using node affinity and anti-affinity rules. This helps keep your app stable and balanced across the cluster.
🎯 Goal: Learn how to write a Kubernetes pod specification that uses node affinity and anti-affinity to schedule pods on specific nodes or avoid certain nodes.
📋 What You'll Learn
Create a pod manifest with a basic container
Add node affinity rules to prefer nodes with a specific label
Add node anti-affinity rules to avoid nodes with another label
Display the final pod manifest YAML
💡 Why This Matters
🌍 Real World
Node affinity and anti-affinity help control where pods run in a Kubernetes cluster. This is useful to improve performance, reliability, and resource use by placing pods on suitable nodes or avoiding problematic ones.
💼 Career
Understanding node affinity and anti-affinity is important for Kubernetes administrators and DevOps engineers to manage cluster workloads effectively and ensure application stability.
Progress0 / 4 steps
1
Create a basic pod manifest
Create a Kubernetes pod manifest named pod.yaml with a pod named myapp-pod that runs the container image nginx:latest. Include the apiVersion, kind, metadata, and spec sections with the container definition.
Kubernetes
Hint
Start with the basic pod YAML structure. Use apiVersion: v1 and kind: Pod. Add metadata with the pod name and a spec with one container running nginx.
2
Add node affinity to prefer nodes with label
Add a nodeAffinity section under spec to prefer scheduling the pod on nodes with the label disktype: ssd. Use preferredDuringSchedulingIgnoredDuringExecution with a weight of 1 and a match expression for the label key disktype and value ssd.
Kubernetes
Hint
Under spec, add affinity with nodeAffinity. Use preferredDuringSchedulingIgnoredDuringExecution with a weight and a match expression for the label disktype: ssd.
3
Add node anti-affinity to avoid nodes with label
Add a nodeAffinityrequiredDuringSchedulingIgnoredDuringExecution rule to avoid scheduling the pod on nodes with the label zone: east. Use a matchExpressions with key zone, operator NotIn, and value east.
Kubernetes
Hint
Under nodeAffinity, add requiredDuringSchedulingIgnoredDuringExecution with a nodeSelectorTerms list. Use a matchExpressions to exclude nodes labeled zone: east.
4
Display the final pod manifest
Print the complete pod manifest YAML to the console exactly as written in the previous steps.
Kubernetes
Hint
Use a print statement to output the full pod manifest YAML exactly as defined.
Practice
(1/5)
1. What does nodeAffinity in Kubernetes primarily control?
easy
A. Which nodes a pod can be scheduled on based on node labels
B. The order in which pods start in a deployment
C. The network policies applied to pods
D. The resource limits of a pod
Solution
Step 1: Understand nodeAffinity purpose
Node affinity is used to specify rules about which nodes a pod can run on, based on labels assigned to nodes.
Step 2: Compare options to nodeAffinity function
Options B, C, and D relate to pod startup order, network policies, and resource limits, which are unrelated to node affinity.
Final Answer:
Which nodes a pod can be scheduled on based on node labels -> Option A
Quick Check:
nodeAffinity controls pod placement on nodes [OK]
Hint: Node affinity = pod placement by node labels [OK]
Common Mistakes:
Confusing node affinity with pod resource limits
Thinking node affinity controls pod startup order
Mixing node affinity with network policies
2. Which of the following is the correct syntax to specify a required 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.