Working with Namespaces

1. Creating Namespaces

MethodCommand
Imperativekubectl create namespace payments
Declarativekubectl apply -f ns.yaml
Inlinekubectl create ns dev --dry-run=client -o yaml | kubectl apply -f -

Example: Namespace YAML with labels

apiVersion: v1
kind: Namespace
metadata:
  name: payments
  labels:
    team: finance
    env: prod
    pod-security.kubernetes.io/enforce: restricted

2. Listing Namespaces

CommandOutput
kubectl get nsAll namespaces + age + status
kubectl get ns -l env=prodFilter by label
kubectl get ns -o jsonpath='{.items[*].metadata.name}'Names only

3. Switching Namespaces

CommandEffect
kubectl config set-context --current --namespace=NSPersist default
kubens NSSame via plugin
kubectl -n NS get podsOne-off override

4. Deleting Namespaces

CommandBehavior
kubectl delete ns NSCascading delete of all resources in NS
kubectl delete ns NS --grace-period=0 --forceForce delete
Warning: Namespace deletion is irreversible. Stuck "Terminating" usually indicates a finalizer; patch spec.finalizers to [] to unstick.

5. Using Namespace Flag

FlagEffect
-n NS / --namespace NSTarget specific namespace
-A / --all-namespacesAll namespaces

6. Viewing Resources Across Namespaces

kubectl get pods -A
kubectl get pods -A -o wide --field-selector=status.phase=Running
kubectl get pods -A --sort-by=.metadata.creationTimestamp
TipCommand
Resource count per nskubectl get pods -A --no-headers | awk '{print $1}' | sort | uniq -c

7. Setting Resource Quotas

Quota TypeExamples
Computerequests.cpu, limits.memory, requests.storage
Object countpods, services, configmaps, secrets, persistentvolumeclaims
Storage classgold.storageclass.../requests.storage
ScopeTerminating, BestEffort, NotBestEffort, PriorityClass

Example: ResourceQuota

apiVersion: v1
kind: ResourceQuota
metadata:
  name: team-quota
  namespace: payments
spec:
  hard:
    requests.cpu: "20"
    requests.memory: 40Gi
    limits.cpu: "40"
    limits.memory: 80Gi
    pods: "100"
    persistentvolumeclaims: "10"

8. Configuring Limit Ranges

FieldPurpose
defaultDefault limit applied if pod omits it
defaultRequestDefault request applied
max / minAllowed range
maxLimitRequestRatioCap on limit/request ratio
typeContainer, Pod, PersistentVolumeClaim

Example: Container LimitRange

apiVersion: v1
kind: LimitRange
metadata: { name: mem-cpu-limits, namespace: payments }
spec:
  limits:
  - type: Container
    default: { cpu: 500m, memory: 512Mi }
    defaultRequest: { cpu: 100m, memory: 128Mi }
    max: { cpu: "2", memory: 2Gi }
    min: { cpu: 50m, memory: 64Mi }

9. Understanding Default Namespaces

NamespacePurpose
defaultCatch-all when no namespace specified
kube-systemControl plane & add-ons
kube-publicCluster-readable info (rare)
kube-node-leaseNode heartbeat leases

10. Isolating Namespaces with Network Policies

Example: Deny all ingress from other namespaces

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata: { name: deny-from-other-ns, namespace: payments }
spec:
  podSelector: {}
  policyTypes: [Ingress]
  ingress:
  - from:
    - namespaceSelector:
        matchLabels: { kubernetes.io/metadata.name: payments }
PatternResult
Empty podSelectorApplies to all pods in NS
namespaceSelectorMatch by NS labels
CNI requirementCalico, Cilium, Weave (not all CNIs enforce)