Managing Kubernetes Workloads with Namespaces:
In Kubernetes, namespaces provide a way to create virtual clusters within a physical cluster. They help segregate deployments based on teams, resources, or access control. When workloads run in different namespaces, they operate independently, even if they have the same name.
By default, there is no communication between namespaces unless explicitly configured. Resources can be allocated at the namespace level, allowing administrators to set resource quotas and limits for each namespace.
Default Namespaces in Kubernetes
Kubernetes comes with four default namespaces:
- default – The default namespace for objects that are not assigned to a specific namespace.
- kube-system – Used for Kubernetes system components (e.g., the scheduler, controller manager, and DNS).
- kube-public – Readable by everyone in the cluster; used for publicly accessible information.
- kube-node-lease – Holds lease objects associated with each node, improving node heartbeats.
Creating a Custom Namespace
If you need a custom namespace, you can define it in a YAML file:
apiVersion: v1
kind: Namespace
metadata:
name: my-namespace
Apply the namespace using:
kubectl apply -f namespace.yaml
Example: Deploying a Pod in a Specific Namespace
To deploy a pod inside a custom namespace, specify the namespace
field in your YAML file:
apiVersion: v1 kind: Pod metadata: name: my-pod namespace: my-namespace spec: containers: - name: nginx-container image: nginx
Apply the deployment:kubectl apply -f pod.yaml
Viewing and Managing Namespaces
List all namespaces:
kubectl get namespaces
Get resources within a specific namespace:
kubectl get pods -n my-namespace
Delete a namespace:
kubectl delete namespace my-namespace
Conclusion
Namespaces are essential for organizing and isolating workloads in Kubernetes. They enable better resource management, security, and multi-tenancy within a cluster. By using namespaces effectively, teams can ensure their deployments remain structured and efficient.