Is there any way to get namespaces based on a particular metadata label

9/6/2018

My namespace has some custom metadata labels. Some have the labels some don't. Is there any way to get the namespaces which has a particular label using kubectl?

-- codec
kubectl
kubernetes

1 Answer

9/6/2018

Yes. Like so:

$ kubectl create ns nswithlabels

$ kubectl label namespace nswithlabels this=thing

$ kubectl describe ns/nswithlabels
Name:         nswithlabels
Labels:       this=thing
Annotations:  <none>
Status:       Active

No resource quota.

No resource limits.

$ kubectl get ns -l=this
NAME           STATUS    AGE
nswithlabels   Active    6m

Note: I could have also used -l=this=thing in the last command to specify both key and value required to match.

-- Michael Hausenblas
Source: StackOverflow