0
0
Kubernetesdevops~5 mins

Default namespaces overview in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Kubernetes organizes resources into groups called namespaces. Default namespaces are pre-made groups that help keep things tidy and separate. They solve the problem of managing many resources by giving each a home.
When you want to separate system resources from your application resources to avoid conflicts.
When you need to run multiple projects on the same Kubernetes cluster without mixing their resources.
When you want to quickly deploy an app without creating a new namespace.
When you want to understand where your pods and services are running by default.
When troubleshooting, to know which namespace your resources belong to.
Commands
This command lists all namespaces in the Kubernetes cluster, showing the default ones included.
Terminal
kubectl get namespaces
Expected OutputExpected
NAME STATUS AGE default Active 10d kube-system Active 10d kube-public Active 10d kube-node-lease Active 10d
This command shows all pods running in the default namespace, which is where resources go if no namespace is specified.
Terminal
kubectl get pods -n default
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-app-pod-1234567890 1/1 Running 0 2d
-n - Specifies the namespace to look in
This command creates a pod named my-nginx in the default namespace because no namespace is specified.
Terminal
kubectl run my-nginx --image=nginx --restart=Never
Expected OutputExpected
pod/my-nginx created
--restart=Never - Creates a pod instead of a deployment
This command lists pods in the default namespace by default, showing the pod created in the previous step.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE my-nginx 1/1 Running 0 10s
Key Concept

If you remember nothing else from this pattern, remember: Kubernetes uses the default namespace automatically when you don't specify one, so your resources go there unless you say otherwise.

Common Mistakes
Not specifying the namespace and expecting the resource to be in a custom namespace.
Kubernetes puts resources in the default namespace if no namespace is given, so your resource won't be where you expect.
Always use the -n flag with kubectl commands to specify the namespace if you want to work outside the default.
Trying to find resources without checking the namespace first.
Resources in other namespaces won't show up unless you specify the namespace or use --all-namespaces.
Use kubectl get pods -n your-namespace or kubectl get pods --all-namespaces to find resources.
Summary
The default namespace is where Kubernetes puts resources if no namespace is specified.
Use 'kubectl get namespaces' to see all namespaces including default ones.
Use the -n flag to specify namespaces when managing resources.