Kubernetes: How to List All Resources in a Namespace

Updated: January 31, 2024 By: Guest Contributor Post a comment

Introduction

Kubernetes has revolutionized the way in which applications are deployed and managed in the cloud. When working with Kubernetes, namespaces are commonly used to create isolated environments for different applications or for different environments such as development, staging, and production. Knowing how to interact with these namespaces and the resources within them is crucial for Kubernetes administrators and developers. This tutorial will walk you through the process of listing all resources within a specific namespace using various methods and tools.

Understanding Namespaces

Kubernetes namespaces are intended to divide cluster resources between multiple users via a scope. Each namespace can contain resources like pods, services, configmaps, and more. By default, Kubernetes comes with a few namespaces out-of-the-box such as ‘default’, ‘kube-system’, and ‘kube-public’.

Before listing resources within a namespace, make sure the Kubernetes command-line tool kubectl is installed and configured to interact with your cluster. If not, you can install it following the instructions one of the following articles:

Listing All Resources with kubectl

The simplest method to list all resources within a namespace is to use the kubectl get all command. This command provides a list of resources like pods, services, daemonsets, deployments, replicasets, and statefulsets.

kubectl get all --namespace your-namespace-name

After running this command, you’ll see an output similar to the following:

NAME                                  READY   STATUS    RESTARTS   AGE
deployment.apps/my-application        2/2     Running   0          8m39s
pod/my-application-fd4c864d8-xkqk9   1/1     Running   0          8m39s
service/my-service                    ClusterIP   10.96.0.10            80/TCP   10m

Listing Specific Resource Types

If you are interested in listing a specific type of resource, such as configmaps or secret within a namespace, you can specify it in the command:

kubectl get configmaps --namespace your-namespace-name
kubectl get secrets --namespace your-namespace-name

Using Custom Resource Definitions (CRDs)

If you have Custom Resource Definitions (CRDs) in your cluster, ‘kubectl get all’ might not list those resources. To display them, you would need to list the specific resource type defined by the CRD. For example:

kubectl get crd-name --namespace your-namespace-name

Advanced: Use of JSONPath and Custom Columns

To create a more tailored overview of the resources in a namespace, we can use JSONPath queries to filter and format the output.

Here’s an example that shows how to list all the pods in a namespace while displaying their name and node they are running on:

kubectl get pods -o=jsonpath='{.items[*].metadata.name} {.items[*].spec.nodeName}' --namespace your-namespace-name

The output will be similar to this:

my-application-fd4c864d8-xkqk9 minikube

Using The Label Selector

Labels are key/value pairs that are attached to objects, such as pods. You may want to list resources by label to understand how resources are organized within your namespace. To list all resources with a specific label:

kubectl get all --selector app=my-application --namespace your-namespace-name

Only resources with the label ‘app=my-application’ will be shown in the namespace you specify.

Conclusion

This guide has demonstrated several effective ways to list all resources within a specific Kubernetes namespace. By using the different kubectl commands and options, you can easily filter and access the resources in isolated environments. This capability is fundamental for managing and maintaining healthy Kubernetes clusters.