Overview

This card collects the kubectl commands needed to inspect, deploy, debug, and recover workloads. The full API reference is at kubectl api-resources; this page covers the 20 percent you use 80 percent of the time. For the container layer underneath, see docker-commands.

Get and describe

Read state before changing it.

CommandWhat it does
kubectl get podsList pods in current namespace.
kubectl get pods -AList pods across all namespaces.
kubectl get pods -n stagingList pods in staging namespace.
kubectl get pods -o wideInclude node, IP, and age.
kubectl get pods -l app=apiFilter by label selector.
kubectl get all -n stagingAll resources in a namespace.
kubectl get nodesList cluster nodes and their status.
kubectl describe pod api-6f9d4 -n prodFull events and conditions for one pod.
kubectl describe node worker-1Node capacity, allocations, and events.
kubectl get events --sort-by='.lastTimestamp'Recent cluster events, newest last.

kubectl describe is the first stop when a pod is Pending or CrashLoopBackOff; the Events section shows exactly what went wrong.

Apply, delete, and rollout

Manage resources declaratively; apply over create.

CommandWhat it does
kubectl apply -f deploy.yamlCreate or update resources from a file.
kubectl apply -f ./k8s/Apply all manifests in a directory.
kubectl delete -f deploy.yamlDelete resources declared in a file.
kubectl delete pod api-6f9d4Force-delete a pod; it will be recreated by the controller.
kubectl rollout status deployment/apiBlock until a rollout finishes.
kubectl rollout history deployment/apiShow revision history.
kubectl rollout undo deployment/apiRoll back to the previous revision.
kubectl rollout undo deployment/api --to-revision=3Roll back to a specific revision.
kubectl scale deployment api --replicas=3Scale a deployment.
kubectl set image deployment/api api=myapp:v2Update the container image.

Prefer kubectl apply over kubectl create; apply is idempotent and works for both create and update.

Logs and exec

Reach inside running pods to read output and run commands.

CommandWhat it does
kubectl logs api-6f9d4Print log output for a pod.
kubectl logs -f api-6f9d4Stream logs.
kubectl logs -f api-6f9d4 -c sidecarStream logs from a specific container in a multi-container pod.
kubectl logs --previous api-6f9d4Logs from the previous (crashed) container instance.
kubectl logs -l app=api --tail=50Last 50 lines across all pods matching a label.
kubectl exec -it api-6f9d4 -- bashOpen an interactive shell inside a pod.
kubectl exec api-6f9d4 -- envPrint environment variables.
kubectl exec api-6f9d4 -- cat /etc/resolv.confRead a file without entering the pod interactively.
kubectl cp api-6f9d4:/app/logs/err.log ./err.logCopy a file out of a pod.

Use kubectl logs --previous when a pod restarted before you could check it; it reads the terminated instance’s logs.

Port-forward and proxy

Access services locally without exposing them publicly.

CommandWhat it does
kubectl port-forward pod/api-6f9d4 8080:8080Forward local port 8080 to pod port 8080.
kubectl port-forward svc/api 8080:80Forward through a Service.
kubectl port-forward deployment/api 8080:8080Forward through a Deployment (picks a random pod).
kubectl proxyStart a proxy to the Kubernetes API at localhost:8001.

kubectl port-forward svc/api 5432:5432 is the standard way to connect a local Postgres client to a cluster database.

Common selectors and resource shortnames

Save keystrokes with short names and label selectors.

Full nameShortnameExample
podspokubectl get po
servicessvckubectl get svc
deploymentsdeploykubectl get deploy
replicasetsrskubectl get rs
configmapscmkubectl get cm
namespacesnskubectl get ns
nodesnokubectl get no
persistentvolumeclaimspvckubectl get pvc
ingressesingkubectl get ing

Label selector syntax: -l key=value, -l 'key in (a,b)', -l key!=value. Field selectors: --field-selector status.phase=Running.

Common gotchas

  • kubectl apply without a namespace uses default. Set the namespace in the manifest metadata or always pass -n.
  • kubectl delete pod on a pod managed by a Deployment deletes the pod but the controller immediately creates a replacement. Delete the Deployment to stop the recreation loop.
  • kubectl get pods hides pods in other namespaces silently. Use -A or -n <ns> to avoid investigating the wrong namespace.
  • kubectl exec requires the container to have bash or sh. Minimal images (distroless, scratch) do not; use an ephemeral debug container with kubectl debug.
  • kubectl rollout undo only rolls back one step. Store manifest history in git; use a specific --to-revision for predictable rollbacks.
  • kubectl port-forward exits silently when the pod restarts. For persistent tunnels, use a local proxy or a proper Ingress.