Required vs Preferred Pod Affinity
You'll learn how hard and soft pod affinity rules differ and how each impacts Kubernetes scheduling decisions.
The Scheduling Spectrum
Pod affinity (and anti-affinity) rules come in two strengths: required and preferred. Think of required rules as non-negotiable requirements—like "must sit next to the emergency exit"—while preferred rules are wishes—"would like a window seat, but aisle is fine too."
Required Pod Affinity
requiredDuringSchedulingIgnoredDuringExecution enforces hard constraints. If the scheduler cannot find a node that satisfies your affinity rule (e.g., co-locate with pods labeled app=cache in the same zone), the pod remains Pending indefinitely. The scheduler will not compromise.
Use when: Compliance, latency, or data locality requirements are absolute. For example, a payment processor must run in the same zone as its database for regulatory reasons.
Preferred Pod Affinity
preferredDuringSchedulingIgnoredDuringExecution expresses soft preferences with a weight (1-100). The scheduler tries to honor these rules but will still place the pod elsewhere if necessary. Multiple preferences are summed, and the node with the highest score wins.
Use when: You want optimization but not at the cost of availability. For example, preferring to co-locate a web frontend with its cache improves performance, but if no matching node is available, scheduling elsewhere keeps your service running.
Scheduling Impact
- Required rules reduce scheduling flexibility and can cause pod starvation
- Preferred rules guide optimization while maintaining cluster utilization
- Combine both: required for critical constraints, preferred for performance tuning
Key Takeaway: Use requiredDuringScheduling... when affinity is mandatory and pods should stay pending rather than violate placement rules; use preferredDuringScheduling... for optimization hints that yield to availability when necessary.