First: Which Cluster Are You On?
kubectl sends every command to the current context in ~/.kube/config. If you work with more than one cluster, a delete meant for a test cluster can hit production. Check before you change anything:
kubectl config current-context
kubectl config get-contexts
In the examples, web is a deployment, web-7d9c a pod, and app a namespace. Replace them with your own names.
Contexts and Namespaces
| Command | Meaning |
|---|---|
kubectl config use-context prod | Switch cluster |
kubectl get ns | List namespaces |
kubectl get pods -n app | Work in one namespace |
kubectl get pods -A | All namespaces |
kubectl create ns app | Create a namespace |
To make a namespace the default for the current context, run kubectl config set-context --current --namespace=app.
Viewing Resources
| Command | Meaning |
|---|---|
kubectl get pods | List pods |
kubectl get pods -o wide | Add node and IP |
kubectl get deploy,svc | Several types |
kubectl get all | Common types |
kubectl describe pod web-7d9c | Details and events |
kubectl get pod web-7d9c -o yaml | Full definition |
kubectl get pods -w | Watch changes |
kubectl get nodes | List nodes |
For recent events in time order, add --sort-by=.metadata.creationTimestamp to kubectl get events.
Logs, Exec, and Port Forwarding
| Command | Meaning |
|---|---|
kubectl logs web-7d9c | Pod output |
kubectl logs -f web-7d9c | Follow output |
kubectl logs web-7d9c --previous | Before last restart |
kubectl logs web-7d9c -c nginx | One container |
kubectl logs deploy/web | A pod of the deployment |
kubectl exec -it web-7d9c -- sh | Shell in the pod |
kubectl port-forward svc/web 8080:80 | Local port to service |
kubectl cp web-7d9c:/tmp/a.log a.log | Copy a file out |
Port forwarding listens on your own machine only and stops with Ctrl+C. It is a safe way to reach a service that is not exposed.
Applying and Deleting
| Command | Meaning |
|---|---|
kubectl apply -f web.yaml | Create or update |
kubectl apply -f k8s/ | Every file in a folder |
kubectl diff -f web.yaml | Preview changes |
kubectl explain deployment.spec | Field reference |
Read this before you delete anything: deleting a namespace deletes everything in it, and deleting a persistent volume claim can delete its data too. Dynamically provisioned volumes take the reclaim policy of their storage class, and that policy defaults to Delete, which removes the volume and the storage behind it. There is no undo. Look first at the cluster, at what the file covers, and at the volumes and their reclaim policy:
kubectl config current-context
kubectl get -f web.yaml
kubectl get pvc -n app
kubectl get pv
Then delete:
kubectl delete -f web.yaml
kubectl delete pod web-7d9c
Deleting a pod that belongs to a deployment is a common way to restart it: the deployment creates a replacement.
Scaling and Rollouts
| Command | Meaning |
|---|---|
kubectl scale deploy/web --replicas=3 | Set pod count |
kubectl set image deploy/web nginx=nginx:1.30 | New image |
kubectl rollout status deploy/web | Wait for rollout |
kubectl rollout history deploy/web | Past revisions |
kubectl rollout undo deploy/web | Previous revision |
kubectl rollout restart deploy/web | Restart all pods |
Prefer changing the YAML file and applying it over set image, so the file stays the record of what runs.
Debugging
| Command | Meaning |
|---|---|
kubectl describe pod web-7d9c | Read the events |
kubectl get events -n app | Namespace events |
kubectl top pods | CPU and memory |
kubectl describe node node1 | Node load and taints |
kubectl describe svc web | Pods behind a service |
kubectl auth can-i delete pods | Check a permission |
kubectl top needs Metrics Server installed and running in the cluster. A pod stuck in a crash loop shows its last error with logs --previous; one stuck in pending usually explains why at the end of describe. For a shell with tools the image lacks, kubectl debug -it web-7d9c --image=busybox adds an ephemeral debug container to the running pod and attaches to it, without restarting the pod.
Output and Shortcuts
- Short names:
pofor pods,deploy,svc,ns,cmfor config maps. - Labels:
-l app=webselects by label in get, logs, and delete. - Machine output:
-o json,-o yaml, or-o namefor scripts. - Completion:
source <(kubectl completion bash)in your .bashrc adds tab completion; thebash-completionpackage must be installed.
For the same kind of quick reference to the shell itself, see the Linux commands cheat sheet. On a single server, Kubernetes is often more than you need; docker compose up -d runs several containers from one file.