Terminus
List Kubernetes Namespaces With kubectl

List Kubernetes Namespaces With kubectl

In Kubernetes, namespaces provide a logical way to separate resources within an application, forming isolated virtual clusters within the Kubernetes cluster. For example, you can create namespaces for the [.inline-code] development[.inline-code]  and [.inline-code] production[.inline-code]  environments and manage their resources independently.

The short answer

To list all the namespaces in your Kubernetes cluster at once, you can use the [.inline-code] kubectl get namespaces[.inline-code]  command:

 $ kubectl get namespaces

This command provides details for every Namespace within your cluster including their status and age.

Where:

  • [.inline-code] NAME[.inline-code]  is the unique name of the namespace.
  • [.inline-code] STATUS[.inline-code]  is the current state of the namespace such as [.inline-code] Active[.inline-code] , [.inline-code] NotFound[.inline-code] , etc.
  • [.inline-code] AGE[.inline-code]  is the time when the namespace was created.

If you want to learn more about namespaces, you can read our article on how to create a namespace in Kubernetes with kubectl.

[#easily-recall-with-ai] Easily retrieve this command using Warp’s AI Command Suggestions [#easily-recall-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code] k8s list namespaces[.inline-code]  (Kubernetes is abbreviated as "k8s") in the AI Command Suggestions will prompt a [.inline-code] kubectl[.inline-code]  command that can then quickly be inserted into your shell by doing [.inline-code] CMD+ENTER[.inline-code] .

[#list-namespaces-by-name] Listing namespaces by name [#list-namespaces-by-name]

To list one or more namespaces by name in your Kubernetes cluster, you can use the [.inline-code] kubectl get namespaces[.inline-code]  command as follows:

 $ kubectl get namespaces <namespace_name …>

Where:

  • [.inline-code] namespace_name …[.inline-code]  is a list of namespace names separated by a space character.

For example:

 $ kubectl get namespaces namespace1 namespace2

Upon execution, the above command will output a table of information about the Namespaces named [.inline-code] namespace1[.inline-code]  and [.inline-code] namespace2[.inline-code] , including their name, status, and age. 

Note that, if you specify a namespace name that does not exist, the command will output an error indicating that the specified namespace was not found. 

[#list-namespaces-by-label] Listing namespaces by label [#list-namespaces-by-label]

Labels are key-value pairs attached to the Kubernetes objects that organize resources based on specific criteria. By default, Kubernetes sets a default label for all namespaces [.inline-code] kubernetes.io/metadata.name[.inline-code]  with the value of the namespace name.

To list namespaces based on a specific label, you can use the [.inline-code] kubectl get namespaces[.inline-code]  command with the [.inline-code] -l[.inline-code]  flag (short for [.inline-code] --label[.inline-code] ):

 $ kubectl get namespaces -l <label>=<value>

Where:

  • [.inline-code] label[.inline-code]  is the key of the label. 
  • [.inline-code] value[.inline-code]  is the value associated with the label. 

For example:

 $ kubectl get namespaces -l app=myapp

Upon execution, the above command will display all namespaces with the label [.inline-code] app=myapp[.inline-code]  assigned to them. 

[#list-labels-of-namespaces] Displaying the labels of all namespaces [#list-labels-of-namespaces]

To view the labels associated with all the namespaces at once, you can use the [.inline-code] kubectl get namespaces[.inline-code]  command with the [.inline-code] --show-label[.inline-code]  flag:

 $ kubectl get namespaces --show-labels

Upon execution, the above command will output an additional column showing any labels associated with namespaces.

[#list-namespaces-in-yaml-or-json] Listing namespaces in the YAML or JSON formats [#list-namespaces-in-yaml-or-json]

By default, the output format of the [.inline-code] kubectl get namespaces[.inline-code]  command is a table. However, you can specify other formats, such as YAML or JSON using the [.inline-code] -o[.inline-code]  flag (short for [.inline-code] --output[.inline-code] ):

 $ kubectl get namespaces -o <output_format>

Where:

  • [.inline-code] output_format[.inline-code]  is one of [.inline-code] yaml[.inline-code]  or [.inline-code] json[.inline-code] . 

For example:

 $ kubectl get namespaces -o yaml

Upon execution, the above command will display comprehensive details about all the namespaces in the specified YAML format.

[#filter-namespaces-by-field-selector] Filtering namespaces using a field selector [#filter-namespaces-by-field-selector]

To filter the list of namespaces based on a specific field, you can use the [.inline-code] kubectl get namespaces[.inline-code]  command with the [.inline-code] --field-selector[.inline-code]  flag:

 $ kubectl get namespaces --field-selector=<field_name>=<field_value>

Where:

  • [.inline-code] field_name[.inline-code]  is a JSONPath expression used for selecting a specific field.
  • [.inline-code] field_value[.inline-code]  is the value for the specified field.

For example, this command will filter and display the list of all active namespaces:

 $ kubectl get namespaces --field-selector=status.phase=Active

You can learn more about JSONPath expressions by visiting the official Kubernetes documentation page.

[#sort-the-namespace-list] Sorting the output of the [.inline-code] kubectl get namespaces[.inline-code]  command[#sort-the-namespace-list]

To sort the output of the [.inline-code] kubectl get namespaces[.inline-code]  command based on a specific field, you can use the [.inline-code] kubectl get namespaces[.inline-code]  command with the [.inline-code] --sort-by[.inline-code]  flag:

 $ kubectl get namespaces --sort-by=<expression>

Where:

  • [.inline-code] expression[.inline-code]  is a JSONPath expression.

For example, this command will display the list of all namespaces sorted by their names in ascending order:

 $ kubectl get namespaces --sort-by=.metadata.name

[#customize-the-namespace-list] Customizing the output of the [.inline-code] kubectl get namespaces[.inline-code]  command [#customize-the-namespace-list]

To customize the output columns of the [.inline-code] kubectl get namespaces[.inline-code]  command, you can use the [.inline-code] kubectl get namespaces[.inline-code]  command with the [.inline-code] -o custom-columns[.inline-code] flag:

 $ kubectl get namespaces -o custom-columns=<custom_column_name>:<expression>

Where:

  • [.inline-code] custom_column_name[.inline-code]  is the name you want to assign to a column.
  • [.inline-code] expression[.inline-code]  is a JSONPath expression.

For example:

 $ kubectl get namespaces -o custom-columns='NAME:.metadata.name,SPEC.FINALIZER:.spec.finalizers[0]'

Upon execution, the above command will output two columns, [.inline-code] NAME[.inline-code]  populated with the values of [.inline-code] metadata.name[.inline-code]  and [.inline-code] SPEC.FINALIZER[.inline-code]  populated with the first values of the [.inline-code] spec.finalizers[.inline-code] . 

Customizing the output using a template file

To customize the output columns of the [.inline-code] kubectl get namespaces[.inline-code]  command using a template file (i.e. a predefined column configuration), you can use the [.inline-code] -o custom-column-file[.inline-code]  flag:

 $ kubectl get namespaces -o custom-columns-file=<template_file_path>

Where:[.inline-code] template_file_path[.inline-code]  is an absolute or relative file path for a template file.

For example:

 $ kubectl get namespaces -o custom-columns-file=./myTemplate.txt

Where the [.inline-code] myTemplate.txt[.inline-code]  file contains:

 NAME                 SPEC.FINALIZER
metadata.name  spec.finalizers[0]

Upon execution, the above command will output two columns [.inline-code] NAME[.inline-code]  populated with the values of [.inline-code] metadata.name[.inline-code]  and [.inline-code] SPEC.FINALIZER[.inline-code]  populated with the first values of the [.inline-code] spec.finalizers[.inline-code] . 

[#extract-namespace-information] Extracting namespace information [#extract-namespace-information]

To output specific field values of namespaces, you can use the [.inline-code] kubectl get namespaces[.inline-code]  command with the [.inline-code] -o[.inline-code]  flag combined with a JSONPath expression as follows:

 $ kubectl get namespaces -o jsonpath="<expression>"

Where:

  • [.inline-code] expression[.inline-code]  is a JSONPath expression

For example, this command will extract the key-value pairs of each label assigned to the Namespace:

 $ kubectl get namespaces -o=jsonpath="{.items[*].metadata.labels}"

Where:

  • [.inline-code] .items[*][.inline-code]  indicates to iterate over each Namespace.
  • [.inline-code] .metadata[.inline-code]  specifies the Namespace metadata.
  • [.inline-code] labels[.inline-code]  retrieves the label name.

[#describe-namespaces] Describe namespaces with additional information [#describe-namespaces]

To display additional information about the namespaces, you can use the [.inline-code] kubectl describe namespaces[.inline-code]  command as follows:

 $ kubectl describe namespaces <namespace_name …>

Where:

  • [.inline-code] namespace_name …[.inline-code]  is a list of Namespace names separated by a space indicator.

For example:

 $ kubectl describe namespaces my-app-namespace my-db-namespace

Upon execution, the above command will output details about the Namespaces [.inline-code] my-app-namespace[.inline-code]  and [.inline-code] my-db-namespace[.inline-code] , such as associated labels, annotations, the status of the namespace, and more. 

To output details about all namespaces in the cluster, execute the following command without specifying namespace names:

 $ kubectl describe namespaces

[#list-resources-by-namespaces] Listing pods, services or nodes by a namespace [#list-resources-by-namespaces]

To list Kubernetes resources (such as pods, nodes, services and more) by a specified namespace, you can use the [.inline-code] kubectl get[.inline-code]  command with the [.inline-code] -n[.inline-code]  flag (short for [.inline-code] --namespace[.inline-code] ) as follows:

 $ kubectl get <resource> -n <namespace>

Where:

  • [.inline-code] resource[.inline-code]  is the name of a Kubernetes resource such as pods, nodes, services, etc. 
  • [.inline-code] namespace[.inline-code]  is the name of a specific Namespace.

For example, the below command will output the list of all pods within the namespace named [.inline-code] my-app-namespace[.inline-code] :

 $ kubectl get pods -n my-app-namespace

And the below command will output the list of all services within the namespace named [.inline-code] my-app-namespace[.inline-code] :

 $ kubectl get services -n my-app-namespace