What if your apps could pick their best homes all by themselves, avoiding crashes and slowdowns?
Why Node affinity and anti-affinity in Kubernetes? - Purpose & Use Cases
Imagine you have many servers and you want to decide exactly which server each app should run on by hand.
You write down notes and try to remember which app goes where every time you add or move apps.
This manual way is slow and confusing.
You might put two apps that should be apart on the same server by mistake.
Or you waste time moving apps around to fix problems.
Node affinity and anti-affinity let you tell Kubernetes rules about where apps should or should not run.
Kubernetes then automatically places apps on the right servers for you.
Manually track app placement in notes and move apps by hand
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: 'zone'
operator: In
values:
- 'us-east-1a'
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: 'app'
operator: In
values:
- 'myapp'
topologyKey: 'kubernetes.io/hostname'You can easily control app placement to improve performance, reliability, and resource use without manual work.
A company runs a web app and a database. They use node affinity to put the database on servers with fast disks, and anti-affinity to keep web app copies on different servers so if one server fails, others keep running.
Manual app placement is slow and error-prone.
Node affinity and anti-affinity automate smart app placement rules.
This improves app reliability and resource use without extra work.