dfuentes
dfuentes

Reputation: 23

Get Pods of Unique Namespaces

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?

Upvotes: 1

Views: 732

Answers (2)

Mark Watney
Mark Watney

Reputation: 5930

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

Upvotes: 0

Tummala Dhanvi
Tummala Dhanvi

Reputation: 3380

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.

Upvotes: 1

Related Questions