Kubectl: Get All Resources in Namespace

The kubectl get all command is used to list down such Kubernetes resources as Pods, Services, StatefulSets, etc.

But this command doesn’t really list all the resources in a Namespace.

To really get all the Kubernetes resources in the Namespace we can use the combination of the kubectl api-resources and kubectl get commands.

This short note shows how to get all the resources in the Namespace using the kubectl.

Cool Tip: How to set a default Namespace in a Kubernetes! Read more →

Get All The Resources in a Namespace using Kubectl

Execute the following command to get all the resources in a selected Namespace and list them in a YAML format:

$ kubectl api-resources --namespaced | awk '{print $1}' | sed '1d' |\
          while read -r resource; do \
            echo "$(kubectl get $resource -n <namespace> -o yaml)"; \
          done

This command does the following:

  1. Lists all the resources supported by the Kubernets cluster using the kubectl api-resources command.
  2. Loops through the list of the supported resources.
  3. For each of the resources in the list it executes the kubectl get command in the selected Namespace.

If you want, for example, to backup or clone all the resources in the Namespace, you can get them all and then save to the separate YAML files as follows:

$ mkdir resources
$ cd resources
$ kubectl api-resources --namespaced | awk '{print $1}' | sed '1d' |\
          while read -r resource; do \
            echo "$(kubectl get $resource -n <namespace> -o yaml > $resource.yaml)"; \
          done

Cool Tip: How to kubectl apply all the files in a directory! Read more →

Leave a Reply