Skip to Content

2 ways to list all the containers in Kubernetes cluster

To list all the containers running in a Kubernetes cluster, you would typically start by listing all Pods, as containers are encapsulated within Pods in Kubernetes. Each Pod can run one or more containers.

Here’s how you can list all containers across all namespaces in a Kubernetes cluster:

Using kubectl to list all the containers

  1. List all Pods and their containers in all namespaces:
    You can use the kubectl command-line tool to list all Pods across all namespaces and then extract the container details from the Pod descriptions. Here’s a command that lists all Pods in all namespaces and uses a custom format to display the names of the containers within those Pods:
    kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"Namespace: "}{.metadata.namespace}{"\nPod: "}{}{"\nContainers:\n"}{range .spec.containers[*]}{"- "}{.name}{"\n"}{end}{"\n"}{end}'

    This command uses the jsonpath output format to iterate over all Pods (range .items[*]) and for each Pod, it iterates over all containers (range .spec.containers[*]) to print their names.

  2. Using kubectl with a loop (for more detailed inspection):
    If you want more detailed information about each container, you might need to run a more complex command or script. Here’s an example of using a loop to list all containers within each Pod in all namespaces:
    for ns in $(kubectl get ns --no-headers | cut -d " " -f1); do
      for pod in $(kubectl get pods --namespace "$ns" --no-headers | cut -d " " -f1); do
        echo "Namespace: $ns, Pod: $pod"
        kubectl get pod "$pod" --namespace "$ns" -o=jsonpath='{.spec.containers[*].name}' | tr -s '[[:space:]]' '\n' | awk '{print "- " $0}'

    This script loops through each namespace and then each Pod within that namespace, printing the namespace and Pod name followed by a list of containers within that Pod.

Using Kubernetes API to list all the containers

If you’re developing an application or a script that interacts with the Kubernetes API directly, you can query the /api/v1/pods endpoint and then parse the JSON response to list all Pods and their containers. This method requires more programming effort and familiarity with the Kubernetes API.

Interacting directly with the Kubernetes API to get a list of all Pods and their containers involves a few steps. Below is a high-level overview of how you can achieve this, along with a simple Python example using the requests library to query the /api/v1/pods endpoint.

Steps to Interact with the Kubernetes API:

  1. Access the Kubernetes API: Depending on your environment, accessing the Kubernetes API might require authentication. If you’re running your script from within a Pod in the cluster, you can use the service account token and CA certificate mounted inside the Pod. For external access, you might need to configure kubectl proxy or use credentials from your kubeconfig file.
  2. Query the /api/v1/pods Endpoint: Send a GET request to the /api/v1/pods endpoint. If you want to list Pods across all namespaces, make sure to use the endpoint without specifying a namespace. Otherwise, include the namespace in the endpoint URL.
  3. Parse the JSON Response: The response will be in JSON format, containing details about all the Pods. You’ll need to parse this JSON response to extract information about each Pod and the containers within it.

Python Example:

This example demonstrates how to query the /api/v1/pods endpoint from within a Pod in the cluster using Python.

First, ensure you have the requests library installed:

pip install requests

Then, you can use the following Python script:

import requests

# Path to the service account token and CA certificate
token_path = "/var/run/secrets/"
ca_cert_path = "/var/run/secrets/"

# Read the service account token
with open(token_path, 'r') as token_file:
    token =

# Kubernetes API URL for listing Pods in all namespaces
api_url = "https://kubernetes.default.svc/api/v1/pods"

# Headers for authentication
headers = {
    "Authorization": f"Bearer {token}"

# Perform the GET request to the Kubernetes API
response = requests.get(api_url, headers=headers, verify=ca_cert_path)

# Check if the request was successful
if response.status_code == 200:
    pods = response.json().get("items", [])
    for pod in pods:
        namespace = pod.get("metadata", {}).get("namespace", "Unknown")
        pod_name = pod.get("metadata", {}).get("name", "Unknown")
        print(f"Namespace: {namespace}, Pod: {pod_name}")
        containers = pod.get("spec", {}).get("containers", [])
        for container in containers:
            container_name = container.get("name", "Unknown")
            print(f"  - Container: {container_name}")
    print(f"Failed to get Pods: {response.status_code}")


  • The script reads the service account token and the CA certificate from the standard paths where they are mounted in Pods.
  • It sends a GET request to the Kubernetes API server using the internal DNS name kubernetes.default.svc.
  • The response is parsed to list the namespaces, Pod names, and container names.

Remember, if you’re running this script outside of a Kubernetes cluster, you’ll need a different method for authentication, such as using credentials from your kubeconfig file.


  • Performance: Listing all containers in a large cluster can be resource-intensive and may take some time. It’s important to consider the impact on the cluster’s performance, especially if you’re running these commands frequently.
  • Permissions: Depending on your cluster’s RBAC (Role-Based Access Control) settings, you might need sufficient permissions to list Pods across all namespaces.

These methods provide a comprehensive view of all containers running in your Kubernetes cluster, which is essential for management, monitoring, and troubleshooting tasks.