Search code examples
kuberneteskubectl

I want to output the result of "kubectl get pod" excluding the key using LabelSelector of kubectl


I am learning Kubernetes Label Selector at the URL below.

https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/

Set-based requirement

Set-based label requirements allow filtering keys according to a set of values. Three kinds of operators are supported: in, notin and exists (only the key identifier). For example:

! partition

The fourth(This) example selects all resources without a label with key partition; no values ​​are checked.

My assumption is that if I specify "!test", all pods will be output, I think that nothing is output when "!app" is specified.

I want to exclude the key and output as in the example described here, but the expected result was not obtained.

My @ Pc: ~ / Understanding-K8s / chap04 $ kubectl get pod -l! app
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l !app
-bash: !app: event not found
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l "!app"
-bash: !app: event not found
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l "!test"
-bash: !test: event not found
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l !test
-bash: !test: event not found

The result of executing the --show-labels command is as follows.

My @ Pc: ~ / Understanding-K8s / chap04 $ kubectl get pod --show-labels
NAME          READY   STATUS    RESTARTS   AGE   LABELS
nginx-pod-a   1/1     Running   0          89m   app=photo-view,env=stage
nginx-pod-b   1/1     Running   0          89m   app=imagetrain,env=test
nginx-pod-c   1/1     Running   0          89m   app=prediction,env=test
nginx-pod-d   1/1     Running   0          89m   app=photo-view,env=stage
nginx-pod-e   1/1     Running   0          89m   app=imagetrain,env=stage
nginx-pod-f   1/1     Running   0          89m   app=photo-view,env=prod

How can I exclude the intended keys such as app and env?


tyzbit has given me the answer, and I have tried. I couldn't describe the result well in the comment, so I will add it here.

My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l '!app'
No resources found in default namespace.
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l '!test'
NAME          READY   STATUS    RESTARTS   AGE
nginx-pod-a   1/1     Running   0          162m
nginx-pod-b   1/1     Running   0          162m
nginx-pod-c   1/1     Running   0          162m
nginx-pod-d   1/1     Running   0          162m
nginx-pod-e   1/1     Running   0          162m
nginx-pod-f   1/1     Running   0          162m

Girdhar Singh Rathore teaches me AWK and jsonpath.

(Maybe, I've written the wrong format...)

AWK
My@Pc:~$ kubectl get pods --show-labels | awk  '$6 !~/app/ {print ;}'
NAME          READY   STATUS    RESTARTS   AGE   LABELS

My@Pc:~$ kubectl get pods --show-labels | awk  '$6 !~/test/ {print ;}'
NAME          READY   STATUS    RESTARTS   AGE   LABELS
nginx-pod-a   1/1     Running   0          18m   app=photo-view,env=stage
nginx-pod-d   1/1     Running   0          18m   app=photo-view,env=stage
nginx-pod-e   1/1     Running   0          18m   app=imagetrain,env=stage
nginx-pod-f   1/1     Running   0          18m   app=photo-view,env=prod
jsonpath
My@Pc:~$ kubectl get pods -o jsonpath='{range .items[*]} {.metadata.name} {.metadata.labels} {"\n"} {end}' | awk '$2 !~/app/ {print $1}'

My@Pc:~$ kubectl get pods -o jsonpath='{range .items[*]} {.metadata.name} {.metadata.labels} {"\n"} {end}' | awk '$2 !~/test/ {print $1}'
nginx-pod-a
nginx-pod-d
nginx-pod-e
nginx-pod-f

Solution

  • The ! is getting interpreted by your shell, use a single quote to prevent this. The correct syntax is:

    kubectl get pods -l '!app'