google cloud platform

How to list all Containers running in Kubernetes Pod ?

This tutorial guides you to list all containers running in kubernetes pod. Let’s see how to use kubectl command to list all of the container images or container names for Pods running in a cluster.

List all containers running in Kubernetes Pod

Note, before you begin this exercise make sure that you have a kubernetes cluster, and kubectl command should be configured so that you would be able to communicate with your cluster.

To create a cluster follow this GCP tutorial Creating a Zonal Cluster.

Once you had created cluster, I will assume that you have Pods with containers running. Now, let’s see how to list all of the Pods running in a cluster for a namespace lat’s say mv using jsonpath.

The following command will fetch all the Pods running in a cluster with namespace mv.

$ kubectl get pods -n mv

NAME                           READY   STATUS    RESTARTS   AGE
helloserver-5486975f5c-dr6d2   2/2     Running   0          57m

List all containers by Pod

The following command list all the containers with name by Pod for all namespaces.

$ kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.name}{", "}{end}{end}' |sort

canonical-service-controller-manager-66b6979d9d-z9ww2:  kube-rbac-proxy, manager,
event-exporter-gke-564fb97f9-5b2q5:     event-exporter, prometheus-to-sd-exporter,
fluentbit-gke-9gmhx:    fluentbit, fluentbit-gke,
fluentbit-gke-bsx4g:    fluentbit, fluentbit-gke,
gke-connect-agent-20210312-01-00-79d6cbb99c-l2b2s:      gke-connect-agent-20210312-01-00,
gke-mcs-importer-5d7454bb67-8cj77:      gke-mcs-importer,
gke-metrics-agent-cpdlq:        gke-metrics-agent,
gke-metrics-agent-rdm97:        gke-metrics-agent,
helloserver-5486975f5c-dr6d2:   main, istio-proxy,
istiod-asm-191-1-86ccfd8654-fdw4z:      discovery,
istiod-asm-191-1-86ccfd8654-mzcts:      discovery,
istio-ingressgateway-b5d7bcfbf-rbhlr:   istio-proxy,
istio-ingressgateway-b5d7bcfbf-vjtcf:   istio-proxy,
kube-dns-57fcf698d8-kg844:      kubedns, dnsmasq, sidecar, prometheus-to-sd,
kube-dns-57fcf698d8-wcgwh:      kubedns, dnsmasq, sidecar, prometheus-to-sd,
kube-dns-autoscaler-7f89fb6b79-gb56q:   autoscaler,
kube-proxy-gke-cluster-1-default-pool-2f3cced3-b492:    kube-proxy,
kube-proxy-gke-cluster-1-default-pool-2f3cced3-kvbn:    kube-proxy,
l7-default-backend-7fd66b8b88-7nmc4:    default-http-backend,
mcs-core-dns-6f76448c9d-rjvzq:  mcs-core-dns,
mcs-core-dns-6f76448c9d-zj84k:  mcs-core-dns,
mcs-core-dns-autoscaler-7df4c85b74-hxhfg:       mcs-core-dns-autoscaler,
metrics-server-v0.3.6-7c5cb99b6f-2clrj: metrics-server, metrics-server-nanny,
pdcsi-node-tgcb4:       csi-driver-registrar, gce-pd-driver,
pdcsi-node-xpp79:       csi-driver-registrar, gce-pd-driver,
stackdriver-metadata-agent-cluster-level-5ff64fbb78-j4zqh:      metadata-agent, metadata-agent-nanny,

By Pod label

To list all the containers filtering by Pod label, run the following command.

$ kubectl get pods --all-namespaces -o=jsonpath="{..image}" -l app=helloserver

By Pod namespace

Finally, to list all containers running in a kubernetes Pod by namespace you need to run the following command.

$ kubectl get pods -n mv -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.name}{", "}{end}{end}' |sort

helloserver-5486975f5c-dr6d2:   main, istio-proxy,

From the above response it is understood that there are two containers running inside the Pod “helloserver-5486975f5c-dr6d2” in the namespace “mv“. This  is how I check whether the containers are up and running.

Also, there is an alternative to jsonpath. Kubernetes supports go-templates, to format the output.

For example,

$ kubectl get pods --all-namespaces -o go-template --template="{{range .items}}{{range .spec.containers}}{{.name}} {{end}}{{end}}"

kube-rbac-proxy manager gke-connect-agent-20210312-01-00 gke-mcs-importer istio-proxy istio-proxy discovery discovery event-exporter prometheus-to-sd-exporter fluentbit fluentbit-gke fluentbit fluentbit-gke gke-metrics-agent gke-metrics-agent kubedns dnsmasq sidecar prometheus-to-sd kubedns dnsmasq sidecar prometheus-to-sd autoscaler kube-proxy kube-proxy default-http-backend mcs-core-dns mcs-core-dns mcs-core-dns-autoscaler metrics-server metrics-server-nanny csi-driver-registrar gce-pd-driver csi-driver-registrar gce-pd-driver metadata-agent metadata-agent-nanny main istio-proxy

That’s it. Hope it helped 🙂

References

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments