env.dev

Kubernetes (kubectl) Cheat Sheet

Quick reference for kubectl: contexts, core resources, pod operations, deployments, scaling, debugging, ConfigMaps, and Secrets.

Last updated:

A quick reference for kubectl and Kubernetes. Covers contexts, core resources, pod operations, deployments, namespaces, debugging, and config.

Context & Cluster

CommandDescription
kubectl config get-contextsList all contexts
kubectl config current-contextShow the active context
kubectl config use-context <ctx>Switch to a context
kubectl config set-context --current --namespace=<ns>Set default namespace for current context
kubectl cluster-infoShow cluster endpoint info
kubectl versionShow client and server versions
kubectl api-resourcesList all available resource types

Core Resource Commands

CommandDescription
kubectl get <resource>List resources (pods, deployments, svc, …)
kubectl get <resource> -o wideList with extra columns (node, IP, …)
kubectl get <resource> -o yamlOutput full YAML definition
kubectl get all -n <ns>List all resource types in a namespace
kubectl describe <resource> <name>Show detailed state and events
kubectl apply -f <file.yaml>Create or update resources from file
kubectl apply -f <dir/>Apply all manifests in a directory
kubectl delete -f <file.yaml>Delete resources defined in a file
kubectl delete <resource> <name>Delete a specific resource by name
kubectl edit <resource> <name>Open resource in editor for live editing

Namespaces & Label Selectors

CommandDescription
kubectl get nsList all namespaces
kubectl create ns <name>Create a namespace
kubectl delete ns <name>Delete a namespace and all its resources
kubectl get pods -n <ns>List pods in a specific namespace
kubectl get pods -AList pods across all namespaces
kubectl get pods -l app=webFilter by label selector
kubectl get pods -l env=prod,tier=frontendFilter by multiple labels
kubectl label pod <name> env=prodAdd or update a label on a resource

Pod Operations

CommandDescription
kubectl logs <pod>Print pod logs
kubectl logs <pod> -fStream/follow pod logs
kubectl logs <pod> -c <container>Logs from a specific container in a pod
kubectl logs <pod> --previousLogs from the previous (crashed) container
kubectl exec -it <pod> -- shOpen a shell in a running pod
kubectl exec -it <pod> -c <c> -- shShell into a specific container
kubectl exec <pod> -- <cmd>Run a one-off command in a pod
kubectl port-forward pod/<pod> 8080:80Forward local port 8080 to pod port 80
kubectl port-forward svc/<svc> 8080:80Forward local port to a service port
kubectl cp <pod>:<src> <dst>Copy file from pod to local
kubectl cp <src> <pod>:<dst>Copy file from local to pod

Deployments & Scaling

CommandDescription
kubectl get deploymentsList deployments
kubectl scale deploy/<name> --replicas=3Scale a deployment to 3 replicas
kubectl rollout status deploy/<name>Watch rollout progress
kubectl rollout history deploy/<name>Show rollout history
kubectl rollout undo deploy/<name>Roll back to the previous revision
kubectl rollout undo deploy/<name> --to-revision=2Roll back to a specific revision
kubectl set image deploy/<name> app=image:v2Update the container image
kubectl rollout restart deploy/<name>Trigger a rolling restart
kubectl autoscale deploy/<name> --min=2 --max=10 --cpu-percent=80Create an HPA

Services & Networking

CommandDescription
kubectl get svcList services
kubectl expose deploy/<name> --port=80 --type=ClusterIPExpose a deployment as a service
kubectl get ingressList ingress resources
kubectl get endpointsShow endpoints (pod IPs behind a service)

ConfigMaps & Secrets

CommandDescription
kubectl get configmapsList ConfigMaps
kubectl describe configmap <name>Show ConfigMap data
kubectl create configmap <name> --from-literal=KEY=valCreate ConfigMap from literal
kubectl create configmap <name> --from-file=<file>Create ConfigMap from a file
kubectl get secretsList Secrets
kubectl create secret generic <name> --from-literal=KEY=valCreate a generic Secret
kubectl create secret docker-registry <name> --docker-server=… --docker-username=… --docker-password=…Create image pull Secret
kubectl get secret <name> -o jsonpath="{.data.KEY}" | base64 -dDecode a secret value

Debugging Workflow

CommandDescription
kubectl get pods1. List pods — spot CrashLoopBackOff / Pending states
kubectl describe pod <name>2. Inspect events and conditions for root cause
kubectl logs <pod> --previous3. Read logs from the last crashed container
kubectl exec -it <pod> -- sh4. Shell in to inspect filesystem or run diagnostics
kubectl get events --sort-by=.lastTimestampView cluster events sorted by time
kubectl top podsShow CPU/memory usage for pods (metrics-server required)
kubectl top nodesShow CPU/memory usage for nodes
kubectl run tmp --image=busybox --rm -it --restart=Never -- shSpin up a throwaway debug pod

Common YAML Structure

FieldDescription
apiVersionAPI group + version, e.g. apps/v1 or v1
kindResource type: Pod, Deployment, Service, …
metadata.nameName of the resource
metadata.namespaceNamespace (defaults to default)
metadata.labelsKey-value pairs for selection and organisation
spec.selector.matchLabelsDeployment: selects the pods it manages
spec.templateDeployment: pod template (metadata + spec)
spec.containers[].imageContainer image, e.g. nginx:1.27
spec.containers[].ports[].containerPortPort the container listens on
spec.containers[].envList of environment variables for the container
spec.containers[].resources.requests/limitsCPU and memory requests and limits
spec.containers[].livenessProbeHealth check — restarts container on failure
spec.containers[].readinessProbeReadiness check — gates traffic to the pod