Namespaces let you divide a single Kubernetes cluster into multiple virtual sub-clusters — useful for separating environments, teams, or projects while sharing the same underlying infrastructure.
What Namespaces Actually Do
Namespaces provide a scope for names (two Deployments named "app" can coexist in different namespaces without conflict) and a boundary for access control and resource limits — but they don't provide network isolation by default.
Default Namespaces
kubectl get namespaces
Every cluster starts with a few built-in namespaces: default (where resources go if unspecified), kube-system (cluster-internal components), and kube-public.
Creating a Namespace
kubectl create namespace staging
Deploying Resources to a Specific Namespace
kubectl apply -f deployment.yaml -n staging
Or specify it directly within the YAML:
metadata:
name: my-app
namespace: staging
Common Namespace Organization Patterns
| Pattern | Example |
|---|---|
| By environment | development, staging, production |
| By team | team-frontend, team-backend |
| By project | project-a, project-b |
Switching Your Default Namespace Context
kubectl config set-context --current --namespace=staging
Avoids needing -n staging on every command afterward, for the current kubectl context.
Resource Quotas: Limiting What a Namespace Can Consume
apiVersion: v1
kind: ResourceQuota
metadata:
name: staging-quota
namespace: staging
spec:
hard:
requests.cpu: "4"
requests.memory: 8Gi
limits.cpu: "8"
limits.memory: 16Gi
pods: "20"
kubectl apply -f resourcequota.yaml
This prevents any single namespace from consuming unbounded cluster resources, important on a shared cluster with multiple teams/projects.
Checking Current Quota Usage
kubectl describe resourcequota staging-quota -n staging
LimitRange: Setting Defaults for Individual Pods
apiVersion: v1
kind: LimitRange
metadata:
name: staging-limits
namespace: staging
spec:
limits:
- default:
cpu: 500m
memory: 256Mi
defaultRequest:
cpu: 100m
memory: 128Mi
type: Container
Ensures every container gets sensible default resource requests/limits even if not explicitly specified, which also makes Horizontal Pod Autoscaling function correctly (see How to Set Up Horizontal Pod Autoscaling in Kubernetes).
Isolating Network Traffic Between Namespaces (Advanced)
By default, Pods across different namespaces can still communicate freely — genuine network isolation requires NetworkPolicy resources, which depend on your cluster's network plugin (CNI) supporting them.
Deleting a Namespace (Caution)
kubectl delete namespace staging
This deletes every resource within that namespace — Deployments, Services, PVCs, everything — with no confirmation prompt beyond the command itself.
Common Errors
"resourcequota exceeded" when deploying — the namespace's quota is already fully consumed; either free up resources from existing workloads or increase the quota if appropriate.
Continue Reading
- How to Set Up Horizontal Pod Autoscaling in Kubernetes
- How to Install kubeadm and Set Up a Multi-Node Kubernetes Cluster
- How to Manage Multiple Environments (Dev/Staging/Prod) on VPS
Browse more articles in Kubernetes & Container Orchestration.