How to add or remove label from node in Kubernetes

Kubernetes Tutorial, DevOPs

In this article I will share the steps to add or remove labels to worker nodes in Kubernetes cluster. This article assumes that you are already familiar with the concept of labels and selectors used in Kubernetes.


Lab Environment

I will be using my multi-node cluster which I had created during the starting of this entire tutorial to demonstrate this article. This cluster contains two worker nodes and one control plane node. So we will add and remove labels on these worker nodes and deploy pods using nodeSelector.

How to add or remove label from node in Kubernetes


List assigned labels on the node

We use kubectl get nodes to list the available nodes in the cluster. Additionally we can use --show-labels arguments to also list the assigned labels to individual nodes in the cluster.

[root@controller ~]# kubectl get nodes --show-labels
NAME                     STATUS   ROLES    AGE   VERSION   LABELS   Ready    master   25d   v1.19.9,,,,,     Ready    <none>   25d   v1.19.9,,,,     Ready    <none>   25d   v1.19.9,,,,

Here you can see, now we have one additional field "LABELS" which shows the labels which are applied to individual nodes.


Add label to a node

To add a label, we can use kubectl label nodes command in the following syntax:

kubectl label nodes <your-node-name> <label>

For example if we want to apply label as "color: blue" to worker-2.example node then this can applied using:

[root@controller ~]# kubectl label nodes color=blue
node/ labeled

You can verify the respective worker node and the applied label:

How to add or remove label from node in Kubernetes


Deploy a pod on specific node

Now we will utilise the label which we specified in the previous example to deploy a pod. By default when you create a Pod, it can be created on any of the available worker nodes. But using node labels and selectors, we can control this behaviour.

So in this example, we will create a deployment where the replica pods must be deployed only on the nodes with label "color: blue". Following is our YAML file to create the deployment:

[root@controller ~]# cat nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
    ## This label is applied to the Deployment
    type: dev
  name: nginx-deploy
  replicas: 1
        ## This label is used to match the Pod to create replicas
        type: dev
        ## This label is applied to the Pod
        type: dev
      - image: nginx
        name: nginx
        - containerPort: 80
        ## This label is used to deploy the pod on matching nodes
        color: blue

Let's create this deployment:

[root@controller ~]# kubectl create -f nginx-deploy.yaml
deployment.apps/nginx-deploy created

As you can see the pod from our deployment is deployed on worker-2 node because that is the only node with label color: blue:

[root@controller ~]# kubectl get pods -o wide
NAME                            READY   STATUS              RESTARTS   AGE     IP             NODE                   NOMINATED NODE   READINESS GATES
nginx-deploy-6d8d787fb7-xxbn4   0/1     ContainerCreating   0          24s     <none>   <none>           <none>
sidecar-pod-1                   2/2     Running             2          7d   <none>           <none>
sidecar-pod-2                   2/2     Running             2          7d   <none>           <none>

So every time the pod is restarted or terminated, it will always start on worker-2 node.


Remove label from a node

Use the following syntax to remove a label from a node:

kubectl label node <nodename> <labelname>-

As you can notice, we use the same command but with a minus sign with the label name. For example to remove the label color: blue from worker-2 node, we will use:

[root@controller ~]# kubectl label node color-
node/ not labeled

You can verify the same, here the command output should be blank:

~]# kubectl get nodes --show-labels | grep color


But this wouldn't mean that your existing pod would be terminated which we created in our example. It will continue to function, although if the pod is terminated then another pod would not be created unless it finds a node with label color: blue.

Let us verify this theory by deleting the deployment pod replica:

[root@controller ~]# kubectl delete pod nginx-deploy-6d8d787fb7-xxbn4
pod "nginx-deploy-6d8d787fb7-xxbn4" deleted

Now check if a new pod was created:
How to add or remove label from node in Kubernetes

So a pod was created as our deployment expects a single replica to be always there but since the pod fails to find a node with label color: blue, the pod container is not yet created.


Let us assign this label to worker-1 node this time:

[root@controller ~]# kubectl label node color=blue
node/ labeled

Now you can see the container is getting created on because we applied the label color: blue

[root@controller ~]# kubectl get pods -o wide
NAME                            READY   STATUS              RESTARTS   AGE     IP             NODE                   NOMINATED NODE   READINESS GATES
nginx-deploy-6d8d787fb7-pgkws   0/1     ContainerCreating   0          2m39s   <none>   <none>           <none>
sidecar-pod-1                   2/2     Running             2          7d   <none>           <none>
sidecar-pod-2                   2/2     Running             2          7d   <none>           <none>


What's Next

You should learn about DaemonSets which is used to deploy node specific pods. In this case if you remove a label from the node then the pod deployed on this specific node will be terminated.



In this article we learned about node labels, add or remove labels from the nodes in a Kubernetes Cluster. Labels and label selectors should be used to organize pods and easily perform operations on multiple pods at once. You can use node labels and selectors to schedule pods only to nodes that have certain features.


Further Reading

Assign Pods to Nodes

Views: 1,140
Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to

Thank You for your support!!

Leave a Comment