Kubernetes Namespaces
In Kubernetes, Namespaces provide a logical way to isolate and organize groups of resources within a single cluster. They are especially useful in environments where multiple teams or projects share the same cluster, allowing separation and management of resources independently.
Each resource in a Namespace must have a unique name, but resources across different Namespaces can share the same name without conflict.
Default Kubernetes Namespaces
When you create a Kubernetes cluster, it comes with four built-in Namespaces:
-
default
-
kube-node-lease
-
kube-public
-
kube-system
To view all available Namespaces, use:
You’ll see the four default Namespaces listed.
Bonus: Kubernetes Dashboard Namespace (Minikube Specific)
When you install Minikube, an additional Namespace called kubernetes-dashboard is automatically created.
This Namespace is specific to Minikube and not present in standard Kubernetes clusters.
1. kube-system
The kube-system Namespace contains core system components and processes managed by Kubernetes itself.
It includes system-level Pods like:
-
kube-dns
-
kube-proxy
-
etcd
-
kube-apiserver
This Namespace is not meant for user-created workloads. Developers should avoid creating or modifying resources in kube-system.
2. kube-public
The kube-public Namespace holds publicly accessible cluster information.
It contains a ConfigMap that stores details about the cluster, which can be accessed even without authentication.
To view this information:
This command retrieves data stored in the kube-public Namespace.
3. kube-node-lease
The kube-node-lease Namespace was introduced to improve cluster performance and scalability.
It maintains Node lease objects, which record the heartbeat and availability status of each Node.
Each Node has its own lease object, helping the Kubernetes control plane efficiently detect Node failures.
4. default
The default Namespace is where Kubernetes places all resources if no Namespace is explicitly specified.
When you create a Pod, Service, or ConfigMap without assigning a Namespace, it’s automatically placed in default.
Creating Namespaces
You can create new Namespaces in two ways:
1. Using CLI Commands
Run the following command to create a Namespace named my-ns:
To verify:
You’ll see my-ns listed among the available Namespaces.
2. Using a Configuration File
It’s often better to define Namespaces using a YAML configuration file — this provides version control and traceability within your infrastructure code.
Example:
Apply the file:
Creating Components in the Default Namespace
When no Namespace is specified, Kubernetes automatically creates resources in the default Namespace.
Example ConfigMap:
Apply the ConfigMap:
To verify which Namespace it belongs to:
Or to view detailed YAML output:
You’ll see that the Namespace field is set to default.
Creating Components in a Custom Namespace
You can assign resources to a specific Namespace in two ways:
1. Specifying Namespace via CLI
Add the --namespace flag when applying the resource file:
Make sure the Namespace (my-ns) already exists, or you’ll get an error.
2. Specifying Namespace in the YAML File
Include the Namespace directly in the metadata section:
Apply the file:
Then verify:
The output will confirm that the ConfigMap now belongs to the my-ns Namespace.
Changing the Active Namespace
By default, your active Namespace is default.
To easily switch between Namespaces, use the kubens tool from kubectx.
Install kubectx and kubens
View All Namespaces
The active Namespace will appear highlighted in green.
Switch to Another Namespace
Now, your active Namespace is set to my-ns, meaning any subsequent resource creation (without specifying a Namespace) will default to this Namespace.
Conclusion
Kubernetes Namespaces are essential for managing complex, multi-team, or multi-project environments within a single cluster.
They offer logical separation, resource isolation, and simplified access control — making them a fundamental part of any production-grade Kubernetes architecture.
Comments
Post a Comment