106
votes

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

12

12 Answers

100
votes

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.

68
votes

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/

37
votes

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]
13
votes

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}'
11
votes

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

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

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

kubectl get pod <pod name> -n <namespace name> -o yaml
1
votes

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

1
votes

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>

1
votes

Use below command:

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

Easiest way to know the containers in a pod:

kubectl logs -c -n

0
votes

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