Pod Affinity and Anti-Affinity Guide
Configure pod affinity and anti-affinity rules for Kubernetes scheduling. Co-locate cache with app, spread replicas across nodes.
π‘ Quick Answer: Use
podAffinityto co-locate pods (cache near app server) andpodAntiAffinityto spread replicas across nodes. PreferpreferredDuringSchedulingIgnoredDuringExecutionfor soft rules that donβt block scheduling.
The Problem
Your cache pods are on different nodes than the app servers that use them β adding 1-2ms network latency per request. Or all 3 replicas of a service landed on the same node β one node failure takes out the entire service.
The Solution
Co-Locate Cache with App
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-server
spec:
template:
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values: ["redis-cache"]
topologyKey: kubernetes.io/hostnameWeb server pods MUST schedule on nodes that already have redis-cache pods.
Spread Replicas Across Nodes
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-server
spec:
replicas: 3
template:
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchLabels:
app: api-server
topologyKey: kubernetes.io/hostnamePrefer different nodes for each replica. Soft rule β if only 2 nodes exist, 2 pods can share a node.
Hard Anti-Affinity (Strict Separation)
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: api-server
topologyKey: kubernetes.io/hostnameβ οΈ Hard anti-affinity with 3 replicas requires at least 3 nodes. Pods stay Pending if not enough nodes.
graph TD
subgraph Node 1
APP1[App Pod] --- CACHE1[Redis Cache]
end
subgraph Node 2
APP2[App Pod] --- CACHE2[Redis Cache]
end
subgraph Node 3
APP3[App Pod] --- CACHE3[Redis Cache]
end
AFFINITY[podAffinity:<br/>app near cache] --> APP1
ANTI[podAntiAffinity:<br/>replicas on different nodes] --> APP1 & APP2 & APP3Common Issues
Pods stuck Pending with hard anti-affinity
Not enough nodes to satisfy the rule. Switch to preferredDuringScheduling (soft) or add more nodes.
Affinity not working β pods on wrong nodes
Check label selectors match exactly. kubectl get pods --show-labels to verify pod labels match the affinity matchLabels.
Best Practices
- Soft anti-affinity for most services β prevents single-node concentration without blocking scheduling
- Hard affinity only for true co-location requirements β cache + app on same node
topologyKey: kubernetes.io/hostnamefor node-level separationtopologyKey: topology.kubernetes.io/zonefor zone-level separation- Prefer topology spread constraints for even distribution β more flexible than anti-affinity
Key Takeaways
- podAffinity co-locates pods (same node/zone); podAntiAffinity separates them
- Soft rules (
preferred) are scheduling preferences; hard rules (required) are mandatory - Hard anti-affinity requires enough topology domains β can cause Pending pods
- Use
topologyKeyto control scope: hostname (node), zone, rack - Topology spread constraints are often better than anti-affinity for even distribution

Recommended
Kubernetes Recipes β The Complete Book100+ production-ready patterns with detailed explanations, best practices, and copy-paste YAML. Everything in one place.
Get the Book βLearn by Doing
CopyPasteLearn β Hands-on Cloud & DevOps CoursesMaster Kubernetes, Ansible, Terraform, and MLOps with interactive, copy-paste-run lessons. Start free.
Browse Courses βπ Deepen Your Skills β Hands-on Courses
Courses by CopyPasteLearn.com β Learn IT by Doing
