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

11/25/2015

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).

-- Charles L.
kubernetes

7 Answers

7/25/2016

If you use json as output format of kubectl get you get plenty details of a pod. With json processors like jq it is easy to select or filter for certain parts you are interested in.

To list the containers of a pod the jq query looks like this:

kubectl get --all-namespaces --selector k8s-app=kube-dns --output json pods \
  | jq --raw-output '.items[].spec.containers[].name'

If you want to see all details regarding one specific container try something like this:

kubectl get --all-namespaces --selector k8s-app=kube-dns --output json pods \
  | jq '.items[].spec.containers[] | select(.name=="etcd")'
-- webwurst
Source: StackOverflow

8/23/2018

I use this to display image versions on the pods.

kubectl get pods  -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{end}{end}' && printf '\n'

It's just a small modification of script from here, with adding new line to start next console command on the new line, removed commas at the end of each line and listing only my pods, without service pods (e.g. --all-namespaces option is removed).

-- Dmitriy Popov
Source: StackOverflow

11/25/2015

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/

-- janetkuo
Source: StackOverflow

6/14/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.

-- Cory Klein
Source: StackOverflow

7/24/2017

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]
-- Borek Bernard
Source: StackOverflow

3/1/2017

if you want a clear output of which containers are from each Pod

kubectl get po -l k8s-app=kube-dns \
   -o=custom-columns=NAME:.metadata.name,CONTAINERS:.spec.containers[*].name
-- Gongora
Source: StackOverflow

5/2/2019

I put some ideas together into the following:

Simple line:

kubectl get po -o jsonpath='{range .items[*]}{"pod: "}{.metadata.name}{"\n"}{range .spec.containers[*]}{"\tname: "}{.name}{"\n\timage: "}{.image}{"\n"}{end}'

Split (for readability):

kubectl get po -o jsonpath='
    {range .items[*]}
    {"pod: "}
    {.metadata.name}
    {"\n"}{range .spec.containers[*]}
    {"\tname: "}
    {.name}
    {"\n\timage: "}
    {.image}
    {"\n"}
    {end}'
-- Shawn
Source: StackOverflow