✅ Summary Table with Field Mapping and Examples
Affinity Type | Required Field | Preferred Field |
---|---|---|
nodeAffinity | requiredDuringSchedulingIgnoredDuringExecution → nodeSelectorTerms | preferredDuringSchedulingIgnoredDuringExecution → preference |
podAffinity | requiredDuringSchedulingIgnoredDuringExecution → labelSelector, topologyKey | preferredDuringSchedulingIgnoredDuringExecution → podAffinityTerm |
podAntiAffinity | requiredDuringSchedulingIgnoredDuringExecution → labelSelector, topologyKey | preferredDuringSchedulingIgnoredDuringExecution → podAffinityTerm |
🔷 1. nodeAffinity
▶ Required Example
apiVersion: v1
kind: Pod
metadata:
name: required-node-affinity-pod
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: app
image: nginx
➡️ Behavior: Pod will only be scheduled on nodes with disktype=ssd
label.
▶ Preferred Example
apiVersion: v1
kind: Pod
metadata:
name: preferred-node-affinity-pod
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: app
image: nginx
➡️ Behavior: Pod will prefer nodes with disktype=ssd
, but can run on others.
🔷 2. podAffinity
▶ Required Example
apiVersion: v1
kind: Pod
metadata:
name: required-pod-affinity-pod
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: redis
topologyKey: "kubernetes.io/hostname"
containers:
- name: backend
image: busybox
command: ["sleep", "3600"]
➡️ Behavior: Pod will only run on the same node as pods labeled app=redis
.
▶ Preferred Example
apiVersion: v1
kind: Pod
metadata:
name: preferred-pod-affinity-pod
spec:
affinity:
podAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchLabels:
app: redis
topologyKey: "kubernetes.io/hostname"
containers:
- name: backend
image: busybox
command: ["sleep", "3600"]
➡️ Behavior: Pod will prefer to run near pods with label app=redis
, but it can run elsewhere.
🔷 3. podAntiAffinity
▶ Required Example
apiVersion: v1
kind: Pod
metadata:
name: required-pod-anti-affinity-pod
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: frontend
topologyKey: "kubernetes.io/hostname"
containers:
- name: frontend
image: nginx
➡️ Behavior: Pod will not be placed on the same node as another pod labeled app=frontend
.
▶ Preferred Example
apiVersion: v1
kind: Pod
metadata:
name: preferred-pod-anti-affinity-pod
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 50
podAffinityTerm:
labelSelector:
matchLabels:
app: frontend
topologyKey: "kubernetes.io/hostname"
containers:
- name: frontend
image: nginx
➡️ Behavior: Pod will try to avoid nodes with app=frontend
pods, but may still land there.
📌 Notes
topologyKey: kubernetes.io/hostname
= "same node"- You can use other topology keys like
topology.kubernetes.io/zone
for zone-aware placement. - Use required when pod must be scheduled in a certain way.
- Use preferred when it's a soft rule or optimization hint.