Namespaces

Difficulty: Beginner

Question

What is a Namespace in Kubernetes, why are Namespaces used, and what isolation do they actually provide?

Answer

A Namespace is a virtual partition inside a cluster that scopes object names and lets you apply access control, quotas and policies per group. Teams use them to separate environments (dev, staging), teams (payments, search) or tenants, so two teams can both have a Deployment called api without conflict. On their own, Namespaces are a naming and policy boundary, not a security or network boundary.

What a Namespace scopes

  • Names: object names must be unique within a namespace, not across the cluster.
  • RBAC: a Role and RoleBinding grant permissions only inside one namespace.
  • ResourceQuota: caps total CPU, memory, Pods, PVCs or LoadBalancers a namespace can consume.
  • LimitRange: sets default and maximum requests/limits for containers in the namespace.
  • NetworkPolicy and Pod Security Admission labels are applied per namespace.
  • DNS: Services resolve as <service>.<namespace>.svc.cluster.local, so api.payments and api.search are different.

What is not namespaced

Some resources are cluster-wide: Nodes, PersistentVolumes, StorageClasses, Namespaces themselves, ClusterRoles, ClusterRoleBindings, CustomResourceDefinitions and IngressClasses. Check with kubectl api-resources --namespaced=false.

Default namespaces

NamespacePurpose
defaultWhere objects go if you do not specify one
kube-systemControl plane and system add-ons like CoreDNS
kube-publicReadable by everyone, rarely used
kube-node-leaseNode heartbeat Lease objects

Isolation: what you still need to add

By default, a Pod in dev can reach a Pod in prod over the network, and Pods from different namespaces share the same nodes and kernel. For real isolation you need:

  1. NetworkPolicies with a default-deny rule per namespace (requires a CNI that enforces them, like Calico or Cilium).
  2. RBAC that binds teams only to their own namespaces.
  3. ResourceQuotas so one team cannot starve others.
  4. Pod Security Admission (pod-security.kubernetes.io/enforce: restricted) to block privileged Pods.
  5. For hostile multi-tenancy, separate node pools or separate clusters, since a kernel escape crosses namespace lines.

Practical gotchas

  • Deleting a namespace deletes everything in it. Protect production namespaces in your tooling.
  • A namespace stuck in Terminating usually has a resource with a finalizer whose controller is gone.
  • Many companies keep dev and prod in separate clusters rather than namespaces, because of blast radius and upgrade risk.

Code examples

Namespace with quota, defaults and Pod Security

apiVersion: v1
kind: Namespace
metadata:
  name: payments
  labels:
    pod-security.kubernetes.io/enforce: restricted
---
apiVersion: v1
kind: ResourceQuota
metadata:
  name: payments-quota
  namespace: payments
spec:
  hard:
    requests.cpu: "20"
    requests.memory: 40Gi
    limits.memory: 60Gi
    pods: "100"
    services.loadbalancers: "1"
---
apiVersion: v1
kind: LimitRange
metadata:
  name: defaults
  namespace: payments
spec:
  limits:
    - type: Container
      defaultRequest: { cpu: 100m, memory: 128Mi }
      default: { memory: 256Mi }

The quota caps the team's total usage, the LimitRange fills in defaults for containers that forget them, and the label enforces the restricted Pod Security standard.

Everyday namespace commands

kubectl get namespaces
kubectl get pods -n payments
kubectl get pods -A                     # all namespaces
kubectl config set-context --current --namespace=payments
kubectl api-resources --namespaced=false

Setting the namespace on the current context saves typing -n on every command.

Key points

  • Namespaces scope names, RBAC Roles, quotas, LimitRanges and policies.
  • They do not isolate network traffic or the kernel by default.
  • Nodes, PVs, StorageClasses, ClusterRoles and CRDs are cluster-scoped.
  • Cross-namespace DNS uses service.namespace.svc.cluster.local.
  • Deleting a namespace deletes every object inside it.

Common follow-up questions

  • How would you stop Pods in dev from talking to Pods in prod?
  • Why might a namespace be stuck in Terminating?
  • When would you choose separate clusters instead of namespaces?
  • What is the difference between a ResourceQuota and a LimitRange?

Interview tips

  • Say clearly that namespaces are not a security boundary by themselves, then list what you add to make them one.
  • Know a few cluster-scoped resources; interviewers often ask 'is a PersistentVolume namespaced?'
  • Mention the trade-off between namespace-per-env and cluster-per-env to show architectural judgment.

Concepts covered

Namespace, ResourceQuota, LimitRange, RBAC Scoping, Cluster-scoped Resources

More Kubernetes & Orchestration questions