What's the alternative for kubectl + grep commands?

11/27/2020

A lot of times the commands I run look like

kubectl get * | grep abc

but this way I can't see the first row (which is the column names), is there an easy alternative to this such that I'll see 2 rows (for resource abc and the column names)?

-- Serge Vu
bash
kubernetes

3 Answers

11/30/2020

I think you still need grep: kubectl get ... | grep -e abc -e NAME.

2 options:

  • you can list and grep all objects at once, but in that case you'll have unnecessary lines in output. Sure, if you want - you can get rid of them using some 'if' logic. In my example this is NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE line
kubectl get all | grep -e nginx -e NAME
NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-deployment-756d9fd5f9-6b5vv   1/1     Running   0          23d
pod/nginx-deployment-756d9fd5f9-r9z84   1/1     Running   0          23d
pod/nginx-deployment-756d9fd5f9-rr7bp   1/1     Running   0          23d
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-deployment   3/3     3            3           146d
NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deployment-756d9fd5f9   3         3         3       146d
  • you can also list resources in kubectl get output. That way you won't have unnecessary lines in output
kubectl get pod,ds,deployment,rs -A | grep -e flu -e NAME
NAMESPACE     NAME                                                            READY   STATUS    RESTARTS   AGE
kube-system   pod/fluentd-gcp-scaler-bfd6cf8dd-cs27w                          1/1     Running   0          23d
kube-system   pod/fluentd-gcp-v3.1.1-fl9wh                                    2/2     Running   0          23d
kube-system   pod/fluentd-gcp-v3.1.1-jctqn                                    2/2     Running   0          23d
kube-system   pod/fluentd-gcp-v3.1.1-q2hcx                                    2/2     Running   0          23d
NAMESPACE     NAME                                            DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR                                                              AGE
kube-system   daemonset.extensions/fluentd-gcp-v3.1.1         3         3         3       3            3           beta.kubernetes.io/fluentd-ds-ready=true,beta.kubernetes.io/os=linux       199d
NAMESPACE     NAME                                                             READY   UP-TO-DATE   AVAILABLE   AGE
kube-system   deployment.extensions/fluentd-gcp-scaler                         1/1     1            1           199d
NAMESPACE     NAME                                                                        DESIRED   CURRENT   READY   AGE
kube-system   replicaset.extensions/fluentd-gcp-scaler-bfd6cf8dd                          1         1         1       199d
-- Vit
Source: StackOverflow

11/27/2020

Kubenertes already supported JSONPath so we can get any value 's field of a kubenertes object.

This is example when i want to get namespace of a pod:

$ kubectl get pods -l app=app-demo --all-namespaces -o=jsonpath='{.items[0].metadata.namespace}'
demo%

You can get reference here: JSONPath Support.

-- Tho Quach
Source: StackOverflow

11/27/2020

Awk would be a better candidate than grep and so:

kubectl get pods | awk 'NR==1 || $1 ~ /abc/ { print }'

Pipe the output of kubectl to awk, printing the first line (headers) along with any lines where the first space delimited field contains abc

-- Raman Sailopal
Source: StackOverflow