15

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
czerny
  • 265

6 Answers6

15

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
5

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,510
1
kubectl describe pods <pod name>

This will list containers in pods of given name.

1
kubectl get pods --all-namespaces

EDIT : damn, burned ! :)

SBO
  • 562
1

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/

-1

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

kubectl get pods -A