When you work on a real-time environment there will be times when you want to run a specific type of workloads (or application) a designated worker node.
For example, production workloads should be running on specific worker nodes and shouldn't get mixed with staging or development workloads.
This is where adding labels to the node will be helpful.
Let me show you how to add labels to nodes in Kubernetes, change the labels and remove those labels later.
How to read node labels in Kubernetes
You can list Kubernetes node details along with their labels in this fashion:
kubectl get nodes --show-labels
If you want to know the details for a specific node, use this:
kubectl label --list nodes node_name
The labels are in form of key-value pair. They must begin with a letter or number, and may contain letters, numbers, hyphens, dots, and underscores, up to 63 characters each.
How to assign label to a node
Now suppose you want
kworker-rj1 node to host all the production related workloads.
Let's label that node with an appropriate name (like production):
root@kmaster-rj:~# kubectl label nodes kworker-rj1 workload=production node/kworker-rj1 labeled
Confirm the pod labelling:
root@kmaster-rj:~# kubectl label --list nodes kworker-rj1 | grep -i workload workload=production
I used the grep command to weed out unnecessary details and focus on the label.
How to overwrite the node label
If you later decide to overwrite some labels based on the requirements see how you can achieve that.
root@kmaster-rj:~# kubectl label --overwrite nodes kworker-rj1 workload=staging node/kworker-rj1 labeled
You may confirm the pod re-labelling:
root@kmaster-rj:~# kubectl label --list nodes kworker-rj1 | grep -i workload workload=staging
How to remove the node labels
To remove the label from a node, provide the key without any value.
root@kmaster-rj:~# kubectl label --overwrite nodes kworker-rj1 workload- node/kworker-rj1 labeled
You can confirm the node label removal:
root@kmaster-rj:~# kubectl label --list nodes kworker-rj1 | grep -i workload root@kmaster-rj:~#
That's all! I hope you got familiar with the kubectl label command with this tutorial.
DevOps Professional | RHCA | Jenkins | Git | Docker | Kubernetes | Ansible | Prometheus | Grafana | AWS Cloud