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-contextsIn 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- | 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, | Several types |
kubectl get all | Common types |
kubectl describe pod web- | Details and events |
kubectl get pod web- | 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- | Pod output |
kubectl logs -f web- | Follow output |
kubectl logs web- | Before last restart |
kubectl logs web- | One container |
kubectl logs deploy/ | A pod of the deployment |
kubectl exec -it web- | Shell in the pod |
kubectl port- | Local port to service |
kubectl cp web- | 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 pvThen delete:
kubectl delete -f web.yaml
kubectl delete pod web-7d9cDeleting 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/ | Set pod count |
kubectl set image deploy/ | New image |
kubectl rollout status deploy/ | Wait for rollout |
kubectl rollout history deploy/ | Past revisions |
kubectl rollout undo deploy/ | Previous revision |
kubectl rollout restart deploy/ | 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- | 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- | 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.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations