K8s Taint and Toleration
Taints and Tolerations are mechanisms used to control pod scheduling and allow or prevent certain pods from running on specific nodes. They are typically used in scenarios where you have nodes with specific requirements or constraints.
Taints: A taint is a label applied to a node that repels pods. It indicates that the node has some specific requirements or constraints, making it unsuitable for running certain types of pods. Taints prevent pods from being scheduled onto nodes unless the pods have corresponding tolerations.
Tolerations: A toleration is a configuration setting that allows a pod to tolerate (or accept) nodes with specific taints. When a pod has a toleration for a particular taint, it can be scheduled on nodes with that taint. Tolerations are specified in the pod's specification, and each toleration contains the key and value that match the taint on the node
Here's an example to illustrate the usage of taints and tolerations:
Taint a node:
kubectl taint nodes <node-name> <key>=<value>:<effect>
The
<key>=<value>
pair represents the label applied to the node, and<effect>
specifies the taint effect, which can be one of three values:NoSchedule
,PreferNoSchedule
, orNoExecute
.
Taints Effects:
the effect
field is used to specify the behavior of a taint or toleration. It determines how the presence of a taint or toleration affects pod scheduling on a node. There are three possible values for the effect
field:
NoSchedule: If a node has a taint with the effect set to
NoSchedule
, pods without a matching toleration will not be scheduled on that node by the Kubernetes scheduler. Existing pods on the node are not affected.PreferNoSchedule: When a node has a taint with the effect set to
PreferNoSchedule
, the Kubernetes scheduler will try to avoid scheduling pods without a matching toleration on that node. However, it is not an absolute restriction likeNoSchedule
. If no other suitable nodes are available, pods without tolerations can still be scheduled on the node.NoExecute: If a node has a taint with the effect set to
NoExecute
, any pod that does not have a matching toleration will be evicted from the node. This can happen when a taint is added to a running node or when a toleration is removed from a pod. Existing pods on the node that were already scheduled with the toleration are not affected.
Create a pod with a toleration:
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: mycontainer
image: nginx
tolerations:
- key: <key>
operator: <operator>
value: <value>
effect: <effect>
The <key>
, <value>
, and <effect>
must match the taint on the node. The <operator>
can be one of three values: Exists
, Equal
, or DoubleEqual
. The Exists
operator ignores the <value>
field.
In Kubernetes, the operator
field is used in the toleration specification to define how the value of the toleration should be compared with the value of the taint on a node. The operator
field can take one of the following three values:
Equal: This is the default operator. When the operator is set to
Equal
, the value of the toleration must be an exact match to the value of the taint on the node for the toleration to be considered a match. For example, if the taint has a value of "high-priority" and the toleration has a value of "high-priority", they would be considered a match.Exists: When the operator is set to
Exists
, the value of the toleration is ignored. This operator is used to indicate that any value of the taint is acceptable. It only checks if the key of the toleration matches the key of the taint. This allows pods to tolerate nodes with any value of the taint. For example, if the taint has a key of "gpu" with any value, a toleration with the key "gpu" and operator "Exists" would match.DoubleEqual: The
DoubleEqual
operator is used when you want to compare the value of the toleration with the value of the taint using a case-insensitive comparison. It checks for an exact match regardless of the case of the values. For example, if the taint has a value of "High-Priority" and the toleration has a value of "high-priority", they would be considered a match with theDoubleEqual
operator.
Subscribe to my newsletter
Read articles from Naveen Elwaka directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Naveen Elwaka
Naveen Elwaka
DevOps ๐จโ๐ป | Tech Blogger ๐จโ๐ป | Cloud Computing | Cloud Native | Open Source