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 development and production environments and manage their resources independently.
The short answer
To list all the namespaces in your Kubernetes cluster at once, you can use the kubectl get namespaces command:
$ kubectl get namespaces
Run in Warp
This command provides details for every Namespace within your cluster including their status and age.
Where:
- NAME is the unique name of the namespace.
- STATUS is the current state of the namespace such as Active , NotFound , etc.
- AGE 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 retrieve this command using Warp’s AI Command Suggestions
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering k8s list namespaces (Kubernetes is abbreviated as "k8s") in the AI Command Suggestions will prompt a kubectl command that can then quickly be inserted into your shell by doing CMD+ENTER .
Listing namespaces by name
To list one or more namespaces by name in your Kubernetes cluster, you can use the kubectl get namespaces command as follows:
$ kubectl get namespaces <namespace_name …>
Run in Warp
Where:
- namespace_name … is a list of namespace names separated by a space character.
For example:
$ kubectl get namespaces namespace1 namespace2
Run in Warp
Upon execution, the above command will output a table of information about the Namespaces named namespace1 and namespace2 , 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.
Listing 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 kubernetes.io/metadata.name with the value of the namespace name.
To list namespaces based on a specific label, you can use the kubectl get namespaces command with the -l flag (short for --label ):
$ kubectl get namespaces -l <label>=<value>
Run in Warp
Where:
- label is the key of the label.
- value is the value associated with the label.
For example:
$ kubectl get namespaces -l app=myapp
Run in Warp
Upon execution, the above command will display all namespaces with the label app=myapp assigned to them.
Displaying the labels of all namespaces
To view the labels associated with all the namespaces at once, you can use the kubectl get namespaces command with the --show-label flag:
$ kubectl get namespaces --show-labels
Run in Warp
Upon execution, the above command will output an additional column showing any labels associated with namespaces.
Listing namespaces in the YAML or JSON formats
By default, the output format of the kubectl get namespaces command is a table. However, you can specify other formats, such as YAML or JSON using the -o flag (short for --output ):
$ kubectl get namespaces -o <output_format>
Run in Warp
Where:
- output_format is one of yaml or json .
For example:
$ kubectl get namespaces -o yaml
Run in Warp
Upon execution, the above command will display comprehensive details about all the namespaces in the specified YAML format.
Filtering namespaces using a field selector
To filter the list of namespaces based on a specific field, you can use the kubectl get namespaces command with the --field-selector flag:
$ kubectl get namespaces --field-selector=<field_name>=<field_value>
Run in Warp
Where:
- field_name is a JSONPath expression used for selecting a specific field.
- field_value 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
Run in Warp
You can learn more about JSONPath expressions by visiting the official Kubernetes documentation page.
Sorting the output of the kubectl get namespaces command
To sort the output of the kubectl get namespaces command based on a specific field, you can use the kubectl get namespaces command with the --sort-by flag:
$ kubectl get namespaces --sort-by=<expression>
Run in Warp
Where:
- expression 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
Run in Warp
Customizing the output of the kubectl get namespaces command
To customize the output columns of the kubectl get namespaces command, you can use the kubectl get namespaces command with the -o custom-columns flag:
$ kubectl get namespaces -o custom-columns=<custom_column_name>:<expression>
Run in Warp
Where:
- custom_column_name is the name you want to assign to a column.
- expression is a JSONPath expression.
For example:
$ kubectl get namespaces -o custom-columns='NAME:.metadata.name,SPEC.FINALIZER:.spec.finalizers[0]'
Run in Warp
Upon execution, the above command will output two columns, NAME populated with the values of metadata.name and SPEC.FINALIZER populated with the first values of the spec.finalizers .
Customizing the output using a template file
To customize the output columns of the kubectl get namespaces command using a template file (i.e. a predefined column configuration), you can use the -o custom-column-file flag:
$ kubectl get namespaces -o custom-columns-file=<template_file_path>
Run in Warp
Where: template_file_path is an absolute or relative file path for a template file.
For example:
$ kubectl get namespaces -o custom-columns-file=./myTemplate.txt
Run in Warp
Where the myTemplate.txt file contains:
NAME SPEC.FINALIZER
metadata.name spec.finalizers[0]
Run in Warp
Upon execution, the above command will output two columns NAME populated with the values of metadata.name and SPEC.FINALIZER populated with the first values of the spec.finalizers .
Extracting namespace information
To output specific field values of namespaces, you can use the kubectl get namespaces command with the -o flag combined with a JSONPath expression as follows:
$ kubectl get namespaces -o jsonpath="<expression>"
Run in Warp
Where:
- expression 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}"
Run in Warp
Where:
- .items[*] indicates to iterate over each Namespace.
- .metadata specifies the Namespace metadata.
- labels retrieves the label name.
Describe namespaces with additional information
To display additional information about the namespaces, you can use the kubectl describe namespaces command as follows:
$ kubectl describe namespaces <namespace_name …>
Run in Warp
Where:
- namespace_name … is a list of Namespace names separated by a space indicator.
For example:
$ kubectl describe namespaces my-app-namespace my-db-namespace
Run in Warp
Upon execution, the above command will output details about the Namespaces my-app-namespace and my-db-namespace , 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
Run in Warp
Listing pods, services or nodes by a namespace
To list Kubernetes resources (such as pods, nodes, services and more) by a specified namespace, you can use the kubectl get command with the -n flag (short for --namespace ) as follows:
$ kubectl get <resource> -n <namespace>
Run in Warp
Where:
- resource is the name of a Kubernetes resource such as pods, nodes, services, etc.
- namespace is the name of a specific Namespace.
For example, the below command will output the list of all pods within the namespace named my-app-namespace :
$ kubectl get pods -n my-app-namespace
Run in Warp
And the below command will output the list of all services within the namespace named my-app-namespace :
$ kubectl get services -n my-app-namespace
Run in Warp
Written by
Mansi Manhas
Filed Under
Related Articles
Copy Files From Pod in Kubernetes
Learn how to copy files and directories from within a Kubernetes Pod into the local filesystem using the kubectl command.
Scale Deployments in Kubernetes
Learn how to manually and automatically scale a Deployment based on CPU usage in Kubernetes using the kubectl-scale and kubectl-autoscale commands.
Get Kubernetes Logs With kubectl
Learn how to get the logs of pods, containers, deployments, and services in Kubernetes using the kubectl command. Troubleshoot a cluster stuck in CrashloopBackoff, ImagePullBackoff, or Pending error states.
Forward Ports In Kubernetes
Learn how to forward the ports of Kubernetes resources such as Pods and Services using the kubectl port-forward command.
Tail Logs In Kubernetes
Learn how to tail and monitor Kubernetes logs efficiently to debug, trace, and troubleshoot errors more easily using the kubectl command.
Get Context In Kubernetes
Learn how to get information about one or more contexts in Kubernetes using the kubectl command.
Delete Kubernetes Namespaces With kubectl
Learn how to delete one or more namespaces and their related resources in a Kubernetes cluster using the kubectl command.
Get Kubernetes Secrets With kubectl
Learn how to list, describe, customize, sort and filter secrets in a Kubernetes cluster by name, type, namespace, label and more using the kubectl command.
How To List Events With kubectl
Learn how to list and filter events in Kubernetes cluster by namespace, pod name and more using the kubectl command.
Kubernetes vs Docker: The Backbone of Modern Backend Technologies
Lean the fundamentals of the Kubernetes and Docker technologies and how they interplay with each other.
Set Context With kubectl
Learn how to create, modify, switch, and delete a context in Kubernetes using the kubectl config command.
List Pods With kubectl
Learn how to list and filter Kubernetes Pods by name, namespaces, labels, manifests, and more using the kubectl command.