Kubernetes Namespace
Namespaces provide virtual clusters within a Kubernetes cluster, enabling isolation of resources, access control, and quota enforcement across teams or environments.
A Namespace is a Kubernetes mechanism for dividing one physical cluster into multiple virtual clusters. Resources in different namespaces are isolated from each other by name: two teams can both create a Deployment named api without conflict. Namespaces are also the boundary for RBAC (Role-Based Access Control), ResourceQuota, and LimitRange — making them the primary tool for multi-tenancy and environment separation within a single cluster.
Default Namespaces
Kubernetes ships with four built-in namespaces:
| Namespace | Purpose |
|---|---|
default |
The namespace used when no namespace is specified in a manifest or command |
kube-system |
Kubernetes system components (kube-apiserver, kube-dns, kube-proxy) |
kube-public |
Publicly readable; used for cluster info accessible to all users including unauthenticated |
kube-node-lease |
Holds Lease objects for node heartbeat detection (one per node) |
Do not deploy application workloads into kube-system. Create dedicated namespaces for each team or environment.
Creating Namespaces
Declarative (recommended):
apiVersion: v1
kind: Namespace
metadata:
name: team-alpha
labels:
team: alpha
env: production
Imperative:
kubectl create namespace team-alpha
# List all namespaces
kubectl get namespaces
# NAME STATUS AGE
# default Active 30d
# kube-system Active 30d
# kube-node-lease Active 30d
# kube-public Active 30d
# team-alpha Active 5s
Working Within a Namespace
# Deploy to a specific namespace
kubectl apply -f deployment.yaml -n team-alpha
# List Pods in a namespace
kubectl get pods -n team-alpha
# List all Pods across all namespaces
kubectl get pods --all-namespaces
# or
kubectl get pods -A
# Set a default namespace for your kubectl context
kubectl config set-context --current --namespace=team-alpha
# Now kubectl commands default to team-alpha without -n
ResourceQuota — Limiting Resource Consumption
A ResourceQuota limits the total resources that all Pods in a namespace can consume:
apiVersion: v1
kind: ResourceQuota
metadata:
name: team-alpha-quota
namespace: team-alpha
spec:
hard:
requests.cpu: "4"
requests.memory: "8Gi"
limits.cpu: "8"
limits.memory: "16Gi"
pods: "20"
count/deployments.apps: "10"
kubectl describe resourcequota team-alpha-quota -n team-alpha
# Name: team-alpha-quota
# Namespace: team-alpha
# Resource Used Hard
# -------- ---- ----
# limits.cpu 1500m 8
# limits.memory 2Gi 16Gi
# pods 3 20
LimitRange — Setting Default Requests/Limits
A LimitRange sets default and maximum CPU/memory for containers in a namespace. This prevents Pods from starting without resource requests when a ResourceQuota is active:
apiVersion: v1
kind: LimitRange
metadata:
name: team-alpha-limits
namespace: team-alpha
spec:
limits:
- type: Container
default:
cpu: "200m"
memory: "256Mi"
defaultRequest:
cpu: "100m"
memory: "128Mi"
max:
cpu: "2"
memory: "4Gi"
Cross-Namespace DNS
A Service in one namespace is reachable from another namespace using the fully qualified DNS name:
<service-name>.<namespace>.svc.cluster.local
For example, a Service named postgres-svc in the data namespace is reachable at:
postgres-svc.data.svc.cluster.local
Common Mistakes
- Running all workloads in the
defaultnamespace. This makes RBAC, quotas, and troubleshooting much harder. Create dedicated namespaces per team or per environment (e.g.,dev,staging,prod). - Forgetting that some resources are cluster-scoped.
Node,PersistentVolume,ClusterRole,IngressClass, andStorageClassare not namespace-scoped and exist at the cluster level. Only namespace-scoped resources likePod,Service, andDeploymentare isolated by namespace. - Deploying to
kube-system. Adding application workloads tokube-systempollutes the system namespace and makes it harder to distinguish platform components from user workloads. - Deleting a namespace without understanding cascading deletion. Deleting a namespace deletes all resources inside it — Pods, Services, ConfigMaps, Secrets, Deployments, and PVCs (though the underlying PersistentVolume may or may not be deleted depending on its
reclaimPolicy). - Relying on namespace isolation for security. Namespaces do not provide network-level isolation by default. All Pods can communicate across namespaces unless a
NetworkPolicyis applied.
Best Practices
- Create one namespace per environment (
dev,staging,prod) or per team, depending on your organizational structure. - Apply
ResourceQuotaandLimitRangeto every non-system namespace to prevent runaway resource consumption. - Use RBAC
RoleandRoleBinding(namespace-scoped) to grant minimum necessary permissions within each namespace. - Apply
NetworkPolicyto restrict cross-namespace traffic to only what is required. - Use labels on namespaces to support tools like
NetworkPolicy(which can usenamespaceSelector) and policy engines.
Interview Questions
1. What is a Kubernetes Namespace and why is it used?
A Namespace is a virtual cluster within a physical Kubernetes cluster. It provides a scope for resource names (two teams can have Deployments with the same name in different namespaces) and is the boundary for RBAC, ResourceQuota, and LimitRange. It is used to separate environments (dev/staging/prod) or teams within a shared cluster.
2. What resources are cluster-scoped vs namespace-scoped?
Namespace-scoped resources (most workload and config types): Pod, Service, Deployment, ConfigMap, Secret, Ingress, PersistentVolumeClaim. Cluster-scoped resources: Node, PersistentVolume, Namespace itself, ClusterRole, ClusterRoleBinding, IngressClass, StorageClass. Cluster-scoped resources do not belong to any namespace.
3. Does a Namespace provide network isolation between Pods?
No. By default, Pods in any namespace can reach Pods in any other namespace. Network isolation requires applying NetworkPolicy resources (which depend on a CNI plugin that enforces them, such as Calico or Cilium). Namespaces only isolate resource names and RBAC scope, not network traffic.
4. What is a ResourceQuota and how does it relate to Namespaces?
A ResourceQuota is a namespace-scoped object that sets hard limits on the total resources (CPU, memory, object counts) that all Pods in a namespace can consume in aggregate. When a quota is active and a Pod's resource request would exceed the remaining quota, the Pod creation is rejected. This prevents one team from starving others in a shared cluster.
5. What is the difference between a Role and a ClusterRole?
A Role is namespace-scoped and grants permissions to resources within a single namespace. A ClusterRole is cluster-scoped and can grant permissions to resources across all namespaces or to cluster-scoped resources (like Node and PersistentVolume). RoleBinding grants a Role or ClusterRole to a user/ServiceAccount within a namespace; ClusterRoleBinding grants a ClusterRole cluster-wide.
6. How do you reach a Service in a different namespace?
Use the fully qualified DNS name: <service-name>.<namespace>.svc.cluster.local. For example, to reach postgres-svc in the data namespace from any Pod: jdbc:postgresql://postgres-svc.data.svc.cluster.local:5432/appdb.
7. What happens when you delete a Namespace?
Deleting a namespace triggers cascading deletion of all namespace-scoped resources inside it: Pods, Services, Deployments, ConfigMaps, Secrets, PVCs, etc. The namespace enters a Terminating state while Kubernetes cleans up finalizers on each resource. The underlying PersistentVolume may be retained or deleted depending on its persistentVolumeReclaimPolicy.
Summary
- A
Namespaceis a virtual cluster that scopes resource names and serves as the boundary for RBAC, ResourceQuota, and LimitRange. - Use dedicated namespaces per team or environment rather than deploying everything to
default. - Namespaces do not provide network isolation — apply
NetworkPolicyfor that. ResourceQuotaandLimitRangeare the tools for controlling resource consumption per namespace.- Cluster-scoped resources (
Node,PersistentVolume,ClusterRole) exist outside of any namespace.
Continue Learning
Previous: Ingress | Next: Volume and PVC
Was this guide helpful?
CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.