Skip to main content
Kubernetes

Get All Resources in a Namespace in Kubernetes

Learn how to get all resources in a namespace in Kubernetes. Also learn to filter it out for more precise result.

β€” LHB Community

Warp Terminal

Namespaces in Kubernetes act as virtual clusters within a physical cluster. They provide a way to separate different environments (such as development, testing, and production) or separate users or teams that are working on shared clusters.

In large Kubernetes clusters, there are often dozens or hundreds of resources (pods, services, config maps, secrets, etc.) running simultaneously. When troubleshooting, auditing, or managing resources, it’s useful to get an overview of everything running within a particular namespace.

While you could list all resources in a particular namespace using this:

kubectl get all -n <namespace>

There is a little more to it and I'll share that in this tutorial.

Listing all resources in a namespace

The simplest way to get all resources in a specific namespace is by using the kubectl get all command. This will list pods, services, deployments, replica sets, and other essential resources.

Basic syntax:

kubectl get all -n <namespace>

For example, if you want to list all resources in the development namespace, the command would be:

kubectl get all -n development

Output:

NAME                           READY   STATUS    RESTARTS   AGE
pod/node-app-56b7db8f87-n5mfw    1/1     Running   0          2d
pod/node-app-56b7db8f87-rkd8v    1/1     Running   0          2d

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
service/node-app-svc   ClusterIP   10.104.183.188   <none>        80/TCP     2d

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/node-app     2/2     2            2           2d

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/node-app-56b7db8f87    2         2         2       2d

This command retrieves all the pods, services, deployments, and replica sets within the specified namespace.

Listing specific resource types in a namespace

If you want to focus on a specific type of resource within the namespace (e.g., only pods or services), you can modify the kubectl get command.

To list only pods, use the below syntax:

kubectl get pods -n <namespace>

For example, to list only pods in a development namespace, run:

kubectl get pods -n development

Output:

NAME                         READY   STATUS    RESTARTS   AGE
pod/node-app-56b7db8f87-n5mfw   1/1     Running   0          2d
pod/node-app-56b7db8f87-rkd8v   1/1     Running   0          2d

To list only services, run the following command.

kubectl get svc -n development

Output:

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
node-app-svc           ClusterIP   10.104.183.188  <none>        80/TCP     2d

This flexibility helps when you only need to troubleshoot or examine a particular type of resource.

Listing resources with more details

To get more information about resources in a namespace, you can use the -o wide flag, which provides additional details such as the node where the resource is running and its IP addresses.

For example, to get more detail information of development namespace, run:

kubectl get all -n development -o wide

Output:

NAME                         READY   STATUS    RESTARTS   AGE   IP             NODE           NOMINATED NODE
pod/node-app-56b7db8f87-n5mfw  1/1     Running   0          2d    192.168.1.10   worker-node1   <none>
pod/node-app-56b7db8f87-rkd8v  1/1     Running   0          2d    192.168.1.11   worker-node2   <none>

This output is useful for debugging networking issues or understanding which node your pods are running on.

Listing custom resources in a namespace

If your cluster uses custom resources (CRDs), you can also list them using kubectl. Custom resources could be specific to your environment or third-party operators installed in your cluster.

For example, if you are using a custom resource called redis, you can list all redis resources in a namespace using:

kubectl get redis -n <namespace>

Make sure to replace <namespace> with the actual namespace name and redis with your custom resource name.

Getting resource usage information

To get resource usage (CPU and memory) for resources in a namespace, you can use the kubectl top command. This is helpful for monitoring the resource consumption of your pods.

For example, to get resource usage information of all pods within development namespace, run:

kubectl top pod -n development

Output:

NAME                         CPU(cores)   MEMORY(bytes)
node-app-56b7db8f87-n5mfw      50m          100Mi
node-app-56b7db8f87-rkd8v      45m          95Mi

This command provides you with a real-time snapshot of CPU and memory usage for each pod in the namespace.

Filtering output with label selectors

If you want to filter resources in a namespace based on labels, you can use the -l flag with kubectl get. Labels allow you to organize and select resources based on specific attributes.

For example, if you want to list all pods with the label app=node-app, you would run:

kubectl get pods -n development -l app=node-app

Output:

NAME                         READY   STATUS    RESTARTS   AGE
node-app-56b7db8f87-n5mfw      1/1     Running   0          2d
node-app-56b7db8f87-rkd8v      1/1     Running   0          2d

This is useful when you're managing multiple resources with different labels and only want to focus on a subset.

Conclusion

Understanding how to list and query resources within a namespace is crucial for managing Kubernetes clusters.

By using the kubectl get command along with its various flags and options, you can retrieve comprehensive information about all resources within a namespace, filter by specific resource types, monitor resource usage, and troubleshoot effectively.

✍️
Author: Hitesh Jethwa has more than 15+ years of experience with Linux system administration and DevOps. He likes to explain complicated topics in easy to understand way.
LHB Community