How to List All Containers in a Pod in Kubernetes Cluster

(Mike) #1

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 describe command.

kubectl describe pods redis-bootstrap -n redis-cluster

(Santo Goerge) #2

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.