Node affinity is a property of Pods that attracts them to a set of nodes (either as a preference or a hard requirement).
Stuff you wanna know:
- Node affinity is conceptually similar to
nodeSelector
, allowing you to constrain which nodes your Pod can be scheduled on based on node labels. - There are two types of node affinity:
requiredDuringSchedulingIgnoredDuringExecution
: The scheduler can’t schedule the Pod unless the rule is met. This functions likenodeSelector
, but with a more expressive syntax.preferredDuringSchedulingIgnoredDuringExecution
: The scheduler tries to find a node that meets the rule. If a matching node is not available, the scheduler still schedules the Pod.
- You can specify node affinities using the
.spec.affinity.nodeAffinity
field in your Pod spec. NotIn
andDoesNotExist
allow you to define node anti-affinity behavior.- You can use node taints to repel Pods from specific nodes.
- If you specify both
nodeSelector
andnodeAffinity
, both must be satisfied for the Pod to be scheduled onto a node. - You can specify a
weight
between 1 and 100 for each instance of thepreferredDuringSchedulingIgnoredDuringExecution
affinity type. - When configuring multiple scheduling profiles, you can associate a profile with a node affinity, which is useful if a profile only applies to a specific set of nodes.
- Inter-pod affinity and anti-affinity allow you to constrain which nodes your Pods can be scheduled on based on the labels of Pods already running on that node, instead of the node labels.
- Inter-pod affinity and anti-affinity require substantial amount of processing which can slow down scheduling in large clusters significantly.
More stuff:
- Node affinity — https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity
- Node affinity weight — https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity-weight
- Node affinity per scheduling profile — https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#node-affinity-per-scheduling-profile
- Taints and Tolerations — https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/
- Assign Pods to Nodes using Node Affinity — https://kubernetes.io/docs/tasks/configure-pod-container/assign-pods-nodes-using-node-affinity/
- Scheduling, Preemption and Eviction — https://kubernetes.io/docs/concepts/scheduling-eviction/_print/#pg-ede4960b56a3529ee0bfe7c8fe2d09a5
- Advanced scheduling in Kubernetes — https://kubernetes.io/blog/2017/03/advanced-scheduling-in-kubernetes/
- Understanding node affinity — https://www.ibm.com/docs/en/openshift?source=https%3A%2F%2Fdocs.openshift.com%2Fcontainer-platform%2F4.5%2Fnodes%2Fscheduling%2Fnodes-scheduler-node-affinity.html&referrer=SSQNUZ_3.5.0%2Fsvc-dv%2Fdv-dednodes.html
- Advanced Scheduling and Node Affinity (Red Hat OpenShift) — https://docs.openshift.com/container-platform/3.11/admin_guide/scheduling/node_affinity.html
- Advanced Kubernetes pod to node scheduling — https://www.cncf.io/blog/2021/07/27/advanced-kubernetes-pod-to-node-scheduling/
- Best practices for advanced scheduler features in Azure Kubernetes Service (AKS) — https://docs.microsoft.com/en-us/azure/aks/operator-best-practices-advanced-scheduler