In Kubernetes, Taints and Tolerations are used to control which Pods can be scheduled on which Nodes.
Taints
A Taint is a label applied to a Node that repels Pods. When a Node is tainted, it will not accept Pods that do not tolerate the taint. A Taint is specified by a key-value pair and a taint effect. The taint effect can be either NoSchedule, PreferNoSchedule, or NoExecute. The NoSchedule effect prevents new Pods from being scheduled on the tainted Node, the PreferNoSchedule effect tries to avoid scheduling new Pods on the tainted Node, and the NoExecute effect evicts existing Pods that do not tolerate the taint.
An example of tainting a Node is:
kubectl taint nodes node-1 app=nginx:NoSchedule
This command applies a taint to the Node named "node-1" with the key-value pair "app=nginx" and the effect "NoSchedule". This means that any Pod without a corresponding toleration for this taint will not be scheduled on this Node.
Tolerations
A Toleration is a property applied to a Pod that allows it to tolerate a taint. When a Pod has a toleration for a specific taint, it can be scheduled on the tainted Node. A Toleration is specified by a key-value pair and an effect that matches the corresponding taint.
An example of adding a toleration to a Pod is:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx:latest
tolerations:
- key: app
operator: Equal
value: nginx
effect: NoSchedule
This YAML file specifies a Pod named "nginx-pod" with an Nginx container. The toleration allows the Pod to be scheduled on a Node with a taint that has the key-value pair "app=nginx" and the effect "NoSchedule".
Taints and Tolerations are useful in Kubernetes cluster setups where specific Nodes have special hardware or are dedicated to certain workloads. They ensure that Pods are scheduled on Nodes that meet specific requirements, helping to optimize resource utilization and ensure high availability of applications.