kubectl commands
73 commands, each with a one-line explanation. Click a command to copy it, or its title for details.
Commands 73 shown
-
kubectl get pods -A -o wideAll pods with node placement and IPs
-
kubectl describe pod <name> -n <ns>Full details: events, conditions, volume mounts
-
kubectl logs -f <pod> -n <ns>Stream logs in real time; add -c <container> for multi-container pods
-
kubectl logs <pod> --previous -n <ns>Logs from the last crashed/restarted container instance
-
kubectl exec -it <pod> -n <ns> -- bashInteractive shell inside a running container
-
kubectl port-forward svc/<name> 8080:80 -n <ns>Tunnel a cluster service port to localhost
-
kubectl rollout status deployment/<name> -n <ns>Block until rollout completes or times out (use in CI)
-
kubectl rollout undo deployment/<name> -n <ns>Revert to the previous revision
-
kubectl scale deployment/<name> --replicas=3 -n <ns>Immediately change replica count
-
kubectl get events -A --sort-by='.lastTimestamp'Recent cluster events, oldest first — great for post-incident review
-
kubectl apply --server-side --dry-run=server -f file.yamlValidate manifest against live API without applying changes
-
kubectl get deployment/<name> -n <ns> -o yamlCurrent live state with managed fields stripped; useful as a base
-
kubectl delete pod <name> -n <ns> --grace-period=0 --forceRemove a Terminating pod immediately (use with caution)
-
kubectl cp <ns>/<pod>:/remote/path ./local-pathExtract a file from a running container
-
kubectl cordon <node>Mark node as unschedulable — no new pods land here
-
kubectl drain <node> --ignore-daemonsets --delete-emptydir-dataGracefully evict all pods before maintenance
-
kubectl edit deployment/<name> -n <ns>Open resource in $EDITOR; changes applied on save
-
kubectl auth can-i list pods --as=system:serviceaccount:<ns>:<sa> -n <ns>Check what a ServiceAccount is allowed to do
-
kubectl get pods -n <ns> -o custom-columns='NAME:.metadata.name,NODE:.spec.nodeName,STATUS:.status.phase'Print only the fields you care about without jq
-
kubectl patch deployment <name> -n <ns> -p '{"spec":{"replicas":2}}'Inline JSON patch without editing the whole manifest
-
kubectl annotate pod <pod> -n <ns> key=value --overwriteSet or update an annotation; --overwrite replaces existing value
-
kubectl label node <node> role=worker --overwriteAdd or change a label on any resource
-
kubectl taint nodes <node> key=value:NoSchedulePrevent pods without matching toleration from scheduling on node
-
kubectl taint nodes <node> key=value:NoSchedule-Remove a taint by appending a dash at the end
-
kubectl set image deployment/<name> <container>=image:tag -n <ns>Rolling-update the container image without editing YAML
-
kubectl rollout history deployment/<name> -n <ns>Show all revisions with change cause (requires --record or annotations)
-
kubectl rollout restart deployment/<name> -n <ns>Trigger zero-downtime pod rotation without changing spec
-
kubectl create namespace <ns>Create a new namespace; idempotent with --dry-run=client | apply
-
kubectl set resources deployment/<name> -c <container> --requests=cpu=100m,memory=128Mi --limits=cpu=500m,memory=512Mi -n <ns>Update resource requests and limits on a running deployment
-
kubectl run tmp --image=nicolaka/netshoot -it --rm --restart=Never -n <ns> -- bashEphemeral pod for debugging; deleted on exit
-
kubectl get nodes --show-labelsSee all labels assigned to each node
-
kubectl uncordon <node>Re-enable scheduling on a previously cordoned node
-
kubectl get hpa -AHorizontal Pod Autoscaler current/desired replicas and metrics
-
kubectl describe resourcequota -n <ns>Used vs hard limits for CPU, memory, object counts in namespace
-
kubectl get pdb -AShow min-available and current healthy pod counts
-
kubectl describe limitrange -n <ns>Default and max resource limits applied to pods in namespace
-
kubectl get crds --sort-by='.metadata.creationTimestamp'List all Custom Resource Definitions sorted by creation time
-
kubectl get endpoints <svc> -n <ns>Show pod IPs backing a Service — diagnose selector mismatch
-
kubectl get certificate -A -o wideSee cert-manager Certificate ready status and expiry dates
-
kubectl get pod <pod> -n <ns> -o jsonpath='{.spec.topologySpreadConstraints}'Inspect topology spread constraints on a pod
-
kubectl apply -f ./dir/ --prune -l app=<label> -n <ns>Apply manifests and remove stale objects matching the label selector
-
kubectl diff -f file.yamlShow what would change if you applied this manifest now
-
kubectl get pods -n <ns> -l app=<label> -o name | xargs -I{} kubectl exec {} -n <ns> -- <cmd>Run a command in every pod matching a label selector
-
kubectl get pods -n <ns> -wLive-refresh pod list as statuses change
-
kubectl top pods -A --sort-by=cpuCPU-sorted pod usage across all namespaces
-
kubectl create token <sa-name> -n <ns> --duration=1hGenerate a short-lived bound service account token
-
kubectl auth can-i --list -n <ns>Show all permissions the current user has in a namespace
-
kubectl delete pods --all -n <ns>Force recreation of all pods — useful after ConfigMap changes
-
kubectl get ingress -AAll Ingress resources with hosts and addresses
-
kubectl get pods -n <ns> -o yaml > pods-dump.yamlBackup/inspect entire pod state in a namespace
-
kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\t"}{range .spec.containers[*]}{.image}{"\n"}{end}{end}'Enumerate every container image running in the cluster
-
kubectl config use-context <context>Switch the active cluster context
-
kubectl config get-contextsShow all contexts in current kubeconfig
-
KUBECONFIG=~/.kube/config:~/.kube/cluster2.yaml kubectl config view --flatten > ~/.kube/merged.yamlCombine multiple kubeconfig files into one
-
kubectl config set-context --current --namespace=<ns>Avoid typing -n on every command for this context
-
kubectl cluster-infoShow API server and CoreDNS endpoints
-
kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{": "}{range .status.conditions[*]}{.type}={.status},{" "}{end}{"\n"}{end}'Show all conditions per node (Ready, MemoryPressure, DiskPressure)
-
kubectl create job --from=cronjob/<name> manual-run -n <ns>Immediately run a CronJob as a one-off Job
-
kubectl wait pod -l app=<label> -n <ns> --for=condition=Ready --timeout=120sBlock until matching pods are Ready or timeout
-
kubectl get --raw /metrics | grep apiserver_request_totalPull raw Prometheus metrics from the apiserver without a metrics stack
-
kubectl get --raw '/healthz/etcd?verbose'Verify etcd connectivity through the apiserver health endpoint
-
kubectl get --raw '/apis/metrics.k8s.io/v1beta1/nodes' | jq '.items[] | {name:.metadata.name, cpu:.usage.cpu, mem:.usage.memory}'Raw node usage from metrics API; bypasses kubectl top formatting
-
kubectl debug <pod> -n <ns> --copy-to=<pod>-dbg --share-processes --image=nicolaka/netshoot -- sleep infinityClone pod with a debug container sharing the PID namespace; original stays untouched
-
kubectl explain deploy.spec --recursive | lessDump the full nested field tree of a resource spec; great for hunting valid fields
-
kubectl rollout pause deploy/<name> -n <ns>; kubectl rollout resume deploy/<name> -n <ns>Batch multiple edits without triggering rollouts, then resume to apply once
-
kubectl apply view-last-applied deploy/<name> -n <ns> -o yamlShow the last-applied-configuration annotation; diff intent vs live state
-
kubectl get events -A --field-selector type=Warning --sort-by=.lastTimestampCluster-wide Warning events newest last; fast triage of failing workloads
-
kubectl get pods -A --field-selector status.phase=FailedList pods in Failed phase across namespaces for cleanup or investigation
-
kubectl wait --for=jsonpath='{.status.loadBalancer.ingress[0].ip}' svc/<name> -n <ns> --timeout=120sBlock in CI until a Service gets its external IP; works on any jsonpath field
-
kubectl auth can-i list secrets -n <ns> --as=<name> --as-group=system:serviceaccountsTest RBAC as another identity without their kubeconfig; needs impersonate verb
-
kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\t"}{.status.containerStatuses[0].restartCount}{"\n"}{end}' | sort -k3 -rn | headSurface the most-restarting pods to catch crashloops fast
-
kubectl get nodes -o json | jq -r '.items[] | {node:.metadata.name, capCPU:.status.capacity.cpu, allocCPU:.status.allocatable.cpu, capMem:.status.capacity.memory, allocMem:.status.allocatable.memory}'Show reserved overhead per node; gap = kube/system reservations
-
kubectl create configmap <name> --from-env-file=app.env -n <ns> --dry-run=client -o yaml | kubectl apply -f -; kubectl patch deploy/<name> -n <ns> --type=json -p='[{"op":"add","path":"/spec/template/spec/containers/0/envFrom/-","value":{"configMapRef":{"name":"<name>"}}}]'Build a CM from KEY=VALUE file, then JSON-patch it into a container's envFrom
no commands match — try a different search