Namespaces

Namespaces partition a cluster into virtual sub-clusters for isolation and organization.

A namespace is a way to divide one physical cluster into multiple virtual ones. Names of objects must be unique within a namespace, but the same name can exist in different namespaces.

Why use them

  • Organization — separate dev, staging, and prod, or one per team.
  • Access control — grant permissions per namespace with RBAC.
  • Resource quotas — cap the CPU/memory a namespace may consume.

Built-in namespaces

Every cluster starts with default (where your objects go if you don't specify one), kube-system (control-plane components), and kube-public.

Target a namespace with -n <name>, or list across all with -A.

Example

Example · bash
# Create a namespace
kubectl create namespace team-a

# Deploy into it
kubectl apply -f app.yaml -n team-a

# List Pods in one namespace
kubectl get pods -n team-a

# See everything, everywhere
kubectl get pods -A

When to use it

  • A company creates separate namespaces for dev, staging, and production teams so that their resources are isolated and billed separately.
  • A platform team applies a ResourceQuota to each team namespace to enforce CPU and memory budget caps in a multi-tenant cluster.
  • A developer switches their kubectl context to their team namespace using kubectl config set-context so they do not accidentally interact with the production namespace.

More examples

Create and switch namespace

Creates a staging namespace and configures the kubectl context to use it by default, avoiding the need to pass -n on every command.

Example · bash
kubectl create namespace staging

# Set default namespace for the current context
kubectl config set-context --current --namespace=staging

# Verify
kubectl config view --minify | grep namespace

List resources in all namespaces

Lists pods across every namespace using -A (cluster-admin view), then narrows to a specific namespace with -n.

Example · bash
kubectl get pods --all-namespaces
# or shorthand:
kubectl get pods -A

kubectl get all -n staging

Namespace with ResourceQuota

Creates a namespace and immediately applies a ResourceQuota that limits the team to 8 CPU, 16 Gi memory, and 50 pods.

Example · yaml
apiVersion: v1
kind: Namespace
metadata:
  name: team-alpha
---
apiVersion: v1
kind: ResourceQuota
metadata:
  name: team-alpha-quota
  namespace: team-alpha
spec:
  hard:
    requests.cpu: "8"
    requests.memory: 16Gi
    count/pods: "50"

Discussion

  • Be the first to comment on this lesson.