78

When I provision a Kubernetes cluster using kubeadm, I get my nodes tagged as "none". It's a known bug in Kubernetes and currently a PR is in progress.

However, I would like to know if there is an option to add a Role name manually for the node.

root@ip-172-31-14-133:~# kubectl get nodes
NAME               STATUS    ROLES     AGE       VERSION
ip-172-31-14-133   Ready     master    19m       v1.9.3
ip-172-31-6-147    Ready     <none>    16m       v1.9.3
DimiDak
  • 4,820
  • 2
  • 26
  • 32
Jeel
  • 2,227
  • 5
  • 23
  • 38

7 Answers7

90

This worked for me:

kubectl label node cb2.4xyz.couchbase.com node-role.kubernetes.io/worker=worker

NAME                                          STATUS    ROLES           AGE       VERSION
cb2.4xyz.couchbase.com                          Ready     custom,worker   35m       v1.11.1
cb3.5xyz.couchbase.com                          Ready     worker          29m       v1.11.1

I could not delete/update the old label, but I can live with it.

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
ram dhakne
  • 926
  • 7
  • 3
  • 2
    You can delete "custom" role with command kubectl label node cb2.4xyz.couchbase.com node-role.kubernetes.io/custom- – Panoptik Mar 24 '20 at 08:41
  • same answer <> better answer :) – rnrneverdies Jun 26 '20 at 04:28
  • Is there a an actual reason why you need to add `worker=worker` and not simply `node-role.kubernetes.io=worker`? Is there a convention people should abide by? – Lethargos Mar 11 '22 at 12:19
  • Oh, I see. My version doesn't change the ROLES column actually :) But this works though and it seems simpler and more elegant: `kubectl label node cb2.4xyz.couchbase.com node-role.kubernetes.io/worker=""` – Lethargos Mar 11 '22 at 12:32
  • 1
    This command removed the `master` role from my three k8s nodes: `kubectl label node k8s1 k8s2 k8s3 node-role.kubernetes.io/master-` Hope this helps. (k8s=1.21.13) – AG6HQ Jun 14 '22 at 04:39
62

A node role is just a label with the format node-role.kubernetes.io/<role>

You can add this yourself with kubectl label

Jordan Liggitt
  • 16,933
  • 2
  • 56
  • 44
  • 10
    Exact command would look like this: kubectl label node aNodeName node-role.kubernetes.io/custom= – Hammelkeule May 15 '18 at 12:13
  • 9
    just to add that `kubernetes.io/role=` works as well – Erez Rabih Oct 29 '18 at 13:29
  • 3
    You shouldn't use `kubernetes.io/role` anymore. https://github.com/kubernetes/enhancements/blob/master/keps/sig-auth/0000-20170814-bounding-self-labeling-kubelets.md#proposal – CashIsClay Jun 02 '20 at 00:49
  • 1
    Yeah this no longer is supported to set via kubelet args from Kubernetes version 1.16. Does anyone know a way to set it at the time of launching the workers? Thanks in advance. – Ankita13 Oct 27 '20 at 12:07
  • 2
    I'm also looking for the best way to do this post k8s version 1.16. Any progress here? – Brannon Nov 06 '20 at 20:36
32

Default
enter image description here

Add Role

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

enter image description here

Remove Role

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

enter image description here

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

Before label:

general@master-node:~$ kubectl get nodes
NAME          STATUS   ROLES    AGE   VERSION
master-node   Ready    master   23m   v1.18.2
slave-node    Ready    <none>   19m   v1.18.2

kubectl label nodes <your_node> kubernetes.io/role=<your_label>

In my case slave-node e.g.

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

After label:

general@master-node:~$ kubectl label nodes slave-node kubernetes.io/role=worker
node/slave-node labeled
general@master-node:~$ kubectl get nodes
NAME          STATUS   ROLES    AGE   VERSION
master-node   Ready    master   24m   v1.18.2
slave-node    Ready    worker   21m   v1.18.2

You can also change the label. Just put --overwrite

kubectl label --overwrite nodes <your_node> kubernetes.io/role=<your_new_label>

e.g.

kubectl label --overwrite nodes slave-node kubernetes.io/role=worker1

After overwriting the label:

general@master-node:~$ kubectl label --overwrite nodes slave-node kubernetes.io/role=worker1
node/slave-node labeled
general@master-node:~$ kubectl get nodes
NAME          STATUS   ROLES     AGE   VERSION
master-node   Ready    master    36m   v1.18.2
slave-node    Ready    worker1   32m   v1.18.2
Community
  • 1
  • 1
Kamlesh
  • 629
  • 7
  • 9
4

Add Roles

$ kubectl get nodes 

master  Ready    control-plane,master   166m    v1.21.1
worker1  Ready    worker                 48m     v1.21.1
worker2  Ready    worker                 16m     v1.21.1
worker3   Ready    worker                 9m57s   v1.21.1    

$ kubectl label node worker1  node-role.kubernetes.io/worker=worker

$ kubectl label node worker2  node-role.kubernetes.io/worker=worker

$ kubectl label node worker3  node-role.kubernetes.io/worker=worker

In case if you wanted to overwrite, please use this below command.

$ kubectl label node worker1  node-role.kubernetes.io/worker=worker --overwrite   
Praveen Kumar K S
  • 3,024
  • 1
  • 24
  • 31
  • 2
    You can choose your node to have any role like `node-role.kubernetes.io/testing=testing-worker` and `node-role.kubernetes.io/system=system-worker`... The column ROLES will display the value(s) separated by comma in case multiple values exist – Marcello DeSales Nov 26 '21 at 23:31
0

In my case i was able to do that with below commands

kubectl label nodes ip-10-0-47-13.ec2.internal kubernetes.io/role=worker

Output

node/ip-10-0-47-13.ec2.internal labeled

mansoor.hasan@PURE-DEV-MANSOORHASAN EKS % kubectl get nodes                                                       
NAME                         STATUS   ROLES    AGE    VERSION
ip-10-0-47-13.ec2.internal   Ready    worker   161m   v1.20.15-eks-fb459a0
0

On top of everything that has already been answered since k8S 1.16, the kubernetes.io/ and k8s.io/ prefixes are reserved for Kubernetes core components.

The suggested way is to create your own namespace to label a kubernetes node by simply reproducing the kubelet way.

For example as a label key node-role.[your-domain]/[role]=. No label value is needed, the label key is actually a tag.

kubectl label node [node-name] node-role.[your-namespace]/[role]=

Where:

  • node-name is your node name (returned by kubectl get node)
  • your-namespace is the "domain" of your namespace (eg. acme.org)
  • role is the node role, (eg. database)

For example:

kubectl label node node001 node-role.acme.org/database=
Nicolas Labrot
  • 4,017
  • 25
  • 40