How do you cleanly list all the containers in a kubernetes pod?

Charles L. picture Charles L. · Nov 25, 2015 · Viewed 70.7k times · Source

I am looking to list all the containers in a pod in a script that gather's logs after running a test. kubectl describe pods -l k8s-app=kube-dns returns a lot of info, but I am just looking for a return like:

etcd
kube2sky
skydns

I don't see a simple way to format the describe output. Is there another command? (and I guess worst case there is always parsing the output of describe).

Answer

Cory Klein picture Cory Klein · Jun 15, 2017

Answer

kubectl get pods POD_NAME_HERE -o jsonpath='{.spec.containers[*].name}'

Explanation

This gets the JSON object representing the pod. It then uses kubectl's JSONpath to extract the name of each container from the pod.