Namespaces are how a single cluster serves many teams, environments, and apps without everything colliding. Used well they're the organizational backbone of a shared cluster; used naively they're just prefixes that give a false sense of isolation. Here's what they're for and how to make them mean something.
Table of Contents
- What Are Namespaces
- When to Use Namespaces
- Namespace Organization Strategies
- Resource Quotas
- Network Policies
- RBAC per Namespace
- Common Mistakes
- Useful Commands
What Are Namespaces
Namespaces provide logical separation within a single cluster — a scope for names (two web Deployments can coexist in different namespaces), a boundary for RBAC and quotas, and a DNS domain for Services (svc.namespace.svc.cluster.local).
# List all namespaces
kubectl get namespaces
# Built-in namespaces
NAME STATUS AGE
default Active 30d # default for everything without -n
kube-system Active 30d # cluster components (DNS, proxy)
kube-public Active 30d # publicly readable data
kube-node-lease Active 30d # node heartbeats
The important caveat up front: a namespace is not a security boundary by itself. Pods in different namespaces share the same nodes and the same default network — a payments pod can reach a dev pod unless you build the walls: RBAC, NetworkPolicies, quotas, Pod Security Standards.
When to Use Namespaces
Good reasons:
- Separate environments (dev / staging / prod)
- Separate teams on a shared cluster
- Separate applications with different resource needs or lifecycles
- A boundary to hang RBAC, quotas, and policies on
Bad reasons:
- One namespace per microservice in a small team — over-engineering that fragments visibility
- Namespaces as a security boundary alone — they need the controls below to mean anything
- Namespaces to avoid cluster upgrades — a second cluster is sometimes the right blast radius; namespaces won't save you from a bad node
Namespace Organization Strategies
Strategy 1: By Environment (most common)
kubectl create namespace dev
kubectl create namespace staging
kubectl create namespace production
Pros: simple, matches CI/CD pipelines, clear blast radius. The standard for small-to-mid teams.
Strategy 2: By Team
kubectl create namespace team-payments
kubectl create namespace team-frontend
Pros: teams own their namespaces, quotas map to ownership, chargeback is legible.
Strategy 3: By Environment + Team (larger orgs)
payments-dev, payments-prod
frontend-dev, frontend-prod
The cartesian product gets noisy fast — generate these with templates (Helm, kustomize, a cluster-provisioning pipeline) rather than handcrafting fifty namespaces.
Resource Quotas
Quotas are what stop one team or environment from eating the whole cluster — a runaway dev deployment shouldn't be able to starve production:
apiVersion: v1
kind: ResourceQuota
metadata:
name: compute-quota
namespace: dev
spec:
hard:
requests.cpu: "4"
requests.memory: 8Gi
limits.cpu: "8"
limits.memory: 16Gi
pods: "20"
persistentvolumeclaims: "10"
services.loadbalancers: "2"
kubectl apply -f quota.yaml
kubectl describe quota -n dev # shows used vs hard limits
A quota counting pods, services.loadbalancers, and persistentvolumeclaims catches the resource types people forget — an accidental type: LoadBalancer Service is a real cloud bill, and a quota of 2 makes it a deliberate act.
LimitRange — Default Per-Container Limits
Quotas cap the namespace total; LimitRanges cap individual containers and — crucially — set defaults for pods that don't declare requests:
apiVersion: v1
kind: LimitRange
metadata:
name: default-limits
namespace: dev
spec:
limits:
- type: Container
default: # limit if unspecified
cpu: "500m"
memory: "512Mi"
defaultRequest: # request if unspecified
cpu: "100m"
memory: "128Mi"
max:
cpu: "2"
memory: "2Gi"
min:
cpu: "50m"
memory: "64Mi"
Without this, a namespace quota becomes a trap: pods without explicit requests fail to schedule once a quota exists, because the quota can't count what isn't declared. defaultRequest/default makes the pair work — every pod gets sane defaults automatically.
Network Policies
Namespaces alone do NOT isolate network traffic — pod-to-pod reaches across namespaces by default. NetworkPolicies build the walls:
# Deny all inbound traffic from other namespaces
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-cross-namespace
namespace: production
spec:
podSelector: {} # applies to all pods in production
policyTypes:
- Ingress
ingress:
- from:
- podSelector: {} # only pods in the same namespace
# Allow the monitoring namespace to scrape metrics
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-monitoring
namespace: production
spec:
podSelector: {}
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: monitoring
ports:
- port: 9090
The pattern that works: a default-deny ingress per namespace, then explicit allows for the flows you actually need (ingress controllers, monitoring scrapers, the DNS egress every namespace needs to kube-system). Policies only work if your CNI enforces them — Calico, Cilium, or the cloud CNI's policy mode; an unenforced NetworkPolicy is a comment.
RBAC per Namespace
Give teams access only to their namespaces — Role + RoleBinding scoped to the namespace, never a ClusterRoleBinding for a per-team grant:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: payments-dev
name: developer
rules:
- apiGroups: ["", "apps", "batch"]
resources: ["pods", "deployments", "jobs", "services", "configmaps"]
verbs: ["get", "list", "watch", "create", "update", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: developers
namespace: payments-dev
subjects:
- kind: Group
name: payments-team
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: developer
apiGroup: rbac.authorization.k8s.io
Namespaces make this practical: the same developer Role applies per-namespace with different subjects per RoleBinding — payments engineers get payments-dev, frontend engineers get frontend-dev, nobody gets production write by accident.
Common Mistakes
1. Everything in default
Deploying everything to default means no quotas, no RBAC isolation, and one flat blast radius. Create real namespaces from day one — retrofitting them means editing every manifest and every Service DNS reference.
2. Assuming namespaces = security
Namespaces are organizational, not a security boundary. Combine with RBAC + NetworkPolicy + Pod Security Standards:
# Enforce restricted pod security per namespace
kubectl label namespace production \
pod-security.kubernetes.io/enforce=restricted
3. Forgetting resource defaults
Pods without requests/limits can starve a namespace — and once a ResourceQuota exists, pods without declared requests can't schedule at all. Always pair namespaces with LimitRange.
4. Namespace sprawl
Dozens of near-empty namespaces are harder to manage than a few well-named ones. Keep the scheme simple, documented, and generated — a namespace per "thing someone once deployed" is how kubectl get ns becomes a graveyard.
Useful Commands
# Create with labels (useful for NetworkPolicy selectors)
kubectl create namespace monitoring
kubectl label namespace monitoring environment=observability
# Set default namespace for your context — stops typing -n everywhere
kubectl config set-context --current --namespace=dev
# See everything in a namespace
kubectl get all -n production
# Delete a namespace (deletes EVERYTHING inside — no undo)
kubectl delete namespace old-env
Namespaces done right are the difference between a shared cluster and a shared incident: quota the noisy neighbor, deny the flat network, scope the access — then the logical boundary becomes a real one.
Related Articles
Our tutorials and troubleshooting runbooks are written by a DevOps practitioner with hands-on experience operating Kubernetes, Terraform, CI/CD pipelines, and Linux infrastructure in production. Commands, manifests, and configurations are verified against current tooling releases. Learn more about our editorial process or report a correction or outdated flag.