Skip to main content
Toolsbase Logo

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 use
  1. 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. 2

    Review options and examples

    Check the command description, common options, and practical usage examples.

  3. 3

    Copy commands

    Click on an example command to copy it to your clipboard for immediate use in your terminal.

kubectl get
List 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 pods
List all pods in current namespace
kubectl get pods -o wide
List pods with node and IP info
kubectl get all -n my-namespace
List all resources in a namespace

kubectl describe
Show detailed state of one or more resources

Common Options

  • -n: Specify namespace
  • --selector: Filter by label selector

Examples

kubectl describe pod my-pod
Describe a specific pod
kubectl describe deployment my-deploy
Describe a deployment
kubectl describe node my-node
Describe a node

kubectl create
Create 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.yaml
Create resource from file
kubectl create deployment my-app --image=nginx
Create a deployment imperatively
kubectl create secret generic my-secret --from-literal=key=value
Create a secret

kubectl delete
Delete 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-pod
Delete a specific pod
kubectl delete -f deployment.yaml
Delete resources defined in file
kubectl delete pods -l app=my-app
Delete pods by label

kubectl apply
Apply 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.yaml
Apply configuration from file
kubectl apply -f ./manifests/
Apply all files in directory
kubectl apply -f https://example.com/manifest.yaml
Apply from URL

kubectl edit
Edit a resource using the default editor

Common Options

  • -n: Specify namespace
  • -o: Output format before editing

Examples

kubectl edit deployment my-deploy
Edit a deployment
kubectl edit configmap my-config
Edit a ConfigMap
kubectl edit svc my-service
Edit a service

kubectl logs
Print 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-pod
Print logs of a pod
kubectl logs my-pod --tail=100
Show last 100 log lines
kubectl logs my-pod -c my-container
Logs from specific container

kubectl exec
Execute 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/bash
Open interactive shell in pod
kubectl exec my-pod -- ls /app
Run a command in pod
kubectl exec -it my-pod -c sidecar -- sh
Shell into specific container

kubectl port-forward
Forward 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:80
Forward local 8080 to pod port 80
kubectl port-forward svc/my-svc 8080:80
Forward to service port
kubectl port-forward deployment/my-deploy 8080:80
Forward to deployment port

kubectl cp
Copy files and directories to and from containers

Common Options

  • -c: Container name in multi-container pod

Examples

kubectl cp my-pod:/app/logs ./logs
Copy from pod to local
kubectl cp ./config.yaml my-pod:/app/config.yaml
Copy from local to pod
kubectl cp my-pod:/app/logs ./logs -c my-container
Copy from specific container

kubectl attach
Attach to a running container

Common Options

  • -it: Interactive with TTY
  • -c: Container name

Examples

kubectl attach my-pod -it
Attach to running pod interactively
kubectl attach my-pod -c my-container
Attach to specific container

kubectl top pod
Display 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 pod
Show CPU/memory usage of pods
kubectl top pod --all-namespaces
Show usage across all namespaces
kubectl top pod --containers
Show per-container resource usage

kubectl rollout status
Show the status of a rollout

Common Options

  • -w: Watch rollout until completion
  • --timeout: Maximum time to wait

Examples

kubectl rollout status deployment/my-deploy
Check rollout status
kubectl rollout status deployment/my-deploy -w
Watch rollout until done

kubectl rollout history
View rollout history of a resource

Common Options

  • --revision: Show details of a specific revision

Examples

kubectl rollout history deployment/my-deploy
List rollout history
kubectl rollout history deployment/my-deploy --revision=2
Show details of revision 2

kubectl rollout undo
Undo a previous rollout

Common Options

  • --to-revision: Rollback to a specific revision

Examples

kubectl rollout undo deployment/my-deploy
Rollback to previous version
kubectl rollout undo deployment/my-deploy --to-revision=2
Rollback to specific revision

kubectl scale
Set 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=3
Scale deployment to 3 replicas
kubectl scale deployment my-deploy --replicas=0
Scale down to zero
kubectl scale statefulset my-sts --replicas=5
Scale a StatefulSet

kubectl autoscale
Auto-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=80
Create HPA for deployment
kubectl get hpa
List Horizontal Pod Autoscalers

kubectl set image
Update 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.25
Update container image
kubectl set image deployment/my-deploy *=nginx:latest
Update all containers to latest

kubectl get namespace
List all namespaces in the cluster

Common Options

  • -o wide: Show additional info
  • --show-labels: Show labels

Examples

kubectl get namespace
List all namespaces
kubectl get ns
List namespaces (short alias)
kubectl get ns -o wide
List namespaces with details

kubectl create namespace
Create a new namespace

Common Options

  • --dry-run=client: Preview without creating

Examples

kubectl create namespace my-namespace
Create a namespace
kubectl create ns staging
Create namespace with short alias

kubectl config set-context
Set 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-namespace
Set default namespace for current context
kubectl config set-context my-context --namespace=staging
Set namespace for a specific context

kubectl config view
Display the kubeconfig settings

Common Options

  • --minify: Remove unused context info
  • --flatten: Flatten merged kubeconfig

Examples

kubectl config view
Show full kubeconfig
kubectl config view --minify
Show only current context config

kubectl config use-context
Set the current-context in a kubeconfig file

Examples

kubectl config use-context my-cluster
Switch to a context
kubectl config use-context prod-cluster
Switch to production cluster context

kubectl config get-contexts
Describe one or many contexts

Common Options

  • --no-headers: Omit header line
  • -o name: Print only context names

Examples

kubectl config get-contexts
List all available contexts
kubectl config get-contexts -o name
List context names only

kubectl config current-context
Display the current-context

Examples

kubectl config current-context
Show currently active context

kubectl logs --follow
Stream 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-pod
Stream logs from a pod
kubectl logs -f my-pod --tail=50
Stream logs from last 50 lines
kubectl logs -f -l app=my-app
Stream logs from pods with label

kubectl get events
List 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 events
List events in current namespace
kubectl get events --sort-by=.lastTimestamp
List events sorted by time
kubectl get events --field-selector involvedObject.name=my-pod
Events for a specific pod

kubectl top node
Display resource usage (CPU/memory) of nodes

Common Options

  • --sort-by: Sort by cpu or memory

Examples

kubectl top node
Show CPU/memory usage of nodes
kubectl top node --sort-by=memory
Show nodes sorted by memory usage

kubectl debug
Create 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=busybox
Debug pod with ephemeral container
kubectl debug node/my-node -it --image=ubuntu
Debug a node
kubectl debug my-pod --copy-to=debug-pod --image=busybox
Create a debugging copy of pod

kubectl run
Run 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 -- sh
Run a temporary debugging pod
kubectl run nginx --image=nginx
Run nginx pod

kubectl patch
Update 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 replicas
kubectl patch node my-node -p '{"spec":{"unschedulable":true}}'
Patch node to unschedulable

kubectl label
Update the labels on a resource

Common Options

  • --overwrite: Allow overwriting existing labels
  • -l: Select resources by label

Examples

kubectl label pod my-pod env=production
Add label to a pod
kubectl label pod my-pod env-
Remove label from a pod
kubectl label pods -l app=my-app version=v2
Label all matching pods

kubectl annotate
Update 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 pod
kubectl annotate deployment my-deploy description-
Remove annotation from deployment

kubectl taint
Update the taints on one or more nodes

Common Options

  • --overwrite: Allow overwriting existing taints

Examples

kubectl taint node my-node key=value:NoSchedule
Taint a node to prevent scheduling
kubectl taint node my-node key:NoSchedule-
Remove a taint from a node

kubectl drain
Drain 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-daemonsets
Drain a node for maintenance
kubectl drain my-node --ignore-daemonsets --delete-emptydir-data
Drain node including emptyDir pods

kubectl cordon
Mark node as unschedulable

Examples

kubectl cordon my-node
Prevent new pods from being scheduled
kubectl uncordon my-node
Re-enable scheduling on the node

kubectl apply --dry-run
Preview 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.yaml
Preview apply changes (client-side)
kubectl apply --dry-run=server -f deployment.yaml
Preview apply changes (server-side)
kubectl diff -f deployment.yaml
Show diff against live state

About 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.