kubectl Commands
Searchable kubectl commands cheat sheet organized by category. Covers basics, Pod operations, deployments, namespaces, and debugging with one-click copy.
Last updated:
How to Use
Expand how to useCollapse how to use
- 1
Search for commands
Enter a command name in the search field, or filter by category (Basic, Pod, Deploy, etc.) to find the command you need.
- 2
Review options and examples
Check the command description, common options, and practical usage examples.
- 3
Copy commands
Click on an example command to copy it to your clipboard for immediate use in your terminal.
kubectl getList one or more resources
Common Options
- -o wide: Show additional columns
- -o yaml: Output as YAML
- -o json: Output as JSON
- -n: Specify namespace
- --all-namespaces: List across all namespaces
Examples
kubectl get podsList all pods in current namespacekubectl get pods -o wideList pods with node and IP infokubectl get all -n my-namespaceList all resources in a namespacekubectl describeShow detailed state of one or more resources
Common Options
- -n: Specify namespace
- --selector: Filter by label selector
Examples
kubectl describe pod my-podDescribe a specific podkubectl describe deployment my-deployDescribe a deploymentkubectl describe node my-nodeDescribe a nodekubectl createCreate a resource from a file or stdin
Common Options
- -f: Filename, directory, or URL
- --dry-run=client: Preview without applying
- -n: Specify namespace
Examples
kubectl create -f pod.yamlCreate resource from filekubectl create deployment my-app --image=nginxCreate a deployment imperativelykubectl create secret generic my-secret --from-literal=key=valueCreate a secretkubectl deleteDelete resources by filenames, stdin, resource names, or label selectors
Common Options
- -f: Delete from file
- --grace-period: Seconds before forced deletion
- --force: Force immediate deletion
- -l: Delete by label selector
Examples
kubectl delete pod my-podDelete a specific podkubectl delete -f deployment.yamlDelete resources defined in filekubectl delete pods -l app=my-appDelete pods by labelkubectl applyApply configuration to a resource by file or stdin
Common Options
- -f: Filename, directory, or URL
- --dry-run=client: Preview without applying
- --prune: Remove resources not in file
- -R: Process directories recursively
Examples
kubectl apply -f deployment.yamlApply configuration from filekubectl apply -f ./manifests/Apply all files in directorykubectl apply -f https://example.com/manifest.yamlApply from URLkubectl editEdit a resource using the default editor
Common Options
- -n: Specify namespace
- -o: Output format before editing
Examples
kubectl edit deployment my-deployEdit a deploymentkubectl edit configmap my-configEdit a ConfigMapkubectl edit svc my-serviceEdit a servicekubectl logsPrint the logs for a container in a pod
Common Options
- -f: Follow log output (stream)
- --tail: Number of lines to show from the end
- -c: Container name in multi-container pod
- --previous: Print logs from previous container instance
- --since: Show logs since duration (e.g. 1h)
Examples
kubectl logs my-podPrint logs of a podkubectl logs my-pod --tail=100Show last 100 log lineskubectl logs my-pod -c my-containerLogs from specific containerkubectl execExecute a command in a container
Common Options
- -it: Interactive with TTY (for shell access)
- -c: Container name in multi-container pod
- --: Separator before the command to run
Examples
kubectl exec -it my-pod -- /bin/bashOpen interactive shell in podkubectl exec my-pod -- ls /appRun a command in podkubectl exec -it my-pod -c sidecar -- shShell into specific containerkubectl port-forwardForward one or more local ports to a pod
Common Options
- --address: Local address to listen on (default 127.0.0.1)
Examples
kubectl port-forward pod/my-pod 8080:80Forward local 8080 to pod port 80kubectl port-forward svc/my-svc 8080:80Forward to service portkubectl port-forward deployment/my-deploy 8080:80Forward to deployment portkubectl cpCopy files and directories to and from containers
Common Options
- -c: Container name in multi-container pod
Examples
kubectl cp my-pod:/app/logs ./logsCopy from pod to localkubectl cp ./config.yaml my-pod:/app/config.yamlCopy from local to podkubectl cp my-pod:/app/logs ./logs -c my-containerCopy from specific containerkubectl attachAttach to a running container
Common Options
- -it: Interactive with TTY
- -c: Container name
Examples
kubectl attach my-pod -itAttach to running pod interactivelykubectl attach my-pod -c my-containerAttach to specific containerkubectl top podDisplay resource usage (CPU/memory) of pods
Common Options
- -n: Specify namespace
- --all-namespaces: Show across all namespaces
- --containers: Show container-level usage
- --sort-by: Sort by cpu or memory
Examples
kubectl top podShow CPU/memory usage of podskubectl top pod --all-namespacesShow usage across all namespaceskubectl top pod --containersShow per-container resource usagekubectl rollout statusShow the status of a rollout
Common Options
- -w: Watch rollout until completion
- --timeout: Maximum time to wait
Examples
kubectl rollout status deployment/my-deployCheck rollout statuskubectl rollout status deployment/my-deploy -wWatch rollout until donekubectl rollout historyView rollout history of a resource
Common Options
- --revision: Show details of a specific revision
Examples
kubectl rollout history deployment/my-deployList rollout historykubectl rollout history deployment/my-deploy --revision=2Show details of revision 2kubectl rollout undoUndo a previous rollout
Common Options
- --to-revision: Rollback to a specific revision
Examples
kubectl rollout undo deployment/my-deployRollback to previous versionkubectl rollout undo deployment/my-deploy --to-revision=2Rollback to specific revisionkubectl scaleSet a new size for a deployment, replica set, or replication controller
Common Options
- --replicas: Number of replicas
- --current-replicas: Precondition check
Examples
kubectl scale deployment my-deploy --replicas=3Scale deployment to 3 replicaskubectl scale deployment my-deploy --replicas=0Scale down to zerokubectl scale statefulset my-sts --replicas=5Scale a StatefulSetkubectl autoscaleAuto-scale a deployment based on CPU usage
Common Options
- --min: Minimum number of replicas
- --max: Maximum number of replicas
- --cpu-percent: Target CPU utilization percentage
Examples
kubectl autoscale deployment my-deploy --min=2 --max=10 --cpu-percent=80Create HPA for deploymentkubectl get hpaList Horizontal Pod Autoscalerskubectl set imageUpdate the image of a pod template
Common Options
- --record: Record the command in the rollout history (deprecated)
Examples
kubectl set image deployment/my-deploy my-container=nginx:1.25Update container imagekubectl set image deployment/my-deploy *=nginx:latestUpdate all containers to latestkubectl get namespaceList all namespaces in the cluster
Common Options
- -o wide: Show additional info
- --show-labels: Show labels
Examples
kubectl get namespaceList all namespaceskubectl get nsList namespaces (short alias)kubectl get ns -o wideList namespaces with detailskubectl create namespaceCreate a new namespace
Common Options
- --dry-run=client: Preview without creating
Examples
kubectl create namespace my-namespaceCreate a namespacekubectl create ns stagingCreate namespace with short aliaskubectl config set-contextSet the current namespace in kubeconfig context
Common Options
- --current: Modify the current context
- --namespace: Default namespace for the context
Examples
kubectl config set-context --current --namespace=my-namespaceSet default namespace for current contextkubectl config set-context my-context --namespace=stagingSet namespace for a specific contextkubectl config viewDisplay the kubeconfig settings
Common Options
- --minify: Remove unused context info
- --flatten: Flatten merged kubeconfig
Examples
kubectl config viewShow full kubeconfigkubectl config view --minifyShow only current context configkubectl config use-contextSet the current-context in a kubeconfig file
Examples
kubectl config use-context my-clusterSwitch to a contextkubectl config use-context prod-clusterSwitch to production cluster contextkubectl config get-contextsDescribe one or many contexts
Common Options
- --no-headers: Omit header line
- -o name: Print only context names
Examples
kubectl config get-contextsList all available contextskubectl config get-contexts -o nameList context names onlykubectl config current-contextDisplay the current-context
Examples
kubectl config current-contextShow currently active contextkubectl logs --followStream logs from a container in real time
Common Options
- --tail: Number of lines to show initially
- --since: Show logs since duration (e.g. 5m)
- -c: Container name
- --prefix: Prefix each log line with pod/container
Examples
kubectl logs -f my-podStream logs from a podkubectl logs -f my-pod --tail=50Stream logs from last 50 lineskubectl logs -f -l app=my-appStream logs from pods with labelkubectl get eventsList events in a namespace for debugging
Common Options
- -n: Specify namespace
- --sort-by: Sort by .metadata.creationTimestamp or .lastTimestamp
- --field-selector: Filter by field
Examples
kubectl get eventsList events in current namespacekubectl get events --sort-by=.lastTimestampList events sorted by timekubectl get events --field-selector involvedObject.name=my-podEvents for a specific podkubectl top nodeDisplay resource usage (CPU/memory) of nodes
Common Options
- --sort-by: Sort by cpu or memory
Examples
kubectl top nodeShow CPU/memory usage of nodeskubectl top node --sort-by=memoryShow nodes sorted by memory usagekubectl debugCreate debugging sessions for pods and nodes
Common Options
- --image: Container image for the debug container
- --target: Container to debug
- -it: Interactive with TTY
Examples
kubectl debug -it my-pod --image=busyboxDebug pod with ephemeral containerkubectl debug node/my-node -it --image=ubuntuDebug a nodekubectl debug my-pod --copy-to=debug-pod --image=busyboxCreate a debugging copy of podkubectl runRun a particular image in the cluster
Common Options
- --image: Container image to run
- --rm: Delete pod after it exits
- -it: Interactive with TTY
- --restart: Restart policy (Never for one-off pods)
Examples
kubectl run test --image=busybox --rm -it --restart=Never -- shRun a temporary debugging podkubectl run nginx --image=nginxRun nginx podkubectl patchUpdate fields of a resource using strategic merge, JSON merge, or JSON patch
Common Options
- --type: Patch type (strategic, merge, json)
- -p: Patch content
Examples
kubectl patch deployment my-deploy -p '{"spec":{"replicas":3}}'Patch deployment replicaskubectl patch node my-node -p '{"spec":{"unschedulable":true}}'Patch node to unschedulablekubectl labelUpdate the labels on a resource
Common Options
- --overwrite: Allow overwriting existing labels
- -l: Select resources by label
Examples
kubectl label pod my-pod env=productionAdd label to a podkubectl label pod my-pod env-Remove label from a podkubectl label pods -l app=my-app version=v2Label all matching podskubectl annotateUpdate the annotations on a resource
Common Options
- --overwrite: Allow overwriting existing annotations
Examples
kubectl annotate pod my-pod description='My app pod'Add annotation to a podkubectl annotate deployment my-deploy description-Remove annotation from deploymentkubectl taintUpdate the taints on one or more nodes
Common Options
- --overwrite: Allow overwriting existing taints
Examples
kubectl taint node my-node key=value:NoScheduleTaint a node to prevent schedulingkubectl taint node my-node key:NoSchedule-Remove a taint from a nodekubectl drainDrain node in preparation for maintenance
Common Options
- --ignore-daemonsets: Ignore DaemonSet-managed pods
- --delete-emptydir-data: Delete emptyDir data
- --force: Continue even if there are non-managed pods
Examples
kubectl drain my-node --ignore-daemonsetsDrain a node for maintenancekubectl drain my-node --ignore-daemonsets --delete-emptydir-dataDrain node including emptyDir podskubectl cordonMark node as unschedulable
Examples
kubectl cordon my-nodePrevent new pods from being scheduledkubectl uncordon my-nodeRe-enable scheduling on the nodekubectl apply --dry-runPreview changes without applying them to the cluster
Common Options
- --dry-run=client: Local validation only
- --dry-run=server: Server-side validation
- -f: Filename or directory
Examples
kubectl apply --dry-run=client -f deployment.yamlPreview apply changes (client-side)kubectl apply --dry-run=server -f deployment.yamlPreview apply changes (server-side)kubectl diff -f deployment.yamlShow diff against live stateAbout kubectl Commands
kubectl Commands is a cheat sheet compiling frequently used Kubernetes CLI commands. It comprehensively covers commands needed for cluster management, from basic resource operations to Pod debugging, deployment rollouts, and advanced node management. Each command includes descriptions, common options, and practical examples that can be copied with one click.
Key Features
- Collection of 37+ essential kubectl commands
- Options and usage examples for each command
- Category filtering (basic, pod, deploy, namespace, etc.)
- Real-time search functionality
- One-click copy of examples
Use Cases
- Looking up kubectl get, describe, logs, and exec syntax during debugging on a cluster
- Referencing deployment rollout and rollback commands during a production incident
- Checking namespace and resource management commands while managing a multi-tenant cluster
- Learning Kubernetes as a developer transitioning from Docker Compose to Kubernetes
- Verifying syntax for advanced operations like kubectl drain, cordon, or debug
- Using as a quick reference during a Helm deployment or CI/CD pipeline setup
FAQ
What's the difference between kubectl apply and kubectl create?
kubectl create creates a resource and fails if it already exists. kubectl apply creates or updates a resource declaratively, making it idempotent. apply is preferred for managing resources with YAML files.
How do I switch between Kubernetes clusters?
Use kubectl config use-context to switch contexts. You can list all available contexts with kubectl config get-contexts. Each context typically corresponds to a different cluster or namespace.
What's the difference between kubectl delete and kubectl drain?
kubectl delete removes a specific resource immediately. kubectl drain gracefully evicts all pods from a node (respecting PodDisruptionBudgets) and marks it unschedulable, preparing it for maintenance.
How do I debug a pod that is CrashLoopBackOff?
Use kubectl logs my-pod --previous to see logs from the previous (crashed) container instance. kubectl describe pod my-pod shows events and conditions. kubectl debug creates an ephemeral container for deeper inspection.
What's the difference between kubectl exec and kubectl attach?
kubectl exec runs a new process in the container (e.g. a shell). kubectl attach connects to the existing main process of the container, useful for interacting with programs that read stdin.
