Signup/Sign In

How to List all Resources in a Kubernetes Namespace

Posted in Cloud   LAST UPDATED: APRIL 3, 2023

    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.

    list resources in a kubernetes namespace

    List of Resources in 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, and 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:

    In this article, multiple ways to list all resources in a Kubernetes namespace have been discussed. The first method is using the 'kubectl get all' command, which lists down all the pods, services, stateful sets, etc., in a namespace, but not all the resources are listed using this command.

    The second method involves using the 'kubectl api-resources' command along with a function, which makes it more intuitive to use. This function can be used to list down all the resources in a namespace by providing the namespace while calling the function.

    The third method involves using the 'kubectl get' command, which can be used to list down the resources we want to see in a namespace. Rather than running the 'kubectl get' command for each resource kind, we can run it for multiple resources in one go. These methods provide easy ways to list all resources in a Kubernetes namespace, making it simpler to manage and monitor the resources running in a namespace.

    If you face any issues, do share them with us. Best of luck!!.

    Frequently Asked Questions:

    1: What is a Kubernetes namespace?

    A Kubernetes namespace is a virtual cluster within a Kubernetes cluster that provides a way to organise resources and isolate them from other resources in the cluster.

    2: How do I list all resources in a Kubernetes namespace?

    To list all resources in a Kubernetes namespace, you can use the "kubectl get all" command followed by the name of the namespace. For example, to list all resources in the "default" namespace, use the following command:

    kubectl get all --namespace=default

    3: What other options can I use with the "kubectl get all" command?

    Other options you can use with the "kubectl get all" command include "-o wide" to display more details about each resource, and "-l" to filter resources by labels.

    4: Can I list resources in multiple Kubernetes namespaces at once?

    Yes, you can list resources in multiple Kubernetes namespaces by specifying a comma-separated list of namespaces after the "--namespace" flag. For example, to list resources in both the "default" and "kube-system" namespaces, use the following command:

    kubectl get all --namespace=default,kube-system

    You May Also Like

    About the author:
    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    Tags:namespacekubernetes
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS