What is a namespace in Kubernetes, and how is it used?
BeginnerConceptContainers & Kubernetes basics
Answer
In one line: A namespace is a virtual partition inside a Kubernetes cluster that groups resources and separates teams, apps or environments, so they can share one cluster without getting in each other's way.
What namespaces give you:
- Organisation — group resources:
payments,orders,monitoring. - Name scoping — two teams can both have a Service called
apiin different namespaces. - Access control — RBAC Roles and RoleBindings apply per namespace (the payments team can only manage the
paymentsnamespace). - Resource quotas — limit total CPU, memory and object counts per namespace with
ResourceQuota, and set defaults withLimitRange. - Network isolation —
NetworkPolicyrules can block traffic between namespaces.
Default namespaces:
default— where things go if you don't specify a namespace (avoid using it for real apps).kube-system— Kubernetes system components (CoreDNS, kube-proxy).kube-publicandkube-node-lease.
Commands:
kubectl create namespace payments
kubectl get pods -n payments
kubectl config set-context --current --namespace=payments
kubectl get pods -A # all namespaces
Service DNS across namespaces: orders.payments.svc.cluster.local (or simply orders.payments).
Common patterns:
- Per team or per app namespaces in a shared cluster.
- Per environment namespaces (
dev,qa) for non-production — but production is usually in a separate cluster for stronger isolation. - Per pull request namespaces for preview environments.
Limits: namespaces are not a hard security boundary. Nodes, cluster-wide resources (like CRDs and ClusterRoles) and the kernel are shared. For strong isolation, use separate clusters or extra tools.
Tip for interviews: mention RBAC, quotas and NetworkPolicies per namespace, and that production usually gets its own cluster.
More on Containers & Kubernetes basics
What is the difference between a Docker image and a container?BeginnerWhat makes a good Dockerfile? Give some best practices.BeginnerWhat is Kubernetes, and why do we need it?BeginnerExplain the Kubernetes architecture in simple terms.BeginnerWhat are Pods, Deployments and Services in Kubernetes?BeginnerWhat are liveness, readiness and startup probes?Beginner