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.
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.
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.
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.
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.
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.
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:
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.
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:
In the script above, you can also run the aforementioned command by replacing with your desired namespace.
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.