Affinity vs Anti-Affinity in Kubernetes: Key Differences and Usage
affinity rules guide pods to be scheduled close to specific nodes or pods, improving performance or resource sharing. Anti-affinity rules do the opposite by keeping pods apart to increase fault tolerance and avoid resource conflicts.Quick Comparison
This table summarizes the main differences between affinity and anti-affinity in Kubernetes.
| Factor | Affinity | Anti-Affinity |
|---|---|---|
| Purpose | Schedule pods close to certain nodes or pods | Schedule pods away from certain nodes or pods |
| Goal | Improve performance or resource sharing | Increase fault tolerance and reduce conflicts |
| Effect | Pods co-locate on same node or zone | Pods spread across nodes or zones |
| Use case example | Place pods near a database for low latency | Avoid placing replicas on the same node |
| Syntax key | podAffinity or nodeAffinity | podAntiAffinity |
| Impact on scheduling | Positive affinity preference or requirement | Negative affinity preference or requirement |
Key Differences
Affinity in Kubernetes lets you tell the scheduler to put pods close to other pods or nodes that match certain labels. This helps when pods need to share resources or communicate quickly, like placing an app pod near its database pod.
Anti-affinity works the opposite way. It tells the scheduler to keep pods apart from others with certain labels. This is useful to avoid single points of failure, for example, by spreading replicas of the same app across different nodes.
Both affinity and anti-affinity can be required (must be met) or preferred (nice to have). They use label selectors to match pods or nodes and can be applied at the pod or node level. The main difference is whether you want pods to be close together (affinity) or far apart (anti-affinity).
Code Comparison
Here is an example of podAffinity that schedules a pod close to pods with label app=database.
apiVersion: v1
kind: Pod
metadata:
name: frontend
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- database
topologyKey: "kubernetes.io/hostname"
containers:
- name: frontend
image: nginxAnti-Affinity Equivalent
This example uses podAntiAffinity to keep the pod away from others with label app=frontend, spreading replicas across nodes.
apiVersion: v1
kind: Pod
metadata:
name: frontend
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- frontend
topologyKey: "kubernetes.io/hostname"
containers:
- name: frontend
image: nginxWhen to Use Which
Choose affinity when your pods benefit from being close together, such as reducing network latency or sharing local storage. Use it to improve performance by co-locating related services.
Choose anti-affinity when you want to increase reliability and fault tolerance by spreading pods across different nodes or zones. This helps avoid downtime if a node fails and reduces resource contention.
In summary, use affinity to bring pods closer for efficiency, and anti-affinity to keep pods apart for safety.