Kubernetes objects called namespaces divide a single Kubernetes cluster into numerous virtual clusters. Every Kubernetes namespace determines the limits for the Kubernetes Names it includes, which implies that each object in the cluster has a unique identity. For separating and administering Kubernetes clusters, namespaces are fundamental objects. We can use namespaces to logically separate and assign resources to certain individuals, teams, or apps. For applications, individuals, or groups of users, namespaces provide basic components for the resource to use allowance, access control, and segregation. You can boost resource efficiency by using Namespaces because a single cluster can now be used for a diverse collection of workloads.

When dealing with Kubernetes, you have two options for listing all the resources associated with a certain namespace: use separate kubectl get commands to list each resource one after the other, or use a single command to display all the resources inside a Kubernetes namespace. We will show you several different approaches to list the resources in a Kubernetes namespace in this tutorial.

Method to show resources in Namespace

Open the command prompt terminal on your Ubuntu 20.04 machine to get all resources in the namespace in Kubernetes. The command-line console can be obtained in the application area or by hitting the shortcut key “Ctrl Alt T.” It is totally up to you whether or not to use either of these approaches. Write down the following minikube instruction as soon as you see the terminal window. To execute the command, tap the “Enter” key.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/1-47.jpg" data-lazy- height="322" src="data:image/svg xml,” width=”734″>

In the command’s output, you view the edition of the minikube cluster installed on your system. If necessary, you can update it. The minikube cluster may take some time to begin.

Method 1

We can list all of the pods, services, stateful sets, and other resources in a namespace by using the kubectl get all command. As a result, you may use this command to see the pods, services, and stateful sets in a specific namespace. Write down the following instruction on the terminal. To execute the instruction, tap the “Enter” key.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/2-45.jpg" data-lazy- height="54" src="data:image/svg xml,” width=”728″>

This command will not display the namespace’s customized resources. As a result, the above command will generate the output displayed in the above-attached image.:

With the command: show all namespaces within-cluster, you may see all of the namespaces in the cluster.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/3-44.jpg" data-lazy- height="136" src="data:image/svg xml,” width=”733″>

This will provide you a list of all the namespaces in the cluster, the default ones, and their status and age. To view some resources inside the above-displayed namespaces, follow the below process. Initially, we don’t have any pods in these namespaces, so that we will create a pod first.

Pods reflect the processes of an application; they are an important unit for grasping the Kubernetes object model. For most cases, pods are used to manage containers in Kubernetes in an indirect manner. In more complicated use cases, pods may include numerous containers sharing resources and serving as the container management hub. Run the following appended command to build a pod using the Nginx image. This will construct an Nginx pod, which will execute the Nginx image from Docker Hub. The pod would be created once you press enter. In the console, you will see pod/Nginx created.

$ kubectl run nginx –image=nginx amespace=kube-node-release

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/4-41.jpg" data-lazy- height="59" src="data:image/svg xml,” width=”733″>

The pod would be created once you press enter. In the console, you will see pod/Nginx created. Now to view resources inside pod, execute the cited below command in the console.

$ kubectl get all –n kube-node-lease

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/5-40.jpg" data-lazy- height="64" src="data:image/svg xml,” width=”734″>

Method 2:

The kubectl API-resources command lists the different resource types in your cluster. As a result, we can use it in conjunction with kubectl get to acquire a list of all instances of each resource type in a Kubernetes namespace. You can use the following command:

$ kubectl api-resources –verbs=list –namespaced -o name | xargs -n 1 kubectl get –show-kind –ignore-not-found -n kube-node-lease

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/6-36.jpg" data-lazy- height="488" src="data:image/svg xml,” width=”736″>

You can run the aforementioned command by replacing with your namespace in the script above. This command can take a long time if there are a lot of resources in a namespace.

Method 3:

To describe the resources, we intend to see in a namespace; we may use the basic kubectl get command. Instead of repeating the kubectl get command for every resource type individually, we may do it once for all of them. If you wanted to retrieve pods for a namespace, for illustration, you would run the following appended command:

$ kubectl get pod -n Kube-node-lease

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/7-35.jpg" data-lazy- height="65" src="data:image/svg xml,” width=”729″>

In the script above, you can also run the aforementioned command by replacing with your desired namespace.

Conclusion

This article provided you with the basic concept of Kubernetes namespaces. Also, three different methods to get all resources in namespaces have been explained in this guide.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content