affinity-and-anti-affinity.md (1534B)
1 +++ 2 title = 'Affinity and anti-affinity' 3 +++ 4 # Affinity and anti-affinity 5 Scheduler takes care of placing pods on nodes to consume cluster resources in reasonable way. 6 But you can control how the scheduler does that by using labesls and selectors. 7 8 For example to: 9 - ensure that certain group of pods ends up on nodes with specific type of hardware (use `nodeSelector` or `nodeAffinity`) 10 - ensure that REST API and database pod end up on same node to reduce network latency during REST API calls to database (use inter-pod affinity `podAffinity`) 11 - ensure that two replicas of database pod don't end up on the same node, to increase high availability if a node fails (inter-pod anti-affinity `podAntiAffinity`) 12 13 Example: 14 15 ```yaml 16 apiVersion: v1 17 kind: Pod 18 metadata: 19 name: nginx 20 labels: 21 env: test 22 spec: 23 containers: 24 - name: nginx 25 image: nginx 26 nodeSelector: 27 size: large 28 ``` 29 30 31 Taints: features of Nodes allowing nodes to repel pods 32 - add taint with `kubectl taint nodes node1 key1=value1:NoSchedule` (taint effect is `NoSchedule`, so no pod will be scheduled on node1 unless it has a toleration that matches key and label) 33 - effects are NoSchedule, PreferNoSchedule, and NoExecute (pod will be evicted from the node if already running on it) 34 - to remove, `kubectl taint nodes node1 key1=value1:NoSchedule-` 35 36 Tolerations: allow specific pod to be scheduled on a node despite of its taint 37 - toleration "matches" a taint if keys are same + effects are same + operator is Exists or (operator is Equal and values equal)