How to List Namespaces in Kubernetes: Simple kubectl Commands
To list all namespaces in Kubernetes, use the
kubectl get namespaces command. This shows all namespaces currently available in your cluster.Syntax
The basic syntax to list namespaces is:
kubectl get namespaces- Lists all namespaces in the cluster.kubectl get ns- Short form of the command.
You can also add flags like -o wide for more details or -o json for JSON output.
bash
kubectl get namespaces
Example
This example shows how to list all namespaces in your Kubernetes cluster using the short form command.
bash
kubectl get ns
Output
NAME STATUS AGE
default Active 10d
kube-system Active 10d
kube-public Active 10d
my-namespace Active 5d
Common Pitfalls
Common mistakes when listing namespaces include:
- Using
kubectl get namespacewithout the pluralssometimes works but is less common; always prefernamespacesorns. - Not having the correct kubeconfig or permissions can cause errors or empty results.
- Expecting namespaces to show resources inside them; this command only lists namespaces, not their contents.
bash
kubectl get namespace # Better: kubectl get namespaces
Quick Reference
| Command | Description |
|---|---|
| kubectl get namespaces | List all namespaces in the cluster |
| kubectl get ns | Short form to list namespaces |
| kubectl get namespaces -o wide | List namespaces with extra details |
| kubectl get namespaces -o json | List namespaces in JSON format |
Key Takeaways
Use
kubectl get namespaces or kubectl get ns to list all namespaces.Ensure your kubeconfig and permissions allow viewing namespaces.
The command lists namespaces only, not the resources inside them.
Use output flags like
-o wide or -o json for more details or machine-readable output.