109

Add label to nodes:

$ kubectl label nodes 10.xx.xx.xx key1=val1 

If I want to delete label(key1=val1) on node(10.xx.xx.xx), how can I delete by kubectl command and API?

Steffi Keran Rani J
  • 3,667
  • 4
  • 34
  • 56
ttyyll
  • 1,171
  • 2
  • 7
  • 6

14 Answers14

190

create labels for the nodes:

kubectl label node <nodename> <labelname>=allow

delete above labels from its respecitve nodes:

kubectl label node <nodename> <labelname>-

Gaurav Gharat
  • 1,921
  • 1
  • 10
  • 5
29

Below command worked for me to remove label:

kubectl label node <nodename> <label>-

Note: The syntax is a minus sign directly after the key. For example, if the node name is worker1 and the label is system=workernode, you can remove a label with the following command.

kubectl label node worker1 system-
sudheerchamarthi
  • 1,081
  • 8
  • 13
21

From kubectl label -h:

Update pod 'foo' by removing a label named 'bar' if it exists.
Does not require the --overwrite flag.
$ kubectl label pods foo bar-

The same works for nodes.

Prashanth B
  • 4,833
  • 1
  • 19
  • 13
18

To add a label to kubernetes nodes:

kubectl label node "your-node-name" node-role.kubernetes.io/worker=worker

To remove a label from kubernetes nodes:

kubectl label node "your node-name" node-role.kubernetes.io/worker-

Note: To remove a label put a "-" symbol at the end of your label name

89f3a1c
  • 1,430
  • 1
  • 14
  • 24
12

This worked for me. Add Label

kubectl label node <node name> node-role.kubernetes.io/<role name>=<key - (any name)>

Remove label

kubectl label node <node name> node-role.kubernetes.io/<role name>-

For More Info

Isuru Amarathunga
  • 2,127
  • 1
  • 20
  • 20
9

To remove the label you can use

kubectl label nodes 10.xx.xx.xx key1-

Danushka
  • 161
  • 1
  • 4
3

You can remove label this way

kubectl label nodes <node_name> key1- key2-

eg: kubectl label nodes ip-172-20-22-247 key1- key2-
Phanindra
  • 329
  • 2
  • 11
3

As already mentioned, correct kubectl example to delete label, but there is no mention of removing labels using API clients. if you want to remove label using the API, then you need to provide a new body with the labelname: None and then patch that body to the node or pod. I am using the kubernetes python client API for example purpose

from pprint import pprint
from kubernetes import client, config

config.load_kube_config()
client.configuration.debug = True

api_instance = client.CoreV1Api()

body = {
    "metadata": {
        "labels": {
            "label-name": None}
        }
}

api_response = api_instance.patch_node("minikube", body)

print(api_response)
Prafull Ladha
  • 12,341
  • 2
  • 37
  • 58
2

You can remove the label from a single node using the following kubectl command

kubectl label node 10.xx.xx.xx Key1-

If you want to remove the label for all the nodes, use the following command

kubectl label nodes --all Key1-

Raviteja
  • 1,892
  • 3
  • 15
  • 12
1

just try that and it should be

kubectl label nodes work1 node-role.kubernetes.io/worker=

to verify and compare with other nodes

kubectl get nodes
kubectl get nodes --show-labels
David
  • 302
  • 1
  • 4
  • When I did this it responded with a comforting and affirming `namespace/X unlabeled`. – Josiah Apr 27 '23 at 04:08
  • it seems the sessions in this site works in some weird way, my response belongs to another thread I had open too, for marking node as worker – David May 05 '23 at 16:43
0
  1. If you want to see existing labels for the nodes kubectl get nodes --show-labels
  2. Then, list the key_name and node_name you want to modify
  3. Then, kubectl label node node_name key_name-
rajdeepbs29
  • 1,211
  • 12
  • 9
0

To add label to your node

kubectl label node ip-172-31-15-136 mynode=tst mnode=ssd

To remove label from your node

kubectl label node ip-172-31-15-136 mynode- mynode-
4b0
  • 21,981
  • 30
  • 95
  • 142
Manoj
  • 21
  • 1
0

To remove or re-name use parameter --overwrite e.g

kubectl label nodes node-name --overwrite label-name
General Grievance
  • 4,555
  • 31
  • 31
  • 45
0

To remove a label from all nodes:

kubectl label node --all label-

Notice the - concatenated to the label without any space.

Mohammed Noureldin
  • 14,913
  • 17
  • 70
  • 99