Zhi Yuan
Zhi Yuan

Reputation: 890

how to use kubectl command with flag --selector?

I have a question about kubectl command with flag --selector. In help menu it says,

-l, --selector="": Selector (label query) to filter on

how ever it does't work as i expect, for example, i want to get RC who have selector like

    "spec": {
    "replicas": 2,
    "selector": {
        "app": "tas-core"
    },

when i give command

kubectl get pod --selector="app:tas-core"

system report: the provided selector "app:tas-core" is not valid: unable to parse requirement: label key: invalid value 'app:tas-core', Details: must match regex [a-z0-9?(.a-z0-9?)* / ] a-z0-9?

after i check the regexp

[[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)* / ] [a-z0-9]([-a-z0-9]*[a-z0-9])?

i still can't find any string which can pass the regexp! i gave,

kubectl get rc -l app/tas-core

nothing has been returned. How could i use it?

I have also another question, how to filter all pods which with label like

"labels": {
  "app": "tas-core"
}

?

Upvotes: 25

Views: 75411

Answers (3)

Jane
Jane

Reputation: 717

Try

kubectl get pods --selector=app=tas-core

as in https://kubernetes.io/docs/reference/kubectl/quick-reference

Upvotes: 36

Ramesh
Ramesh

Reputation: 1763

We should be able to query the pod name using label selectors and jsonpath

kubectl get pods -l "app.kubernetes.io/name=nginx" -o jsonpath={.items[0].metadata.name}

Upvotes: 2

Omar Khaled
Omar Khaled

Reputation: 441

  • To use one selector with kubectl command,apply the follwing command: kubectl get po --selector name=value where name is the selector name and value is the selector value.
  • You can use rc, svc or deployments (any k8s resource) needed to be listed and also filtered with a selector.
  • There is also an option to list or filter k8s resources with multiple selectors, just use the following command: kubectl get all --selector name1=value1,name2=value2,name3=value3

Upvotes: 15

Related Questions