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

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

14 Answers

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.

0

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 for how to use jsonpath template. See for how to use go template.

Update: Check this doc for other example commands to list container images:

3

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]
4

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}'
5

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

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")'

How to list BOTH init and non-init containers for all pods

kubectl get pod -o="custom-columns=NAME:.metadata.name,INIT-CONTAINERS:.spec.initContainers[*].name,CONTAINERS:.spec.containers[*].name"

Output looks like this:

NAME INIT-CONTAINERS CONTAINERS
helm-install-traefik-sjts9 <none> helm
metrics-server-86cbb8457f-dkpqm <none> metrics-server
local-path-provisioner-5ff76fc89d-vjs6l <none> local-path-provisioner
coredns-6488c6fcc6-zp9gv <none> coredns
svclb-traefik-f5wwh <none> lb-port-80,lb-port-443
traefik-6f9cbd9bd4-pcbmz <none> traefik
dc-postgresql-0 init-chmod-data dc-postgresql
backend-5c4bf48d6f-7c8c6 wait-for-db backend

To see verbose information along with configmaps of all containers in a particular pod, use this command:kubectl describe pod/<pod name> -n <namespace name>

Use below command:

kubectl get pods -o=custom-columns=PodName:.metadata.name,Containers:.spec.containers[*].name,Image:.spec.containers[*].image

To get the output in the separate lines:

kubectl get pods POD_NAME_HERE -o jsonpath='{range .spec.containers[*]}{.name}{"\n"}{end}'

Output: base-container
sidecar-0
sidecar-1
sidecar-2

Use below command to see all the information of a particular pod

kubectl get pod <pod name> -n <namespace name> -o yaml
0

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

For overall details about the pod try following command to get the container details as well

kubectl describe pod <podname>

Easiest way to know the containers in a pod:

kubectl logs -c -n

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like