11

How do I use kubectl to get K8S nodes which do not have any labels? Also , how do I fetch K8S pods which do not have any labels?

Att A
  • 165
  • 1
  • 2
  • 7

7 Answers7

13

For those who want to find resources without a specific label, no matter its value:

kubectl get ns --selector='!label_name'
leoschet
  • 1,697
  • 17
  • 33
6

The first answer works but you can also use the following

kubectl get nodes -l '!label_name'

Karna
  • 579
  • 6
  • 5
5

You have to leverage kubectl -o flag and go-template output:

kubectl get nodes -o go-template='{{range .items }}{{if .metadata.labels }}{{else}}{{printf "%s\n" .metadata.name}}{{ end }}{{end}}

This command will show only nodes which do not have any labels. The same can be used for pods:

kubectl get pods --all-namespaces -o go-template='{{range .items }}{{if .metadata.labels }}{{else}}{{printf "%s\n" .metadata.name}}{{ end }}{{end}}'
Ottovsky
  • 2,068
  • 15
  • 22
  • I could get the pods with labels using jsonpath by entering the following `kubectl get pods --all-namespaces -o jsonpath='{.items[?(@.metadata.labels)].metadata.name}'` But the following did not work `kubectl get pods --all-namespaces -o jsonpath='{.items[?(!@.metadata.labels)].metadata.name}'` – Att A Jun 17 '19 at 19:05
  • Kubectl jsonpath parser does not support negation:https://github.com/kubernetes/client-go/blob/master/util/jsonpath/parser.go#L148 , you have to use go-template as per my example in order to do what you want – Ottovsky Jun 18 '19 at 07:49
3

according to the official documentation I dont think there is a way of doing that, but you can do something like that with the negation of equality:

kubectl get nodes --selector=kubernetes.io/hostname!=node_host_name

basically you can select everything that doesn't have a particular label, you can also chain selectors

More reading: https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
0

There is no specific way to check for no labels in general without listing every possible label. You would have to do this client side.

coderanger
  • 52,400
  • 4
  • 52
  • 75
0

here the client side jq query for it.

kubectl get nodes -o json| jq '.items[].metadata|select( has("labels") == false )|.name'

Here as bonus solution to find object (secret in this example) without any annotation nor labels

secretNoAnno=$(kubectl  get secret -o json| jq '.items[].metadata|select( has("annotations") == false )|.name') 
secretNoAnnoClean=$(echo "$secretNoAnno" | tr -d '"')
kubectl  get secret $secretNoAnnoClean -o json| jq '.items[].metadata|select( has("labels") == false )|.name'

Tilo
  • 1,110
  • 3
  • 21
  • 42
-1

there is no way to check the nodes/pods that dont have labels. Instead what you can do is check for nodes/pods for specific label

follow the below steps

add label mylabel=k8s

master $ kubectl get no
NAME      STATUS    ROLES     AGE       VERSION
master    Ready     master    51m       v1.11.3
node01    Ready     <none>    50m       v1.11.3
master $
master $
master $ kubectl label nodes node01 mylabel=k8s
node/node01 labeled
master $
master $ kubectl get no -L mylabel
NAME      STATUS    ROLES     AGE       VERSION   MYLABEL
master    Ready     master    52m       v1.11.3
node01    Ready     <none>    52m       v1.11.3   k8s

list nodes that has label mylabel=k8s

master $ kubectl get no -l mylabel=k8s
NAME      STATUS    ROLES     AGE       VERSION
node01    Ready     <none>    53m       v1.11.3
master $

list the nodes that doesnt have label mylabel=k8s

master $ kubectl get no -l mylabel!=k8s
NAME      STATUS    ROLES     AGE       VERSION
master    Ready     master    53m       v1.11.3
P Ekambaram
  • 15,499
  • 7
  • 34
  • 59