ThisCalculator

Everyday

List All Containers in a Kubernetes Cluster

Updated · Standard formula, shown below

List All Containers in a Kubernetes Cluster

To list all containers in a Kubernetes cluster, you first list all pods across every namespace, then extract the container names from each pod’s specification. The most direct command is kubectl get pods --all-namespaces -o jsonpath='{.items[*].spec.containers[*].name}', which returns a space-separated list of every container name in the cluster. This article explains the command, its variations, and the underlying concepts you need to manage containers effectively.

Background: Containers vs. Pods

Kubernetes does not manage containers directly; it manages pods, which are the smallest deployable units. A pod can hold one or more containers that share the same network namespace, storage volumes, and lifecycle. When you ask to “list all containers,” you are really asking to inspect the container definitions inside every pod across all namespaces.

Each pod also may have init containers that run before the main containers start, and ephemeral containers for debugging. These are separate fields in the pod spec and are not included in the default containers list. To get a complete picture, you need to query all three types.

How to List Containers with kubectl

The most common approach uses kubectl with JSONPath or custom columns. Here are the essential commands:

  • All container names (all namespaces):
    kubectl get pods --all-namespaces -o jsonpath='{.items[*].spec.containers[*].name}'
  • With pod names for context:
    kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace}{" "}{.metadata.name}{" "}{.spec.containers[*].name}{"\n"}{end}'
  • Using custom columns (easier to read):
    kubectl get pods --all-namespaces -o custom-columns=NAMESPACE:.metadata.namespace,POD:.metadata.name,CONTAINERS:.spec.containers[*].name
  • Including init and ephemeral containers:
    kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace}{" "}{.metadata.name}{" "}{.spec.containers[*].name}{" "}{.spec.initContainers[*].name}{" "}{.spec.ephemeralContainers[*].name}{"\n"}{end}'

If you want a simple list without pod names, the first command is sufficient. To see which namespace each container belongs to, use the custom-columns version.

Understanding the Output

The output from these commands is a list of container names, but it does not tell you the image, status, or resource usage. To get more detail, you can use kubectl describe pod <pod-name> -n <namespace> for a single pod, or kubectl get pods --all-namespaces -o wide to see node and IP information. For a cluster-wide view of running containers (not just definitions), you can use kubectl get pods --all-namespaces -o json | jq '.items[] | {namespace: .metadata.namespace, pod: .metadata.name, containers: [.spec.containers[].name]}' if you have jq installed.

Remember that the JSONPath command lists container names from the pod spec, which includes containers that may have exited or failed. To see only currently running containers, you would need to filter by pod status, which is more complex and usually not necessary for a simple inventory.

Common Mistakes and Pitfalls

  • Forgetting --all-namespaces: Without it, you only see containers in the current namespace (default is default).
  • Confusing pods with containers: A pod with two containers will show two names, not one. The command lists each container individually.
  • Ignoring init containers: If you need a complete list, include .spec.initContainers and .spec.ephemeralContainers.
  • Assuming the list is live: The output is a snapshot of the pod spec. If a pod is being recreated, the list may change between runs.

Alternatives: Using kubectl Plugins and Other Tools

If you frequently need container inventories, consider using kubectl plugins like kubectl-tree or kubectl-view-allocations, which provide visual representations. A more powerful option is to use a Kubernetes dashboard (like the official Kubernetes Dashboard) that shows all pods and their containers in a GUI. For programmatic access, you can use the Kubernetes API directly with curl or a client library (Python, Go, etc.) to fetch pod specs and extract container names.

Why You Might Need This List

Listing all containers is often the first step in auditing or troubleshooting. For example, you might want to:

  • Check if a specific container image is running in multiple namespaces.
  • Count the total number of containers to estimate resource usage.
  • Verify that a deployment has the expected number of replicas and containers.
  • Find containers that are not using the latest image tag.

In production, you typically combine this with kubectl get pods -o yaml to inspect full specifications, or with monitoring tools like Prometheus to track container metrics.

Frequently Asked Questions

How do I list containers in a specific namespace?

Replace --all-namespaces with -n <namespace>. For example: kubectl get pods -n my-namespace -o jsonpath='{.items[*].spec.containers[*].name}'.

Does this include containers in the kube-system namespace?

Yes, --all-namespaces includes every namespace, including system namespaces like kube-system and kube-public. If you want to exclude them, you can filter with grep -v or use a JSONPath that excludes those namespaces, but that is more complex.

Can I list containers by image name?

Yes, you can modify the JSONPath to output the image instead of the name: kubectl get pods --all-namespaces -o jsonpath='{.items[*].spec.containers[*].image}'. To see both, use custom columns: -o custom-columns=CONTAINER:.spec.containers[*].name,IMAGE:.spec.containers[*].image.

Why do I see duplicate container names?

Container names are unique within a pod, but the same name can appear in different pods (e.g., every pod in a deployment may have a container named app). The JSONPath output lists them all, so duplicates are expected. If you want unique names, pipe to sort -u.

How do I count the total number of containers?

Pipe the output to wc -w to count words (each container name is a word). For example: kubectl get pods --all-namespaces -o jsonpath='{.items[*].spec.containers[*].name}' | wc -w. This counts only regular containers, not init or ephemeral ones.

Is there a way to list containers without kubectl?

Yes, you can use the Kubernetes API directly. First, get a token and API server address, then call GET /api/v1/pods with the appropriate headers. You can parse the JSON response to extract container names. Tools like kubectl are just wrappers around this API.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.