Question:In Kubernetes, are
podAntiAffinityweights compared to each other? Or independently? What about
podAffinityin the below example? And what if
nodeAffinitywas 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.
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.
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)
In your example the anti-affinity would outweigh the affinity settings if there is a 1:1 ratio of
test-2on a node. With a 2:1 ratio of
test-2on a node the (anti-)affinities would cancel each other out.
If you have better answer, please add a comment about this, thank you!
If you like this answer, you can give me a coffee by click here (view Ads)