14

How do i list all containers in Kubernetes cluster using kubectl?

Current documentation doesn't mention anything like 'container' resource.

kubectl get pod -o json

lists all pods which contains container descriptions. But is it possible to list containers as first class citizens?

olivierg
  • 524
  • 2
  • 8
  • 27
czerny
  • 255
  • 1
  • 2
  • 8

6 Answers6

14

This will get all container with the namespace in a pretty format:

kubectl get pods --all-namespaces -o=custom-columns=NameSpace:.metadata.namespace,NAME:.metadata.name,CONTAINERS:.spec.containers[*].name
  • 1
    To prevent expansion of wildcards by my shell, I had to put a single quote around the `-o=` value, like `kubectl get pods --all-namespaces -o='custom-columns=NameSpace:.metadata.namespace,NAME:.metadata.name,CONTAINERS:.spec.containers[*].name'` – Steve Campbell Jan 20 '21 at 15:28
4

When you don't use the namespace flag you are only looking in the default namespace. Try

kubectl get pod --all-namespaces

That will list all the pods in your cluster

You can filter via namespace like

kubectl get pod -n kube-system

To show all containers

kubectl get pods --all-namespaces -o jsonpath={.items[*].spec.containers[*].name}
Mike
  • 22,310
  • 7
  • 56
  • 79
  • Commands in the answer list pods. Is there any way how to list containers in a similar way? – czerny Sep 13 '17 at 16:24
  • 1
    you can do this `kubectl get pods --all-namespaces -o jsonpath={.items[*].spec.containers[*].name}` its not as nicely printed out – Mike Sep 13 '17 at 16:31
  • Thanks. Could you please copy it to the main answer since the question is really about containers, not pods? – czerny Sep 13 '17 at 17:39
  • done.. I made an edit – Mike Sep 14 '17 at 10:58
2
kubectl get pods --all-namespaces

EDIT : damn, burned ! :)

SBO
  • 544
  • 1
  • 5
  • 12
1
kubectl describe pods <pod name>

This will list containers in pods of given name.

Ankur Agarwal
  • 457
  • 1
  • 6
  • 15
0

you can use -A shortcut instead of --all-namespaces

kubectl get pods -A
Saeed Kazemi
  • 13
  • 1
  • 4
0

You can list all containers in default namespace

kubectl get pods -o=jsonpath="{.items[*].spec.containers[*].name}"

Also you can user --namespace and --selector.

kubectl get pods --namespace default --selector app=nginx -o=jsonpath="{.items[*].spec.containers[*].name}"

Reference

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get https://kubernetes.io/docs/reference/kubectl/jsonpath/