176

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.
  • 5,795
  • 10
  • 40
  • 60

15 Answers15

204

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
  • 51,188
  • 43
  • 183
  • 243
  • 4
    For those who want to run this command in a specific namespace then, don't forget to add `--namespace` or `-n` in this command. For i.e. (`kubectl -n YOUR_NAMESPACE get pods POD_NAME_HERE -o jsonpath='{.spec.containers[*].name}'`) – Bhavin_m Jul 21 '22 at 11:33
  • That will not include the `initContainers`. For those you need: `kubectl -n get pods -o jsonpath='{.spec.initContainers[*].name}'` – Marcello Romani Jun 22 '23 at 09:26
90

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
  • 2,635
  • 14
  • 17
59

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
  • 50,745
  • 59
  • 165
  • 240
  • 1
    This answer is exactly what it says.. a quick hack. Really useful :) Of course cannot be used in a script kind of thing. It serves the requirement. – Rakesh N Aug 28 '18 at 06:42
  • Latest one also gives both `containers` and `init containers` – Neo Mar 29 '19 at 01:23
  • @Neo I don't see `init containers` with this way on v1.18.8 :( – subtleseeker Oct 30 '20 at 05:03
  • 3
    I love this. The only downside is that it doesn't work for pods that have a single container. It just dumps the log for the one container. – Joel Mellon Nov 03 '21 at 17:25
  • 2
    Note that this quick hack doesn't work if the pod has annotaton `kubectl.kubernetes.io/default-container` (or the deprecated annotation `kubectl.kubernetes.io/default-logs-container`)... You'll then get the log of the default container instead. – t0r0X Jul 30 '22 at 21:47
23

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}'
EzLo
  • 13,780
  • 10
  • 33
  • 38
0bel1sk
  • 505
  • 5
  • 4
  • @EzLo I'm not sure if I like the single line format better. I'm not a fan of having to scroll. – 0bel1sk May 02 '19 at 11:54
  • you can show both alternatives. Feel free to change back if you want. Using the code formatting (CTRL+K) is usually better than the quote (CTRL+Q) because of the typography, for code. – EzLo May 02 '19 at 12:13
  • Is there a way i can have the pod name listed for each container in this format? – Sam Thomas Jan 08 '20 at 18:18
  • To avoid need of scrolling and manual search - pipe it to `less` or to `vi`: ```COMMAND | less``` or ```vi<(COMMAND)``` – Sergey S Sep 22 '20 at 09:04
  • scrolling at stack overflow @SergeySmolnikov – 0bel1sk Sep 23 '20 at 14:23
  • The query works, Thanks! but aren't you missing an {end} element at the end? – Tal Aloni May 31 '23 at 18:40
20

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
Paweł Załuski
  • 498
  • 5
  • 12
13

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
  • 595
  • 6
  • 10
11

To get the output in the separate lines (init and non-init containers):

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

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

goulashsoup
  • 2,639
  • 2
  • 34
  • 60
Binzari Catalin
  • 172
  • 1
  • 8
4

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
  • 4,830
  • 3
  • 23
  • 32
4

Use below command:

kubectl get pods -o=custom-columns=PodName:.metadata.name,Containers:.spec.containers[*].name,Image:.spec.containers[*].image
Sandeep Arora
  • 389
  • 3
  • 4
3

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>

LostAtSea
  • 5,384
  • 1
  • 9
  • 11
3

There are enough answers here but sometimes you want to see a deployment object pods' containers and initContainers. To do that;

1- Retrieve the deployment name

kubectl get deployment

2- Retrieve containers' names

kubectl get deployment <deployment-name> -o jsonpath='{.spec.template.spec.containers[*].name}'

3- Retrieve initContainers' names

kubectl get deployment <deployment-name> -o jsonpath='{.spec.template.spec.initContainers[*].name}'

hbceylan
  • 968
  • 10
  • 10
2

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

kubectl get pod <pod name> -n <namespace name> -o yaml
Kalhara Tennakoon
  • 1,302
  • 14
  • 20
2

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

kubectl describe pod <podname>

Laksh
  • 31
  • 1
1

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
  • 2,150
  • 3
  • 25
  • 34
-5

Easiest way to know the containers in a pod:

kubectl logs -c -n

  • Note: answers that are very brief and/or are questions back to the poster should probably be comments. You only need 50 rep points to comment under a question - could you move this there? – Tyler2P Dec 07 '20 at 15:46
  • I get response error: expected 'logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER]'. POD or TYPE/NAME is a required argument for the logs command See 'kubectl logs -h' for help and examples – mabraham Jan 18 '21 at 08:48