Node Affinity
Actually, you can combine taints and tolerations with node affinity to schedule pods on specific nodes. Remember, you need to apply taints and tolerations to the nodes first before you use node affinity. For example, you can apply a taint to your node that has a large GPU to prevent other pods from being scheduled on your node. Then you can use node affinity to prevent your pods from being scheduled on other nodes.
Concept and Usage of Node Affinity
The concept of Node Affinity is basically allows the users to specify rules for placing/scheduling pods on specific nodes. For example, if we want to schedule a pod on a node that has a large or medium GPU. We can use Node Affinity to specify the rules for scheduling the pod on the node that has a large or medium GPU.
apiVersion: v1
kind: Pod
metadata:
name: sample-pod
spec:
containers:
- name: sample-container
image: nginx
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: gpu
operator: In # Got other operators like NotIn, Exists
values:
- large
- medium
- Operator Reference (opens in a new tab)
Exists
operator is used to check if the label exists on the node. So, when you setExists
, then you don't need to specify thevalues
field, as it does not compare the values.
Node Affinity Types
Reference (opens in a new tab)
Let me explain DuringScheduling and DuringExecution.
-
DuringScheduling
- The pod does not exist on the node, so the rules must be met for the pod to be scheduled on the node.requiredDuringScheduling
will be used when the pod must be scheduled on the node that satisfies the rule.preferredDuringScheduling
will be used when the pod is less important to be scheduled on the node that satisfies the rule, as if the node does not satisfy the rule, then the pod will be scheduled on the other node.
-
DuringExecution
- The pod has been running on the node, so the rules must be met for the pod to continue running on the node. For example, if the admin changes the node label, what happen to the existing pods that being deployed?RequiredDuringExecution
- The pod will be evicted if the node label is changed or the pod does not satisfy the rule.IgnoredDuringExecution
- The pod will continue to run, even if the node label is changed or the pod does not satisfy the rule.
Node Affinity Types:
-
requiredDuringSchedulingIgnoredDuringExecution
- The pod must be scheduled on the node that satisfies the rule, if does not satisfy the rule, then the pod will not be scheduled on the node. Then, if the node labels are changed, the pod will still continue to run (IgnoredDuringExecution). -
preferredDuringSchedulingIgnoredDuringExecution
- The pod is less important to be scheduled on the node that satisfies the rule, if does not satisfy the rule, then the pod will be scheduled on the other node. Then, if the node labels are changed, the pod will still continue to run (IgnoredDuringExecution). -
requiredDuringSchedulingRequiredDuringExecution
- The pod must be scheduled on the node that satisfies the rule, if does not satisfy the rule, then the pod will not be scheduled on the node. Then, if the node labels are changed, the pod will be evicted. -
preferredDuringSchedulingRequiredDuringExecution
- The pod is less important to be scheduled on the node that satisfies the rule, if does not satisfy the rule, then the pod will be scheduled on the other node. Then, if the node labels are changed, the pod will be evicted.