Kubernetes namespaces provide a way to divide cluster resources between multiple users, teams, or applications. By isolating different environments (like dev, test, and prod) within the same cluster, namespaces help in organizing and managing resources efficiently.
By default, Kubernetes comes with four namespaces: default
,kube-public
, kube-system
and kube-node-lease
Understanding Kubernetes Namespaces
When you first deploy a Kubernetes cluster, it comes with four default namespaces:
default: The default namespace for resources with no other specified namespace.
kube-public: Used for resources that should be publicly accessible across the cluster. Typically contains cluster-wide information.
kube-system: Reserved for Kubernetes system components, such as the API server, scheduler, and other core processes.
kube-node-lease: Used for managing node lease objects, which help in determining the availability of nodes in the cluster.
These namespaces allow you to categorize and manage resources efficiently, especially as your cluster grows in complexity.
Creating a Kubernetes Namespace
Creating a namespace in Kubernetes is a straight forward process. You can use the kubectl
command-line tool or a YAML file.
1. Verifying Existing Namespaces
Before creating a new namespace, it's helpful to verify the namespaces already running in your cluster:
kubectl get namespaces
2. Creating a Namespace Using kubectl create
Command (Imperative Approach)
The simplest way to create a namespace is by using the kubectl create
command:
kubectl create namespace <namespace-name>
For example, to create a namespace called dev
:
kubectl create namespace dev
You can verify the creation by listing all namespaces:
kubectl get namespaces
3. Creating a Namespace Using a YAML Configuration (Declarative Approach)
Create a YAML file named namespaces.yaml
:
apiVersion: v1
kind: Namespace
metadata:
name: test
Apply the YAML file to create the namespace:
kubectl apply -f namespaces.yaml
This declarative method is ideal for maintaining version control and automating your Kubernetes infrastructure.
Verifying the Namespace
After creating a namespace, you can verify its existence with:
kubectl get namespaces
This command will list all namespaces in your cluster, including the newly created one.
Deleting a Kubernetes Namespace
Deleting a namespace is just as simple as creating one, but you need to be cautious. When you delete a namespace, all resources (like pods, services, and deployments) within that namespace are also deleted.
To delete a namespace, use the following command:
kubectl delete namespace <namespace-name>
For example, to delete the dev
namespace:
kubectl delete namespace dev
This will remove the namespace and all its resources from your cluster.
Conclusion
Namespaces are a powerful feature in Kubernetes that allows for efficient resource management and isolation within a cluster. By understanding how to create and delete namespaces, you can better organize your workloads and maintain a clean and manageable cluster environment.