kubectl and namespaces
Learn the handful of commands you will use every day and how namespaces keep names from colliding.
Open this lesson in the learning hubKey points
kubectl apply -fis declarative and idempotent. Run it a hundred times and you get the same cluster.- The debug loop is four verbs:
getto list,describeto read events,logsto see output,execto poke inside. - The Events section at the bottom of
describeexplains 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
webin different namespaces without a clash. - Pin the namespace on your context once instead of typing
-nfor 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.