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

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.

Other supported output output templates are go-template, go-template-file, jsonpath-file. See http://kubernetes.io/docs/user-guide/jsonpath/ for how to use jsonpath template. See https://golang.org/pkg/text/template/#pkg-overview for how to use go template.

Update: Check this doc for other example commands to list container images: https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/


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.


Quick hack to avoid constructing the JSONpath query for a single pod:

$ kubectl logs mypod-123
a container name must be specified for pod mypod-123, choose one of: [etcd kubesky skydns]

Tags:

Kubernetes