The kubectl taint
command in Kubernetes is used to update node taints, which are key-value pairs associated with nodes. Taints are used to repel certain pods from nodes unless the pods have corresponding tolerations. This helps in influencing pod placement decisions and ensuring that only suitable workloads are scheduled on specific nodes.
Let’s explore several examples of how to use kubectl taint
:
Example 1: Add a taint to a node
To add a taint to a node named node1
with key app
and value blue
, use the following command:
kubectl taint nodes node1 app=blue:NoSchedule
Explanation: This command adds a taint with key app
and value blue
to node1
. Pods without a matching toleration won’t be scheduled on this node. The NoSchedule
effect indicates that pods will not be scheduled unless they tolerate this taint.
Example 2: Remove a taint from a node
To remove the taint with key app
from node1
, use:
kubectl taint nodes node1 app-
Explanation: This command removes the taint with key app
from node1
, allowing pods that previously couldn’t be scheduled due to this taint to now be considered for scheduling.
Example 3: View taints on a node
To view all taints on node1
, use:
kubectl describe node node1 | grep Taints
Explanation: This command retrieves and displays all taints applied to node1
. It uses kubectl describe
to fetch detailed information about the node and then filters for the taints section.
Example 4: Add a taint to a node with effect and toleration
To add a taint with key special
, value critical
, effect NoExecute
, and a pod toleration, use:
kubectl taint nodes node1 special=critical:NoExecute --overwrite
Explanation: This command adds a taint with the effect NoExecute
, which evicts existing pods that do not tolerate it. The --overwrite
flag ensures any existing taint with the same key is replaced.
Example 5: Add a taint to multiple nodes
To add the same taint to multiple nodes at once, create a YAML file like:
apiVersion: v1 kind: Node metadata: name: node1 spec: taints: - key: special value: critical effect: NoSchedule --- apiVersion: v1 kind: Node metadata: name: node2 spec: taints: - key: special value: critical effect: NoSchedule
Apply it using:
kubectl apply -f filename.yaml
Explanation: This YAML file defines nodes node1
and node2
with the same taint. Applying it with kubectl apply
updates the nodes accordingly.
To verify if a taint has been successfully added or removed from a node, you can re-describe the node and check the taints section. For example, after adding a taint, run:
kubectl describe node node1 | grep Taints
This will display the current taints on node1
and confirm if the operation was successful.
Also check similar articles.
Prepare Nodes for Maintenance with kubectl drain
Scheduling Nodes in Kubernetes with kubectl uncordon
How to Cordon Kubernetes Nodes with kubectl cordon
Monitor Resource Usage with kubectl top
Access Cluster Information Using kubectl cluster-info
Discussion about this post