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?

6 Answers 6

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
  • 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' Jan 20, 2021 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}
4
  • Commands in the answer list pods. Is there any way how to list containers in a similar way?
    – czerny
    Sep 13, 2017 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, 2017 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, 2017 at 17:39
  • done.. I made an edit
    – Mike
    Sep 14, 2017 at 10:58
2
kubectl get pods --all-namespaces

EDIT : damn, burned ! :)

1
kubectl describe pods <pod name>

This will list containers in pods of given name.

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/

0

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

kubectl get pods -A

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.