Cheatsheets / kubectl Cheatsheet

kubectl Cheatsheet

Everyday kubectl commands for inspecting clusters, managing pods and deployments, and debugging - the ones you actually run day to day.

Last verified

Context and config

kubectl config get-contexts

Lists all configured clusters/contexts and shows which one is active.

kubectl config current-context

Prints just the active context’s name.

kubectl config use-context staging

Switches the active context to a different cluster.

kubectl config set-context --current --namespace=my-app

Sets the default namespace for the current context, so you stop typing -n my-app every time.

kubectl cluster-info

Shows the API server and core service endpoints for the current cluster.

Getting and describing resources

kubectl get pods

Lists pods in the current namespace.

kubectl get pods -A

Lists pods across all namespaces.

kubectl get pods -o wide

Lists pods with extra columns: node, IP, and more.

kubectl get pods -w

Watches pods and streams status changes live.

kubectl get all

Lists pods, services, deployments, and replicasets in the current namespace.

kubectl describe pod my-pod

Shows detailed info and recent events for a pod - the first thing to check when something’s stuck.

kubectl get events --sort-by=.lastTimestamp

Lists recent cluster events, oldest first, across the current namespace.

kubectl get pods -l app=api

Lists pods matching a label selector.

kubectl get pod my-pod -o yaml

Dumps a resource’s full manifest, including server-populated fields.

Pods

kubectl logs my-pod

Shows a pod’s stdout/stderr logs.

kubectl logs -f my-pod

Follows a pod’s logs live.

kubectl logs my-pod -c sidecar

Shows logs for a specific container in a multi-container pod.

kubectl logs my-pod --previous

Shows logs from a pod’s previous instance - useful right after a crash loop restart.

kubectl exec -it my-pod -- bash

Opens an interactive shell inside a running pod.

kubectl exec my-pod -- env

Runs a one-off command inside a pod without attaching a shell.

kubectl delete pod my-pod

Deletes a pod (a Deployment-managed pod is immediately recreated by its controller).

kubectl port-forward pod/my-pod 8080:80

Forwards a local port to a port inside the pod, for local debugging.

Deployments and scaling

kubectl get deployments

Lists deployments and their ready/available replica counts.

kubectl scale deployment my-app --replicas=3

Changes the number of replicas for a deployment.

kubectl rollout status deployment my-app

Watches a deployment’s rollout until it finishes or fails.

kubectl rollout history deployment my-app

Lists past rollout revisions for a deployment.

kubectl rollout undo deployment my-app

Rolls back to the previous deployment revision.

kubectl set image deployment/my-app api=myrepo/api:1.2.0

Updates a deployment’s container image, triggering a rolling update.

kubectl autoscale deployment my-app --min=2 --max=10 --cpu-percent=80

Creates a Horizontal Pod Autoscaler for a deployment.

Services and networking

kubectl get svc

Lists services in the current namespace.

kubectl expose deployment my-app --port=80 --target-port=8080

Creates a Service in front of a deployment.

kubectl port-forward svc/my-app 8080:80

Forwards a local port to a service, without needing a specific pod name.

kubectl get ingress

Lists Ingress resources and the hosts/paths they route.

ConfigMaps and Secrets

kubectl create configmap app-config --from-literal=LOG_LEVEL=debug

Creates a ConfigMap from a literal key-value pair.

kubectl create secret generic db-creds --from-literal=password=hunter2

Creates a Secret from a literal key-value pair (values are base64-encoded, not encrypted, at rest by default).

kubectl get secret db-creds -o jsonpath='{.data.password}' | base64 -d

Decodes and prints a Secret’s value.

kubectl describe configmap app-config

Shows a ConfigMap’s keys and values.

Apply, delete, and rollout

kubectl apply -f deployment.yaml

Creates or updates resources to match a manifest file - the standard way to deploy.

kubectl apply -f k8s/

Applies every manifest file in a directory.

kubectl diff -f deployment.yaml

Shows what apply would change, without actually changing anything.

kubectl delete -f deployment.yaml

Deletes every resource defined in a manifest file.

kubectl delete pods -l app=api

Deletes all pods matching a label selector.

kubectl top pods

Shows live CPU and memory usage per pod (requires the metrics-server add-on).

kubectl top nodes

Shows live CPU and memory usage per node.