kubernetes kubectl · ·

Kubectl Cheat Sheet: 60+ Essential Commands for DevOps

A comprehensive kubectl reference with 60+ commands grouped by task: pods, deployments, networking, debugging, RBAC, output formatting, and more.

Kubectl Cheat Sheet: 60+ Essential Commands for DevOps

This is a task-grouped reference for the kubectl commands you reach for daily. Bookmark it, then jump to the section you need. Commands use <name> for a resource name, <ns> for a namespace, and <pod> for a pod name.

Two habits that save time across every section:

  • Set a default namespace so you can drop -n <ns> from most commands: kubectl config set-context --current --namespace=<ns>
  • Alias k=kubectl and enable shell completion (see the last section).

Cluster and Version Info

CommandDescription
kubectl versionShow client and server versions
kubectl cluster-infoShow control plane and service endpoints
kubectl cluster-info dumpDump full cluster state for debugging
kubectl api-resourcesList all resource types and short names
kubectl api-versionsList supported API group versions
kubectl get componentstatusesCheck control plane component health

Namespaces

CommandDescription
kubectl get nsList all namespaces
kubectl create ns <ns>Create a namespace
kubectl delete ns <ns>Delete a namespace and everything in it
kubectl config set-context --current --namespace=<ns>Set default namespace
kubectl get all -n <ns>List common resources in a namespace

Pods

CommandDescription
kubectl get podsList pods in the current namespace
kubectl get pods -AList pods across all namespaces
kubectl get pods -o wideList pods with node and IP columns
kubectl get pods --show-labelsList pods with their labels
kubectl get pods -wWatch pod status changes live
kubectl describe pod <pod>Show detailed pod information and events
kubectl delete pod <pod>Delete a pod (a controller may recreate it)
kubectl run tmp --image=busybox -it --rm -- shStart a throwaway debug pod

Deployments and ReplicaSets

CommandDescription
kubectl get deployList deployments
kubectl create deploy <name> --image=<image>Create a deployment
kubectl scale deploy <name> --replicas=3Scale a deployment
kubectl autoscale deploy <name> --min=2 --max=10 --cpu-percent=80Add a horizontal pod autoscaler
kubectl get rsList ReplicaSets
kubectl set image deploy/<name> <container>=<image>Update the container image
kubectl set resources deploy/<name> --limits=cpu=500m,memory=256MiSet resource limits

Rollouts

CommandDescription
kubectl rollout status deploy/<name>Watch a rollout to completion
kubectl rollout history deploy/<name>List rollout revisions
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 rollout restart deploy/<name>Restart pods without changing spec
kubectl rollout pause deploy/<name>Pause a rollout mid-flight
kubectl rollout resume deploy/<name>Resume a paused rollout

StatefulSets, DaemonSets, and Jobs

CommandDescription
kubectl get statefulsetList StatefulSets
kubectl get daemonsetList DaemonSets
kubectl get jobsList Jobs
kubectl get cronjobsList CronJobs
kubectl create job --from=cronjob/<name> <run-name>Trigger a CronJob manually

Services and Networking

CommandDescription
kubectl get svcList services
kubectl expose deploy/<name> --port=80 --target-port=8080Create a service for a deployment
kubectl get endpointsList service endpoints
kubectl get ingressList ingress resources
kubectl port-forward svc/<name> 8080:80Forward a local port to a service
kubectl port-forward pod/<pod> 5000:5000Forward a local port to a pod
kubectl get networkpolicyList network policies

ConfigMaps and Secrets

CommandDescription
kubectl get configmapList ConfigMaps
kubectl create configmap <name> --from-file=./configCreate a ConfigMap from a file
kubectl create configmap <name> --from-literal=key=valueCreate a ConfigMap from literals
kubectl get secretList secrets
kubectl create secret generic <name> --from-literal=pass=s3cr3tCreate a generic secret
kubectl get secret <name> -o jsonpath='{.data.pass}' | base64 -dDecode a secret value

Storage

CommandDescription
kubectl get pvList PersistentVolumes
kubectl get pvcList PersistentVolumeClaims
kubectl get storageclassList storage classes
kubectl describe pvc <name>Inspect a claim and its binding status

Logs

CommandDescription
kubectl logs <pod>Print pod logs
kubectl logs <pod> -fStream pod logs
kubectl logs <pod> -c <container>Logs from a specific container
kubectl logs <pod> --previousLogs from the previously crashed container
kubectl logs -l app=<label> --tail=100Tail logs across pods by label
kubectl logs deploy/<name> --all-containersLogs from all containers in a deployment

Exec, Attach, and Copy

CommandDescription
kubectl exec -it <pod> -- shOpen a shell in a pod
kubectl exec <pod> -- envRun a one-off command in a pod
kubectl attach -it <pod>Attach to a running container process
kubectl cp <pod>:/path/file ./fileCopy a file out of a pod
kubectl cp ./file <pod>:/path/fileCopy a file into a pod

Debugging and Troubleshooting

CommandDescription
kubectl get events --sort-by=.metadata.creationTimestampList cluster events oldest first
kubectl describe node <name>Check node conditions and pressure
kubectl top podsShow pod CPU and memory usage
kubectl top nodesShow node CPU and memory usage
kubectl debug <pod> -it --image=busyboxAttach an ephemeral debug container
kubectl get pods --field-selector=status.phase=FailedList failed pods only

Nodes and Scheduling

CommandDescription
kubectl get nodesList nodes
kubectl cordon <node>Mark a node unschedulable
kubectl uncordon <node>Mark a node schedulable again
kubectl drain <node> --ignore-daemonsetsEvict pods to prepare for maintenance
kubectl taint nodes <node> key=value:NoScheduleAdd a taint to a node

Labels, Annotations, and Selectors

CommandDescription
kubectl label pod <pod> env=prodAdd or update a label
kubectl label pod <pod> env-Remove a label
kubectl annotate pod <pod> note='needs review'Add an annotation
kubectl get pods -l 'env in (prod,staging)'Select pods by label expression

Apply, Diff, and Manage Manifests

CommandDescription
kubectl apply -f manifest.yamlCreate or update from a manifest
kubectl apply -f ./dir/Apply every manifest in a directory
kubectl diff -f manifest.yamlPreview changes before applying
kubectl delete -f manifest.yamlDelete resources defined in a manifest
kubectl replace --force -f manifest.yamlRecreate a resource from a manifest
kubectl kustomize ./overlayRender a kustomize overlay

RBAC and Access

CommandDescription
kubectl auth can-i create podsCheck your own permissions
kubectl auth can-i '*' '*' --as=system:serviceaccount:<ns>:<sa>Check another identity’s access
kubectl get roles,rolebindings -AList roles and bindings everywhere
kubectl get clusterroleList cluster roles

Output Formatting and JSONPath

CommandDescription
kubectl get pod <pod> -o yamlPrint the full resource as YAML
kubectl get pod <pod> -o jsonPrint the full resource as JSON
kubectl get pods -o namePrint only resource names
kubectl get pods -o jsonpath='{.items[*].metadata.name}'Extract fields with JSONPath
kubectl get pods --sort-by=.status.startTimeSort output by a field
kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeNameCustom column output
kubectl explain pod.spec.containersShow schema docs for a field

Productivity: Aliases and Completion

CommandDescription
alias k=kubectlShorten the command to k
source <(kubectl completion bash)Enable bash completion
kubectl completion zsh > "${fpath[1]}/_kubectl"Install zsh completion
complete -o default -F __start_kubectl kExtend completion to the k alias
export KUBECONFIG=~/.kube/config:~/.kube/devMerge multiple kubeconfig files

Keep this page open in a tab during incidents. When you find a command you run often that is not here, add it to your own dotfiles as an alias so it becomes muscle memory.

Get the next article in your inbox

Practical DevOps tips, tutorials, and guides. No spam, unsubscribe anytime.