0
0
KubernetesHow-ToBeginner · 3 min read

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 namespace without the plural s sometimes works but is less common; always prefer namespaces or ns.
  • 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

CommandDescription
kubectl get namespacesList all namespaces in the cluster
kubectl get nsShort form to list namespaces
kubectl get namespaces -o wideList namespaces with extra details
kubectl get namespaces -o jsonList 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.