1. Introduction

Running kubectl get all to retrieve a list of all resources in a namespace only does so much. While it returns a list of various resources in a namespace, it omits some resource types. Of course, this is undesirable when we really need an exhaustive list of all resources in the namespace.

In this tutorial, we’ll learn how to list all resources in a namespace in Kubernetes.

2. Using kubectl get all

As we mentioned earlier, we can get a list of some resources in a namespace when we run kubectl get all:

$  kubectl get all
NAME                                  READY   STATUS                       RESTARTS   AGE
pod/app-deployment-5dc467b756-4zwcp   0/1     CreateContainerConfigError   0          5d19h
pod/app-deployment-5dc467b756-qplj6   0/1     CreateContainerConfigError   0          5d19h

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   5d22h

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/app-deployment   0/2     2            0           5d19h

NAME                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/app-deployment-5dc467b756   2         2         0       5d19h

However, some resource types, such as config map, would be missing from the returned list.

Wherever applicable, kubectl get all returns a list of pods, services, daemon sets, deployments, replica sets, jobs, cronjobs, and stateful sets. But it doesn’t display resources like config maps, secrets, persistent volumes, persistent volume claims, ingresses, service accounts, amongst others.

3. Using kubectl api-resources 

When we want a more encompassing list of all resources in a namespace, we can combine the kubectl api-resources command with kubectl-get:

$ kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --ignore-not-found --show-kind -n <namespace>

Let’s try the command on the default namespace of our cluster:

$ kubectl api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --ignore-not-found --show-kind
NAME                         DATA   AGE
configmap/db-config          1      137m
...truncated..
NAME                   ENDPOINTS           AGE
endpoints/kubernetes   192.168.49.2:8443   6d1h
LAST SEEN   TYPE     REASON      OBJECT                                MESSAGE
76s         Normal   Pulling     pod/app-deployment-5dc467b756-4zwcp   Pulling image "adeyomola/app:latest"
...truncated...
NAME                              STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/www-web-0   Bound    pvc-43444fce-40e5-4b85-9336-95776ffe332e   1Gi        RWO            standard       139m
...truncated...
NAME                                  READY   STATUS                       RESTARTS   AGE
pod/app-deployment-5dc467b756-4zwcp   0/1     CreateContainerConfigError   0          5d22h
...truncated...
NAME                     SECRETS   AGE
serviceaccount/default   0         10d
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   6d1h
NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/app-deployment   0/2     2            0           5d22h
NAME                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/app-deployment-5dc467b756   2         2         0       5d22h
...truncated...
NAME                                        CLASS           HOSTS   ADDRESS   PORTS   AGE
ingress.networking.k8s.io/minimal-ingress   nginx-example   *                 80      44m

From the output, we can see that ingresses, endpoints, service accounts, persistent volume claims, and config maps are amongst the returned resources. This is unlike the restrictive list from kubectl get all.

3.1. Understanding the kubectl api-resources Command

kubectl api-resources –verbs=list –namespaced -o name retrieves all namespaced API resource types that support the list API verb. Then it outputs their names. Those names are then redirected to xargs as standard input.

xargs -n 1 singly passes each of those names as initial arguments to kubectl get –ignore-not-found –show-kind -n <namespace>. Then, the kubectl get command returns a list of resources belonging to each resource type in the specified namespace.

The –ignore-not-found flag of kubectl get suppresses output if an instance of a requested resource type doesn’t exist in the namespace. In other words, the flag suppresses “No resources found in <namespace> namespace.” messagesThen, the –show-kind flag ensures that the output specifies the resource type of every resource.

The -n 1 arguments in the xargs command ensures that only one name from the kubectl api-resources output is used per iteration of the kubectl get command.

4. Using kubectl api-resources and for Loop

In place of xargs -n 1, we can pass the name of each resource type returned by kubectl api-resources –verbs=list –namespaced -o name to kubectl get –ignore-not-found –show-kind -n <namespace> using a for loop:

$ for i in $(kubectl api-resources --namespaced --verbs=list -o name | tr "\n" " ");
do
kubectl get $i --show-kind --ignore-not-found;
done
NAME                         DATA   AGE
configmap/db-config          1      4h16m
...truncated...
NAME                   ENDPOINTS           AGE
endpoints/kubernetes   192.168.49.2:8443   6d3h
...truncated..
NAME                                        CLASS           HOSTS   ADDRESS   PORTS   AGE
ingress.networking.k8s.io/minimal-ingress   nginx-example   *                 80      164m

We get pretty much the same output as when we used kubectl api-resources and kubectl get with xargs.

5. Retrieving a List of Multiple Resource Types

Sometimes, we’re not really interested in a list of all resources in a namespace. However, we want a list of all resources of specific resource types in a namespace. At such times, combining kubectl get and kubectl api-resources would be an overkill since the combination is somewhat non-specific.

Adding multiple, comma-separated arguments to kubectl get allows us to list all resources of specific resource types in a namespace:

$ kubectl get <resource type>,<resource type>,<resource type>,... -n <namespace>

Let’s retrieve a list of config maps, persistent volumes, endpoints, ingress, and service accounts in the kube-system namespace:

$ kubectl get configmaps,pv,endpoints,ingress,serviceaccounts -n kube-system
NAME                                           DATA   AGE
configmap/coredns                              1      10d
...truncated...

NAME                                                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM               STORAGECLASS   REASON   AGE
persistentvolume/pvc-43444fce-40e5-4b85-9336-95776ffe332e   1Gi        RWO            Delete           Bound    default/www-web-0   standard                4h36m
persistentvolume/pvc-a6f90403-b427-4483-a6a4-7cf07d2d7852   1Gi        RWO            Delete           Bound    default/www-web-1   standard                4h36m

NAME                                 ENDPOINTS                                           AGE
endpoints/k8s.io-minikube-hostpath   <none>                                              10d
endpoints/kube-dns                   10.244.0.161:53,10.244.0.161:53,10.244.0.161:9153   6d4h

NAME                                                SECRETS   AGE
serviceaccount/attachdetach-controller              0         10d
serviceaccount/bootstrap-signer                     0         10d
...truncated...

6. Conclusion

In this article, we discussed how to list all resources in namespaces of a Kubernetes cluster. We also saw how to list resources of multiple (specific) resource types in a namespace.

Many times, running kubectl get all might be enough to show us a list of all resources we need to see in a namespace. Most of the resources it returns are the ones we’d commonly work with. However, whenever we need comprehensiveness, we’ll have to redirect names of API resource types from kubectl api-resources to kubectl get.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.