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:
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:
Where:
- [.inline-code] namespace_name …[.inline-code] is a list of namespace names separated by a space character.
For example:
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] ):
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:
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:
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] ):
Where:
- [.inline-code] output_format[.inline-code] is one of [.inline-code] yaml[.inline-code] or [.inline-code] json[.inline-code] .
For example:
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:
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:
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:
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:
[#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:
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:
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:
Where:[.inline-code] template_file_path[.inline-code] is an absolute or relative file path for a template file.
For example:
Where the [.inline-code] myTemplate.txt[.inline-code] file contains:
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:
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:
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:
Where:
- [.inline-code] namespace_name …[.inline-code] is a list of Namespace names separated by a space indicator.
For example:
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:
[#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:
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] :
And the below command will output the list of all services within the namespace named [.inline-code] my-app-namespace[.inline-code] :