Difficulty: Beginner
What is a Namespace in Kubernetes, why are Namespaces used, and what isolation do they actually provide?
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.
Role and RoleBinding grant permissions only inside one namespace.<service>.<namespace>.svc.cluster.local, so api.payments and api.search are different.Some resources are cluster-wide: Nodes, PersistentVolumes, StorageClasses, Namespaces themselves, ClusterRoles, ClusterRoleBindings, CustomResourceDefinitions and IngressClasses. Check with kubectl api-resources --namespaced=false.
| Namespace | Purpose |
|---|---|
default | Where objects go if you do not specify one |
kube-system | Control plane and system add-ons like CoreDNS |
kube-public | Readable by everyone, rarely used |
kube-node-lease | Node heartbeat Lease objects |
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:
pod-security.kubernetes.io/enforce: restricted) to block privileged Pods.Terminating usually has a resource with a finalizer whose controller is gone.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.
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.
Namespace, ResourceQuota, LimitRange, RBAC Scoping, Cluster-scoped Resources