user674669
user674669

Reputation: 12342

List all containers in all pods with status

I am trying to get a list of all non-READY containers in all pods to debug a networking issue in my cluster.

Is it possible to use kubectl to get a clean list of all containers in all pods with their status (READY/..)?

I am currently using

$ kubectl get pods 

However, the output can be huge and it can be difficult to know which containers are READY and which ones have issues.

Thanks.

Upvotes: 2

Views: 7415

Answers (3)

Jiehong
Jiehong

Reputation: 885

Using jq, we can list all pods and their containers' statuses:

kubectl get pods --output=json | jq --raw-output '.items | map([.metadata.name, (.status.containerStatuses | map(.name + ": " + (.ready | tostring)))[]]) | .[] | join("\n ")'

This displays something like this:

pod-1234-auiend
  container1: true
  container2: false
another-pod-2454-123123:
  container1: true
(...)

Upvotes: 1

Luke
Luke

Reputation: 770

I've adapted @joshua-oliphant's answer to include container-names:

kubectl get pods -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .status.containerStatuses[*]}{.name}{": "}{.ready}{", "}{end}{end}'

This will display all pods including all containers in the current namespace with their ready-state such as:

pod-name: container-name-a: true, container-name-b: false

Upvotes: 1

Joshua Oliphant
Joshua Oliphant

Reputation: 932

kubectl get pods -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .status.containerStatuses[*]}{.ready}{", "}{end}{end}'

Adapted from this doc: https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/#list-containers-by-pod

Edit to describe what the jsonpath is doing:

From what I understand with the jsonpath, range iterates all of the .items[*] returned by getting the pods. \n is added to split the result to one per line, otherwise the result would come to one line. To see how the rest work, you should choose one of your pods and run: kubectl get pod podname -o yaml
.metadata.name corresponds to

apiVersion: v1
kind: Pod
metadata:
  name: podname

Similarly, .status.containerStatuses[*] corresponds to the list of container statuses that should be towards the bottom.

Upvotes: 7

Related Questions