What if your apps could pick their best homes all by themselves, avoiding crashes and slowdowns?
Why Node affinity and anti-affinity in Kubernetes? - Purpose & Use Cases
Imagine you have many servers and you want to decide exactly which server each app should run on by hand.
You write down notes and try to remember which app goes where every time you add or move apps.
This manual way is slow and confusing.
You might put two apps that should be apart on the same server by mistake.
Or you waste time moving apps around to fix problems.
Node affinity and anti-affinity let you tell Kubernetes rules about where apps should or should not run.
Kubernetes then automatically places apps on the right servers for you.
Manually track app placement in notes and move apps by hand
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: 'zone'
operator: In
values:
- 'us-east-1a'
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: 'app'
operator: In
values:
- 'myapp'
topologyKey: 'kubernetes.io/hostname'You can easily control app placement to improve performance, reliability, and resource use without manual work.
A company runs a web app and a database. They use node affinity to put the database on servers with fast disks, and anti-affinity to keep web app copies on different servers so if one server fails, others keep running.
Manual app placement is slow and error-prone.
Node affinity and anti-affinity automate smart app placement rules.
This improves app reliability and resource use without extra work.
Practice
nodeAffinity in Kubernetes primarily control?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 AQuick Check:
nodeAffinity controls pod placement on nodes [OK]
- Confusing node affinity with pod resource limits
- Thinking node affinity controls pod startup order
- Mixing node affinity with network policies
Solution
Step 1: Identify required node affinity syntax
Required node affinity uses the keyrequiredDuringSchedulingIgnoredDuringExecutionwithnodeSelectorTermsandmatchExpressionsto 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.Final Answer:
"nodeAffinity": {"requiredDuringSchedulingIgnoredDuringExecution": {"nodeSelectorTerms": [{"matchExpressions": [{"key": "disktype", "operator": "In", "values": ["ssd"]}]}]}} -> Option DQuick Check:
Required node affinity uses requiredDuringSchedulingIgnoredDuringExecution [OK]
- Confusing preferred and required affinity syntax
- Using podAffinity instead of nodeAffinity
- Mixing nodeSelector with nodeAffinity syntax
zone: us-east-1a but no label disk: ssd?
{
"affinity": {
"nodeAffinity": {
"requiredDuringSchedulingIgnoredDuringExecution": {
"nodeSelectorTerms": [
{
"matchExpressions": [
{"key": "zone", "operator": "In", "values": ["us-east-1a"]},
{"key": "disk", "operator": "In", "values": ["ssd"]}
]
}
]
}
}
}
}Solution
Step 1: Analyze required node affinity conditions
Required node affinity means all match expressions must be true for scheduling. The node must have bothzone: us-east-1aanddisk: ssdlabels.Step 2: Check node labels against requirements
The node haszone: us-east-1abut lacksdisk: ssd. This fails the required condition, so the pod cannot be scheduled there.Final Answer:
The pod will not be scheduled on the node because it lacks the required disk label -> Option BQuick Check:
Required affinity needs all labels matched [OK]
- Assuming partial label match is enough
- Confusing required with preferred affinity
- Thinking pod schedules with warnings on missing labels
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchLabels:
app: myapp
topologyKey: "kubernetes.io/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 usespreferredDuringSchedulingIgnoredDuringExecution, 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 CQuick Check:
Preferred anti-affinity is a soft rule [OK]
- Confusing preferred and required anti-affinity
- Assuming labelSelector syntax is wrong without checking
- Thinking topologyKey must be zone for anti-affinity
app=frontend never run on the same node as pods with label app=backend. Which affinity configuration achieves this?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
UsingrequiredDuringSchedulingIgnoredDuringExecutionenforces pods never co-locate on the same node.Step 3: Match labelSelector to pods to avoid
The labelSelector should match pods withapp=backendto prevent frontend pods from scheduling on same node.Final Answer:
Use podAntiAffinity with requiredDuringSchedulingIgnoredDuringExecution and labelSelector matchingapp=backend-> Option AQuick Check:
Pod anti-affinity + required = strict pod separation [OK]
- Using nodeAffinity instead of podAntiAffinity for pod separation
- Using preferred instead of required anti-affinity for strict rules
- Confusing podAffinity with podAntiAffinity
Ready to prove it?
Pass the test and this pattern moves unknown
