How to List All Containers in a Pod in Kubernetes Cluster

A kubernetes pod may contain a single pod or multiple related pods. There are few use cases where you might want to list the containers in a single pod. One such use case is to get the logs of specific containers.

To get the list of containers inside a pod, use the following command.

kubectl get pods [pod-name-here] -n [namespace] -o jsonpath='{.spec.containers[*].name}*

For example.

kubectl get pods redis-bootstrap -n redis-cluster -o jsonpath='{.spec.containers[*].name}*

Alternatively, you can use the pod to describe the command.

kubectl describe pods redis-bootstrap -n redis-cluster

Bonus: Register for kubernetes CKA/CKAD certification exam today and get 25% discount using a coupon DCUBE20. Plus use cka study guide for reference materials

2 Likes

You can use get and choose one of the supported output template with the --output (-o) flag.

Take jsonpath for example, kubectl get pods -l k8s-app=kube-dns -o jsonpath={.items[].spec.containers[].name} gives you etcd kube2sky skydns.

1 Like

I think there is a typo in the above command.
Incorrect:

kubectl get pods [pod-name-here] -n [namespace] -o jsonpath='{.spec.containers[*].name}*

Correct:

kubectl get pods [pod-name-here] -n [namespace] -o jsonpath='{.spec.containers[*].name}'
1 Like