Question:
In Kubernetes, arepodAffinity
and podAntiAffinity
weights compared to each other? Or independently? What about podAffinity
and nodeAffinity
? Would podAntiAffinity
outweight the podAffinity
in the below example? And what if nodeAffinity
was added to the mix as well. affinity:
podAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 50
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app.kubernetes.io/name
operator: In
values:
- test-1
topologyKey: kubernetes.io/hostname
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app.kubernetes.io/name
operator: In
values:
- test-2
topologyKey: kubernetes.io/hostname
Answer:
All affinity settings for both pods and nodes are weighed the same way if you don’t overwrite the default individual weight of 1.During execution the occurrence of affinity conditions on a node is multiplied by the weight of the condition.
pod a has an affinity to b
pod a has an anti-affinity to c
new pod of type "a" to be scheduled
---
node1 runs 3 "b" pods and 1 "c" pod --> affinity score: 2 (3 "b" pods - 1 "c" pod)
node2 runs 1 "b" pod --> affinity score 1: 1 (1 "b" pod)
Based on the affinity calculation the pod will be scheduled on node1 despite it already running pod “c” as 3 “b” pods outweigh 1 “c” pod.In your example the anti-affinity would outweigh the affinity settings if there is a 1:1 ratio of
test-1
to test-2
on a node. With a 2:1 ratio of test-1
to test-2
on a node the (anti-)affinities would cancel each other out.Source
If you have better answer, please add a comment about this, thank you!
Source: Stackoverflow.com
If you like this answer, you can give me a coffee by click here (view Ads)
Leave a Review