Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying pods by multiple labels

Tags:

kubernetes

I need to retrieve a list of pods by selecting their corresponding labels. When the pods have a simple label app=foo, k8s-app=bar, the selection is quite easy:

kubectl get po -l 'app in (foo), k8s-app in (bar)'

The complexity comes with labels that contain special characters, for example: app.kubernetes.io/name=foo So when I query only this label, I don't have a problem, but if I try to add this label to the existing query, it will end by returning no resources were found.

kubectl get po -l app.kubernetes.io/name=foo,app=bar
kubectl get po -l 'app.kubernetes.io/name in (foo), app in (bar)'

Any idea how can I join the two labels in a single query?

like image 254
Tomer Leibovich Avatar asked Dec 09 '25 23:12

Tomer Leibovich


2 Answers

Currently, Kubernetes does not support OR in label selectors. You can only OR different values of the same label (like kubectl get pods -l 'app in (foo,bar)').

See also How can the OR selector be used with labels in Kubernetes?

like image 107
derkoe Avatar answered Dec 12 '25 13:12

derkoe


You can use below command for retrieving a list of pods by selecting their corresponding labels.

kubectl get pods --selector app=foo,k8s-app=bar
like image 30
Nikunj Ranpura Avatar answered Dec 12 '25 14:12

Nikunj Ranpura



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!