kubectl and namespaces

Kubernetes · lesson 4 of 32 · 4 min read

Learn the handful of commands you will use every day and how namespaces keep names from colliding.

Open this lesson in the learning hub

Key points

  • kubectl apply -f is declarative and idempotent. Run it a hundred times and you get the same cluster.
  • The debug loop is four verbs: get to list, describe to read events, logs to see output, exec to poke inside.
  • The Events section at the bottom of describe explains most failures: image pull errors, unschedulable pods, OOMKilled.
  • A namespace scopes names and quotas. It is organisation, not security, until you add RBAC and NetworkPolicies.
  • Two teams can each own a Service called web in different namespaces without a clash.
  • Pin the namespace on your context once instead of typing -n for the rest of your life.

Example

kubectl apply -f deployment.yaml
kubectl get pods -o wide
kubectl describe pod web-7d9f8c-4xk2p

kubectl logs -f deploy/web
kubectl logs web-7d9f8c-4xk2p --previous   # logs from the crashed instance
kubectl exec -it deploy/web -- sh
kubectl port-forward svc/web 8080:80

kubectl get namespaces
kubectl create namespace staging
kubectl get pods -n staging
kubectl config set-context --current --namespace=staging

get, describe, logs, exec. Ninety percent of your Kubernetes life happens in those four verbs.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Kubernetes course, and every lesson in it is listed on the Kubernetes contents page.