When you are working with Kubernetes, and want to list down all the resources(Kubernetes objects) associated to a specific namespace, you can either use individual kubectl get command to list down each resource one by one, or you can list down all the resources in a Kubernetes namespace by running a single command.
In this article we will show you multiple different ways to list all resources in a Kubernetes namespace.
1. Using kubectl get all
Using the kubectl get all command we can list down all the pods, services, statefulsets, etc. in a namespace but not all the resources are listed using this command. Hence, if you want to see the pods, services and statefulsets in a particular namespace then you can use this command.
kubectl get all -n studytonight
In the above command studytonight is the namespace for which we want to list down these resources.
The above command will get the following resources running in your namespace, prefixed with the type of resource:
-
pod
-
service
-
daemonset
-
deployment
-
replicaset
-
statefulset
-
job
-
cronjobs
This command will not show the custom resources running in the namespace.
So you will see an output like this for the above command:
NAME READY STATUS RESTARTS AGE
pod/nginx-59cbfd695c-5v5f8 1/1 Running 4 19h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx ClusterIP 182.41.44.514 <none> 80/TCP 5d18h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment/nginx 1/1 1 1 19h
2. Using kubectl api-resources
The kubectl api-resources enumerates the resource types available in your cluster. So we can use it by combining it with kubectl get to list every instance of every resource type in a Kubernetes namespace.
Here is the command you can use:
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n 1 kubectl get --show-kind --ignore-not-found -n <namespace>
In the code above, provide your namespace in place of <namespace> and can run the above command. For too many resources present in a namespace, this command can take some time.
We can use the above command, but a better variant of that would be something I found on Stackoverflow, where the above code has been converted into a function, which makes it more intuitive to use.
function kubectlgetall {
for i in $(kubectl api-resources --verbs=list --namespaced -o name | grep -v "events.events.k8s.io" | grep -v "events" | sort | uniq); do
echo "Resource:" $i
kubectl -n ${1} get --ignore-not-found ${i}
done
}
All we have to do is provide the namespace while calling the above function. To use the above function, copy the complete code and paste it into the Linux terminal, hit Enter.
Then you can call the function:
kubectlgetall studytonight
To list down all the resources in the studytonight namespace. This function will be available for use in the current session only, once you logout of the machine, this change will be lost and you will have to again define the function first and then use it in the next session.
3. Using kubectl get
We can also use the simple kubectl get command to list down the resources we want to see in a namespace. Rather than running kubectl get command for each resource kind, we can run it for multiple resources in one go.
For example, if you want to get pods, services, and deployments for a namespace, then you would run the following three commands:
kubectl get service -n studytonight
kubectl get pod -n studytonight
kubectl get deployment -n studytonight
Well you can combine these three commands into a single command too,
kubectl get service, pod, deployment -n studytonight
Yes, this will work. Here studytonight is the name of the namespace, which you can change and provide your namespace.
Conclusion:
So now you know 3 different ways to list down all the resources in a Kubernetes namespace. Personally, I like the second approach where I use the function, because it becomes super easy to use it if you have to frequently see the resources.
If you face any issue, do share it with us in the comment section below.



