Taints and Tolerations
- Taints and Toleration are used to describe what pods can be scheduled on what node
- They wont force a pod to schedule on the node that has a certain taint that cant be tolerated by a pod
- If a pod has to be mandatorily scheduled on a particular node then node-affinity has to be used
Taints
- Taints are set on the nodes
- By default none of the pods have any toleration set
- So if a node is tainted, then with default settings on the pods, none of the pods can be scheduled on the tainted node
- Note that by default the scheduler never schedules a pod on a master node
- This is because there is a taint set on the master node
- This can be modified if required, but as a best practice only the management software is supposed to be run on the master node
kubectl describe node <node> | grep Taint
Tolerations
- Tolerations are set on the pods
How to set taint on a node? Use kubectl as described below
kubectl taint node <node-name> <taint-key-value-pairs>:<taint-effect>
-> Create a taint on a node
kubectl taint node node01 app=blue:NoSchedule
-> Create a taint on node01 with app=blue &taint-effect to NoSchedule
kubectl taint nodes node01 app:NoSchedule-
-> Remove the taint on a node
Taint Effect
- NoSchedule
- This means if the pod cannot tolerate the taint, then it will not be scheduled on the node
- Any existing pods on the node that cannot tolerate the taint on the node are NOT EVICTED
- PreferNoSchedule
- In this case Kubernetes will try not to schedule the pod on the node that has a taint that pod cannot tolerate but it will not guarantee
- NoExecute
- In this case Kubernetes will not schedule any new pod on the node that has a taint that pod cannot tolerate
- If there is any existing pod that cannot tolerate the taint, then it will be evicted out
How to set toleration on a pod? Update pod-definition file as described below
- Add toleration section in the spec section
- Add key, operator, value and effect keys and their corresponding values
- These values are likely to be matched with the taint of the node on which pod has to run
- Note that the values have to put in double-quotes. This is mandatory
pod-definition.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
location: IN
spec:
containers:
- name: nginx-container
image: nginx
tolerations:
- key: "app"
operator: "Equal"
value: "blue"
effect: "NoSchedule"
Example of how taints and tolerations works:
kubectl taint nodes node1 key1=value1:NoSchedule
kubectl taint nodes node1 key1=value1:NoExecute
kubectl taint nodes node1 key2=value2:NoSchedule
----------
tolerations:
- key: "key1"
operator: "Equal"
value: "value1"
effect: "NoSchedule"
- key: "key1"
operator: "Equal"
value: "value1"
effect: "NoExecute"
- In this case, the pod will not be able to schedule onto the node, because there is no toleration matching the third taint.
- But an existing pod will continue to run if it is already running on the node when the taint is added,
- This is because the third taint is the only one of the three that is not tolerated by the pod.
- Normally, if a taint with effect NoExecute is added to a node, then any pods that do not tolerate the taint will be evicted immediately,
- Pods that do tolerate the taint will never be evicted.
- However, a toleration with NoExecute effect can specify an optional tolerationSeconds field that dictates how long the pod will stay bound to the node after the taint is added.
Comments
Post a Comment