Lab 09 - Namespaces

Lab 09 - Namespaces

Goal: scope your work and stop typing -n. Chapter: Namespaces

# 1 - what exists now
kubectl get ns

Four namespaces come with every cluster. kube-system holds the control plane Pods - look but do not deploy there.

# 2 - declarative (recommended)
cd ~
cat > ns-dev.yaml <<'YAML'
apiVersion: v1
kind: Namespace
metadata:
  name: dev
YAML
kubectl apply -f ns-dev.yaml

# 3 - imperative, for the throwaway one
kubectl create ns test

# 4 - the same name in both, with no conflict
kubectl run web --image nginx:1.27 -n dev
kubectl run web --image nginx:1.27 -n test
kubectl get pods -n dev
kubectl get pods -A | grep ' web '

Names are unique per namespace per kind. That is the whole point: dev and prod can both have a web.

# 5 - namespaced vs cluster-scoped
kubectl api-resources --namespaced=true  | head -10
kubectl api-resources --namespaced=false | head -10
kubectl get nodes -n dev                 # -n is silently ignored for nodes
# 6 - make dev the default for this context
kubectl config set-context --current --namespace=dev
kubectl get pods                         # no -n needed
kubectl config view --minify | grep namespace

This edits your kubeconfig, not the cluster. It is also how people delete things in the wrong place - kubectl config get-contexts shows the active namespace, and it is worth checking before anything destructive.

Clean up

# 7 - deleting a namespace deletes everything inside it
kubectl config set-context --current --namespace=default
kubectl delete ns test
kubectl delete -f ns-dev.yaml
kubectl get ns
Warning

No confirmation, no undo. Deleting a namespace deletes every object in it.