Programming

Listing all resources in a namespace

25 September 2026 · 8 min read

Listing all resources in a namespace

Managing resources within a Kubernetes namespace effectively requires a clear understanding of what’s deployed and how it’s configured. Listing all resources in a namespace is a fundamental task for administrators and developers alike, enabling better oversight, troubleshooting, and overall cluster management. This process can be achieved through various command-line tools and scripting techniques, providing different levels of detail and filtering capabilities. Knowing how to accurately list and interpret these resources is crucial for maintaining a healthy and efficient Kubernetes environment. Efficiently managing and visualizing these resources ensures applications perform optimally and are easily maintainable. This blog post will provide a comprehensive guide to accomplishing this vital task.

Understanding Kubernetes Namespaces

Kubernetes namespaces are virtual clusters within a physical cluster. They provide a way to isolate and organize resources, allowing multiple teams or projects to share the same cluster without interfering with each other. Each namespace has its own set of resources, such as pods, services, deployments, and configmaps. This logical separation is key to implementing multi-tenancy and improving resource utilization. Without namespaces, managing large clusters would become incredibly complex and prone to errors. Namespaces significantly improve overall cluster organization.

Namespaces act as a scope for names. Resource names need to be unique within a namespace but not across namespaces. This allows teams to use the same resource names in different namespaces without conflict. For example, you can have a “development” namespace, a “staging” namespace, and a “production” namespace, each containing a service named “web-app.” This simplifies deployment and management across different environments. Proper namespace design and management are critical for maintaining a well-organized and scalable Kubernetes infrastructure. As stated by the Kubernetes documentation, “Namespaces are intended for use in environments with many users spread across multiple teams, or projects.” Kubernetes Namespaces Documentation

Effective namespace management also enhances security. By assigning different namespaces to different teams or applications, you can implement granular access control policies. For instance, you can grant a team access to only their specific namespace, preventing them from accessing or modifying resources in other namespaces. This approach minimizes the risk of accidental or malicious interference and improves the overall security posture of the cluster. Role-Based Access Control (RBAC) policies are often used in conjunction with namespaces to achieve fine-grained access control. This combination ensures that only authorized users and services can interact with specific resources within a given namespace.

Using kubectl to List Resources in a Namespace

The kubectl command-line tool is the primary way to interact with a Kubernetes cluster. It offers a wide range of commands for managing resources, including the ability to list all resources in a given namespace. To achieve this, you can use the kubectl get all command, specifying the namespace using the -n or –namespace flag. This command retrieves all resource types within the specified namespace, providing a comprehensive overview of the deployments. It is a fundamental command for any Kubernetes user.

The kubectl get all -n command is a simple yet powerful way to list resources. However, it can sometimes return a lot of information, making it difficult to find what you’re looking for. To filter the results, you can specify the resource type you’re interested in. For example, kubectl get pods -n will only list the pods in the specified namespace. Similarly, kubectl get deployments -n will only list deployments. This targeted approach allows you to quickly identify and inspect specific resources. For example, to see all pods in the production namespace, the command would be: kubectl get pods -n production. This targeted search strategy is highly effective.

Here’s the featured snippet paragraph: To list all resources in a namespace using kubectl, use the command kubectl get all -n . This command retrieves all resource types within the specified namespace. For instance, to list all resources in the default namespace, you would run kubectl get all -n default. This command is invaluable for quick overviews of deployed resources. It’s also possible to filter by resource type using commands like kubectl get pods -n . This makes kubectl a versatile tool for resource management.

Filtering and Sorting Resources

While kubectl get all provides a basic listing, you often need more advanced filtering and sorting capabilities. kubectl provides several options for refining your search. You can use the -l or –selector flag to filter resources based on labels. Labels are key-value pairs that you can attach to resources to categorize and organize them. For example, if you have labeled your web application pods with app=web-app, you can list only those pods using kubectl get pods -n -l app=web-app. This is extremely useful in environments with many applications.

Furthermore, you can use the –sort-by flag to sort the results based on a specific field. For example, to sort pods by their name, you can use kubectl get pods -n –sort-by=.metadata.name. This can be particularly helpful when you’re dealing with a large number of resources and want to quickly find a specific one. Combining filtering and sorting allows you to precisely target the resources you need to manage. Learning these techniques can significantly improve your efficiency when working with Kubernetes. You can also output results in different formats using the -o flag, such as JSON or YAML, for further processing with other tools. Kubernetes JSONPath Guide provides more details about filtering and formatting.

To summarize, these are the main filtering and sorting options:

  • Label Selectors: Use -l or –selector to filter based on labels.
  • Sorting: Use –sort-by to sort results based on a specific field.
  • Output Formatting: Use -o to format output as JSON or YAML.

Advanced Techniques: Using jq and Scripting

For more complex filtering and manipulation, you can combine kubectl with other command-line tools like jq. jq is a powerful JSON processor that allows you to extract, transform, and filter data from JSON output. By piping the output of kubectl to jq, you can perform sophisticated queries and generate custom reports. This approach is particularly useful when you need to automate tasks or integrate with other systems. This gives you very granular control over displayed data.

For example, you can use kubectl get pods -n -o json | jq ‘.items[].metadata.name’ to extract only the names of the pods in a given namespace. This command first retrieves the pod information in JSON format and then uses jq to extract the name field from each pod’s metadata. This is a simple example, but jq can handle much more complex queries, allowing you to perform advanced filtering, aggregation, and transformation. Many DevOps engineers rely heavily on jq for managing Kubernetes resources. jq Official Documentation provides a comprehensive guide to its capabilities.

Here’s an example of how to create a script to list all resources of a specific type in all namespaces:

  1. Create a script file: touch list_resources.sh
  2. Edit the script: Add the following content: ``` !/bin/bash RESOURCE_TYPE=$1 for NAMESPACE in $(kubectl get namespaces -o jsonpath="{.items[].metadata.name}"); do echo “Resources of type $RESOURCE_TYPE in namespace $NAMESPACE:” kubectl get $RESOURCE_TYPE -n $NAMESPACE done
  3. Make the script executable: chmod +x list_resources.sh
  4. Run the script: ./list_resources.sh pods
Infographic here showing kubectl commands and jq usage
FAQ ---
How do I list all pods across all namespaces?
Use the command kubectl get pods --all-namespaces.
How do I list services in a specific namespace?
Use the command kubectl get services -n .
Can I use wildcards with kubectl to list resources?
No, kubectl does not directly support wildcards for resource names. You can use label selectors or jq for more complex filtering.
How can I save the list of resources to a file?
You can redirect the output of kubectl to a file, e.g., kubectl get all -n > resources.txt.
Listing all resources in a namespace is a fundamental skill for Kubernetes administrators and developers. By mastering the techniques outlined in this article, including the use of kubectl, filtering options, and scripting, you can effectively manage your Kubernetes deployments. Don't hesitate to explore further by experimenting with different commands and tools to find the best approach for your specific needs. Learning these techniques will empower you to efficiently manage your Kubernetes resources, troubleshoot issues quickly, and maintain a healthy and scalable cluster. This ensures that your applications are running smoothly and efficiently.

Now that you understand how to list resources, why not delve deeper into resource management within Kubernetes? Check out our guide on optimizing resource allocation in Kubernetes for advanced tips and tricks to enhance your cluster’s performance. Continue to explore and refine your skills to become a Kubernetes expert.

Question & Answer :
I would like to see all resources in a namespace.

Doing kubectl get all will, despite of the name, not list things like services and ingresses.

If I know the the type I can explicitly ask for that particular type, but it seems there is also no command for listing all possible types. (Especially kubectl get does for example not list custom types).

How to show all resources before for example deleting that namespace?

Based on this comment , the supported way to list all resources is to iterate through all the api versions listed by kubectl api-resources:

kubectl api-resources enumerates the resource types available in your cluster.

this means you can combine it with kubectl get to actually list every instance of every resource type in a namespace:

kubectl api-resources --verbs=list --namespaced -o name \ | xargs -n 1 kubectl get --show-kind --ignore-not-found -l <label>=<value> -n <namespace>