0
0
Kubernetesdevops~10 mins

Pod affinity and anti-affinity in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Pod affinity and anti-affinity
Pod Scheduling Request
Check Pod Affinity Rules
Match Affinity
Check Pod Anti-Affinity Rules
Reject Node
Pod Scheduled on Node
When Kubernetes schedules a pod, it checks affinity and anti-affinity rules to decide which node is suitable. Affinity prefers nodes with matching pods; anti-affinity avoids nodes with certain pods.
Execution Sample
Kubernetes
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - frontend
        topologyKey: kubernetes.io/hostname
This pod requests to be scheduled on a node that already runs pods with label app=frontend.
Process Table
StepPod Affinity CheckPod Anti-Affinity CheckNode DecisionReason
1Check if node has pods with label app=frontendCheck if node has pods with conflicting labelsPass affinity checkNode has pods with app=frontend label
2N/ANo pods with conflicting labels foundPass anti-affinity checkNo anti-affinity conflict on node
3N/AN/APod scheduledNode meets affinity and anti-affinity rules
4Check next nodeCheck anti-affinityFail affinity checkNode lacks pods with app=frontend label
5N/AN/APod not scheduled on this nodeAffinity rule not met
💡 Pod scheduled on first node that passes both affinity and anti-affinity checks
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Node Affinity Matchfalsetruetruetruetrue
Node Anti-Affinity Conflictfalsefalsefalsefalsefalse
Pod Scheduledfalsefalsefalsetruetrue
Key Moments - 2 Insights
Why does the pod fail to schedule on a node without matching affinity labels?
Because the affinity rule requires the node to have pods with label app=frontend, nodes without this label fail the affinity check as shown in execution_table row 4.
What happens if a node has pods that violate anti-affinity rules?
The node is rejected during the anti-affinity check, preventing the pod from scheduling there, as indicated in execution_table row 2 if anti-affinity conflict was true.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the pod get scheduled?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
Refer to the 'Node Decision' column where 'Pod scheduled' appears at Step 3.
According to the variable tracker, what is the value of 'Node Anti-Affinity Conflict' after Step 2?
Aundefined
Btrue
Cfalse
Dnull
💡 Hint
Check the 'Node Anti-Affinity Conflict' row under 'After Step 2' in variable_tracker.
If the pod affinity rule was removed, how would the scheduling change in the execution table?
APod would never schedule
BPod would schedule on any node without affinity check
CPod would fail anti-affinity check always
DPod would schedule only on nodes with anti-affinity conflicts
💡 Hint
Without affinity rules, the affinity check step would be skipped, allowing scheduling on any node passing anti-affinity.
Concept Snapshot
Pod affinity and anti-affinity guide Kubernetes where to place pods.
Affinity prefers nodes with certain pods.
Anti-affinity avoids nodes with conflicting pods.
Rules use labels and topology keys.
Pods schedule only on nodes passing both checks.
Full Transcript
Pod affinity and anti-affinity are rules Kubernetes uses to decide where to place pods. When scheduling, Kubernetes checks if nodes have pods matching affinity labels to prefer those nodes. It also checks anti-affinity rules to avoid nodes with pods that should not be colocated. If a node passes both checks, the pod is scheduled there. This process ensures pods run close to or away from certain other pods based on labels and topology keys.