Skip to content
Log In Sign Up info@muvcloud.com

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

CommandMeaning
kubectl config use-context prodSwitch cluster
kubectl get nsList namespaces
kubectl get pods -n appWork in one namespace
kubectl get pods -AAll namespaces
kubectl create ns appCreate a namespace

To make a namespace the default for the current context, run kubectl config set-context --current --namespace=app.

Viewing Resources

CommandMeaning
kubectl get podsList pods
kubectl get pods -o wideAdd node and IP
kubectl get deploy,svcSeveral types
kubectl get allCommon types
kubectl describe pod web-7d9cDetails and events
kubectl get pod web-7d9c -o yamlFull definition
kubectl get pods -wWatch changes
kubectl get nodesList nodes

For recent events in time order, add --sort-by=.metadata.creationTimestamp to kubectl get events.

Logs, Exec, and Port Forwarding

CommandMeaning
kubectl logs web-7d9cPod output
kubectl logs -f web-7d9cFollow output
kubectl logs web-7d9c --previousBefore last restart
kubectl logs web-7d9c -c nginxOne container
kubectl logs deploy/webA pod of the deployment
kubectl exec -it web-7d9c -- shShell in the pod
kubectl port-forward svc/web 8080:80Local port to service
kubectl cp web-7d9c:/tmp/a.log a.logCopy 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

CommandMeaning
kubectl apply -f web.yamlCreate or update
kubectl apply -f k8s/Every file in a folder
kubectl diff -f web.yamlPreview changes
kubectl explain deployment.specField 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

CommandMeaning
kubectl scale deploy/web --replicas=3Set pod count
kubectl set image deploy/web nginx=nginx:1.30New image
kubectl rollout status deploy/webWait for rollout
kubectl rollout history deploy/webPast revisions
kubectl rollout undo deploy/webPrevious revision
kubectl rollout restart deploy/webRestart all pods

Prefer changing the YAML file and applying it over set image, so the file stays the record of what runs.

Debugging

CommandMeaning
kubectl describe pod web-7d9cRead the events
kubectl get events -n appNamespace events
kubectl top podsCPU and memory
kubectl describe node node1Node load and taints
kubectl describe svc webPods behind a service
kubectl auth can-i delete podsCheck 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: po for pods, deploy, svc, ns, cm for config maps.
  • Labels: -l app=web selects by label in get, logs, and delete.
  • Machine output: -o json, -o yaml, or -o name for scripts.
  • Completion: source <(kubectl completion bash) in your .bashrc adds tab completion; the bash-completion package 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.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

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.

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