Node Affinity
Node affinity rules constrain which nodes a pod can be scheduled on based on node labels.
SSD
HDD
Pod prefers node with SSD disk
Mental Model
Node affinity is like having a preference for where you sit in a restaurant. "I prefer a window seat" (preferred) vs "I must sit in non-smoking" (required). Kubernetes respects these when placing pods.
YAML Example
apiVersion: v1
kind: Pod
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: zone
operator: In
values:
- us-west-1
containers:
- name: app
image: myappCommon Misconceptions
- ✗Affinity guarantees placement - FALSE: Only required affinity is mandatory
- ✗Affinity replaces nodeSelector - FALSE: Can be used together
- ✗Affinity is evaluated after scheduling - FALSE: It's used during filtering/scoring
Key Takeaways
- • Node affinity rules constrain which nodes a pod can be scheduled on based on node labels.
- • Node affinity is like having a preference for where you sit in a restaurant. "I prefer a window seat...
- • Check the YAML example for implementation details
- • Be aware of the common misconceptions listed above