Get Pods of Unique Namespaces

3/4/2020

I'm running the following command from Terminal to retrieve the status of a specific pod amongst all of my namespaces. The problem I'm trying to solve is to only return the unique namespaces that have a status of 'Evicted'

kubectl get pods --all-namespaces -lapp=myapp | grep Evicted | sort | uniq -c

This is an example of the results I get:

NAMESPACE       READY       STATUS
customer-1       0/1        Evicted
customer-3       0/1        Evicted
customer-2       0/1        Evicted
customer-3       0/1        Evicted
customer-1       0/1        Evicted

This is the result I'm after:

NAMESPACE       READY       STATUS
customer-1       0/1        Evicted
customer-2       0/1        Evicted
customer-3       0/1        Evicted

How would I go about achieving this?

-- dfuentes
kubectl
kubernetes

2 Answers

3/5/2020

I would like to suggest you a different approach using kubectl command arguments:

$ kubectl get pods --all-namespaces --field-selector=status.phase=Evicted --sort-by=.metadata.namespace -o custom-columns=NAMESPACE:.metadata.namespace | uniq

Here we are using some arguments to filter, sort and define custom columns output.

The output will be similar to this:

NAMESPACE
customer-1
customer-2
customer-3
-- mWatney
Source: StackOverflow

3/4/2020
kubectl get pods --all-namespaces -lapp=myapp | grep Evicted | awk {'print $1'} | uniq -c

should do the trick for you. Uniq wasn't having an effect because of non-unique pod names.

-- Tummala Dhanvi
Source: StackOverflow