Kubernetes Namespaces
Kubernetes Namespaces
Section titled “Kubernetes Namespaces”Namespaces partition a single Kubernetes cluster into logically isolated environments. Resources within a namespace are isolated from those in other namespaces (with some exceptions like nodes and persistent volumes).
Default Namespaces
Section titled “Default Namespaces”| Namespace | Purpose |
|---|---|
default | Where resources go if no namespace is specified |
kube-system | Kubernetes system components (DNS, metrics-server, etc.) |
kube-public | Publicly readable resources (cluster info) |
kube-node-lease | Node heartbeat objects |
Creating Namespaces
Section titled “Creating Namespaces”kubectl create namespace stagingOr from YAML:
apiVersion: v1kind: Namespacemetadata: name: staging labels: env: stagingWorking Across Namespaces
Section titled “Working Across Namespaces”# Specify namespace with -nkubectl get pods -n stagingkubectl apply -f deployment.yaml -n staging
# See resources in all namespaceskubectl get pods --all-namespaceskubectl get pods -A
# Set a default namespace for your contextkubectl config set-context --current --namespace=stagingDNS Across Namespaces
Section titled “DNS Across Namespaces”Services are reachable across namespaces using their fully qualified DNS name:
<service-name>.<namespace>.svc.cluster.localFrom the default namespace, reach a service in staging:
http://api-service.staging.svc.cluster.localResource Quotas
Section titled “Resource Quotas”Prevent one team or environment from consuming all cluster resources:
apiVersion: v1kind: ResourceQuotametadata: name: staging-quota namespace: stagingspec: hard: pods: "20" requests.cpu: "4" requests.memory: 8Gi limits.cpu: "8" limits.memory: 16Gi persistentvolumeclaims: "10"kubectl apply -f quota.yamlkubectl describe resourcequota -n stagingLimitRange
Section titled “LimitRange”Set default resource requests and limits for pods in a namespace:
apiVersion: v1kind: LimitRangemetadata: name: default-limits namespace: stagingspec: limits: - default: cpu: 500m memory: 256Mi defaultRequest: cpu: 100m memory: 128Mi type: ContainerNamespace-Based Access Control
Section titled “Namespace-Based Access Control”RBAC roles are namespace-scoped by default:
apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: dev-team-binding namespace: stagingsubjects: - kind: Group name: dev-team apiGroup: rbac.authorization.k8s.ioroleRef: kind: Role name: developer apiGroup: rbac.authorization.k8s.ioCommon Patterns
Section titled “Common Patterns”Dev/Staging/Prod in one cluster:
kubectl create namespace devkubectl create namespace stagingkubectl create namespace prodPros: shared infrastructure cost, easier cluster management Cons: production workloads compete for resources with dev; requires strong RBAC and quotas
One cluster per environment: Better isolation, harder to manage. Preferred for strict compliance requirements.
Deleting Namespaces
Section titled “Deleting Namespaces”kubectl delete namespace stagingThis deletes all resources inside — use with caution. Terminating can hang if there are resources with finalizers; check with:
kubectl get namespace staging -o yaml