DEV Community

DevOps Start
DevOps Start

Posted on • Originally published at devopsstart.com

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

Command Description
kubectl version Show client and server versions
kubectl cluster-info Show control plane and service endpoints
kubectl cluster-info dump Dump full cluster state for debugging
kubectl api-resources List all resource types and short names
kubectl api-versions List supported API group versions
kubectl get componentstatuses Check control plane component health

Namespaces

Command Description
kubectl get ns List 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

Command Description
kubectl get pods List pods in the current namespace
kubectl get pods -A List pods across all namespaces
kubectl get pods -o wide List pods with node and IP columns
kubectl get pods --show-labels List pods with their labels
kubectl get pods -w Watch 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 -- sh Start a throwaway debug pod

Deployments and ReplicaSets

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

Rollouts

Command Description
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=2 Roll 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

Command Description
kubectl get statefulset List StatefulSets
kubectl get daemonset List DaemonSets
kubectl get jobs List Jobs
kubectl get cronjobs List CronJobs
kubectl create job --from=cronjob/<name> <run-name> Trigger a CronJob manually

Services and Networking

Command Description
kubectl get svc List services
kubectl expose deploy/<name> --port=80 --target-port=8080 Create a service for a deployment
kubectl get endpoints List service endpoints
kubectl get ingress List ingress resources
kubectl port-forward svc/<name> 8080:80 Forward a local port to a service
kubectl port-forward pod/<pod> 5000:5000 Forward a local port to a pod
kubectl get networkpolicy List network policies

ConfigMaps and Secrets

Command Description
kubectl get configmap List ConfigMaps
kubectl create configmap <name> --from-file=./config Create a ConfigMap from a file
kubectl create configmap <name> --from-literal=key=value Create a ConfigMap from literals
kubectl get secret List secrets
kubectl create secret generic <name> --from-literal=pass=s3cr3t Create a generic secret
`kubectl get secret -o jsonpath='{.data.pass}' \ base64 -d`

Storage

Command Description
kubectl get pv List PersistentVolumes
kubectl get pvc List PersistentVolumeClaims
kubectl get storageclass List storage classes
kubectl describe pvc <name> Inspect a claim and its binding status

Logs

Command Description
kubectl logs <pod> Print pod logs
kubectl logs <pod> -f Stream pod logs
kubectl logs <pod> -c <container> Logs from a specific container
kubectl logs <pod> --previous Logs from the previously crashed container
kubectl logs -l app=<label> --tail=100 Tail logs across pods by label
kubectl logs deploy/<name> --all-containers Logs from all containers in a deployment

Exec, Attach, and Copy

Command Description
kubectl exec -it <pod> -- sh Open a shell in a pod
kubectl exec <pod> -- env Run a one-off command in a pod
kubectl attach -it <pod> Attach to a running container process
kubectl cp <pod>:/path/file ./file Copy a file out of a pod
kubectl cp ./file <pod>:/path/file Copy a file into a pod

Debugging and Troubleshooting

Command Description
kubectl get events --sort-by=.metadata.creationTimestamp List cluster events oldest first
kubectl describe node <name> Check node conditions and pressure
kubectl top pods Show pod CPU and memory usage
kubectl top nodes Show node CPU and memory usage
kubectl debug <pod> -it --image=busybox Attach an ephemeral debug container
kubectl get pods --field-selector=status.phase=Failed List failed pods only

Nodes and Scheduling

Command Description
kubectl get nodes List nodes
kubectl cordon <node> Mark a node unschedulable
kubectl uncordon <node> Mark a node schedulable again
kubectl drain <node> --ignore-daemonsets Evict pods to prepare for maintenance
kubectl taint nodes <node> key=value:NoSchedule Add a taint to a node

Labels, Annotations, and Selectors

Command Description
kubectl label pod <pod> env=prod Add 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

Command Description
kubectl apply -f manifest.yaml Create or update from a manifest
kubectl apply -f ./dir/ Apply every manifest in a directory
kubectl diff -f manifest.yaml Preview changes before applying
kubectl delete -f manifest.yaml Delete resources defined in a manifest
kubectl replace --force -f manifest.yaml Recreate a resource from a manifest
kubectl kustomize ./overlay Render a kustomize overlay

RBAC and Access

Command Description
kubectl auth can-i create pods Check your own permissions
kubectl auth can-i '*' '*' --as=system:serviceaccount:<ns>:<sa> Check another identity's access
kubectl get roles,rolebindings -A List roles and bindings everywhere
kubectl get clusterrole List cluster roles

Output Formatting and JSONPath

Command Description
kubectl get pod <pod> -o yaml Print the full resource as YAML
kubectl get pod <pod> -o json Print the full resource as JSON
kubectl get pods -o name Print only resource names
kubectl get pods -o jsonpath='{.items[*].metadata.name}' Extract fields with JSONPath
kubectl get pods --sort-by=.status.startTime Sort output by a field
kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName Custom column output
kubectl explain pod.spec.containers Show schema docs for a field

Productivity: Aliases and Completion

Command Description
alias k=kubectl Shorten 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 k Extend completion to the k alias
export KUBECONFIG=~/.kube/config:~/.kube/dev Merge 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.

Top comments (0)