display failed pods from kubectl output

6/19/2019

I want to write a wrapper on kubectl to display only failed pods which means it should only display items whose Ready column values are not the same (i.e 0/1, 0/2, 1/2, 2/3, etc.)

$ kubectl get pods --all-namespaces
NAMESPACE       NAME                                        READY     STATUS             RESTARTS   AGE
default         pod-with-sidecar                            1/2       ErrImagePull       0          39s
kube-system     calico-node-chcqq                           2/2       Running            2          7d
kube-system     calico-policy-controller-6449789dd6-z5t5j   1/1       Running            0          7d
kube-system     etcd                                        1/1       Running            0          7d
kube-system     kube-apiserver                              1/1       Running            2          7d
kube-system     kube-controller-manager                     1/1       Running            0          7d
kube-system     kube-dns-5c76d6bddb-8zhmq                   3/3       Running            1          7d
kube-system     kube-proxy-xq8j6                            1/1       Running            0          7d
kube-system     kube-scheduler-                             1/1       Running            0          7d
kube-system     tiller-deploy-5b7cb9cfd7-j725s              1/1       Running            0          7d
my-system       glusterfs-brick-0                           0/2       Pending            0          3m
my-system       sticky-scheduler-6d968f8d74-xvjqn           0/1       ImagePullBackOff   0          4m

so from the above output i want to print these failed pods

NAMESPACE       NAME                                        READY     STATUS             RESTARTS   AGE
default         pod-with-sidecar                            1/2       ErrImagePull       0          4m
my-system       glusterfs-brick-0                           0/2       Pending            0          56s
my-system       sticky-scheduler-6d968f8d74-xvjqn           0/1       ImagePullBackOff   0          8m

This works!

$ kubectl get pods --all-namespaces | grep -vE '1/1|2/2|3/3'

NAMESPACE       NAME                                        READY     STATUS             RESTARTS   AGE
default         pod-with-sidecar                            1/2       ErrImagePull       0          4m
my-system       glusterfs-brick-0                           0/2       Pending            0          56s
my-system       sticky-scheduler-6d968f8d74-xvjqn           0/1       ImagePullBackOff   0          8m

But it won't work if i have 2/4,0/4,0/5,0/6 etc in the Ready column, what can i do with grep -vE '1/1|2/2|3/3' to make it work for all such cases

Reference: https://github.com/kubernetes/kubernetes/issues/49387

-- Bad_Coder
grep
kubernetes

2 Answers

6/21/2019

You can do it by using --field-selector:

kubectl get pods --all-namespaces --field-selector=status.phase!=Running

Source

-- MWZ
Source: StackOverflow

6/19/2019

With GNU grep:

 | grep -Ev '([0-9]+)/\1'

Output:

NAMESPACE NAME READY STATUS RESTARTS AGE default pod-with-sidecar 1/2 ErrImagePull 0 39s my-system glusterfs-brick-0 0/2 Pending 0 3m my-system sticky-scheduler-6d968f8d74-xvjqn 0/1 ImagePullBackOff 0 4m

-- Cyrus
Source: StackOverflow