Kubernetes Namespaces
Namespaces are a way to divide a Kubernetes cluster into virtual sub‑clusters. They help organize resources, especially when multiple teams or projects use the same cluster.
What Are Namespaces?
A namespace is a scope for names. Resources in different namespaces can have the same name without conflict. For example, you can have a
database service in both dev and prod namespaces.Namespaces provide isolation and organization for cluster resources.
Default Namespaces
When you start a cluster, Kubernetes creates several default namespaces:
- default: The default namespace for resources with no explicit namespace.
- kube-system: For system resources (e.g., kube-dns, metrics server).
- kube-public: Readable by all users, for public resources.
- kube-node-lease: Holds node lease objects for heartbeat monitoring.
kubectl get namespacesCreating a Namespace
Create a namespace with
kubectl create namespace my-namespace. Or using YAML:apiVersion: v1
kind: Namespace
metadata:
name: my-namespaceApply with kubectl apply -f namespace.yaml.Working with Namespaces
To create a pod in a specific namespace, use
--namespace flag:kubectl run nginx --image=nginx --namespace=my-namespaceTo list resources in a namespace:kubectl get pods --namespace=my-namespaceYou can set a default namespace for all kubectl commands with:kubectl config set-context --current --namespace=my-namespaceDeleting a Namespace
Deleting a namespace removes all resources inside it:
kubectl delete namespace my-namespaceTwo Minute Drill
- Namespaces isolate resources within a cluster.
- Default namespaces:
default,kube-system,kube-public. - Create namespace:
kubectl create namespace name. - Specify namespace with
--namespaceor set default withkubectl config set-context. - Delete namespace:
kubectl delete namespace name.
Need more clarification?
Drop us an email at career@quipoinfotech.com
