Skip to content

Kubectl Reference

First PublishedLast UpdatedByAtif Alam

kubectl is the CLI for interacting with a Kubernetes cluster. Commands follow the pattern:

Terminal window
kubectl <verb> <resource> [name] [flags]
Terminal window
kubectl get pods # list pods in default namespace
kubectl get pods -n staging # list pods in "staging" namespace
kubectl get pods -A # list pods in all namespaces
kubectl get pods -o wide # extra columns (node, IP)
kubectl get pods -o yaml # full YAML output
kubectl get pods -l app=web # filter by label
kubectl get deployments
kubectl get services
kubectl get nodes
kubectl get namespaces
kubectl get all # pods, services, deployments, replicasets
Terminal window
kubectl describe pod my-pod # detailed info, events, conditions
kubectl describe deployment my-app
kubectl describe node node-1
kubectl get pod my-pod -o yaml # full spec + status
kubectl explain deployment.spec # documentation for a field
Terminal window
kubectl logs my-pod # logs from the pod
kubectl logs my-pod -c my-container # specific container in a multi-container pod
kubectl logs my-pod --tail=50 # last 50 lines
kubectl logs my-pod -f # follow (stream) logs
kubectl logs -l app=web # logs from all pods matching label
Terminal window
kubectl exec my-pod -- ls /app # run a command
kubectl exec -it my-pod -- /bin/sh # interactive shell
kubectl exec -it my-pod -c app -- bash # specific container
Terminal window
kubectl apply -f deployment.yaml # create or update from file
kubectl apply -f ./k8s/ # apply all files in a directory
kubectl apply -f https://example.com/manifest.yaml # from URL
kubectl create namespace staging # create imperatively
kubectl create configmap my-config --from-literal=KEY=value
kubectl create secret generic my-secret --from-literal=PASSWORD=abc123
Terminal window
kubectl edit deployment my-app # open in editor, save to apply
kubectl patch deployment my-app -p '{"spec":{"replicas":5}}'
kubectl set image deployment/my-app app=nginx:1.26 # update image
Terminal window
kubectl delete pod my-pod
kubectl delete -f deployment.yaml # delete resources defined in file
kubectl delete deployment my-app
kubectl delete namespace staging # deletes everything in the namespace
Terminal window
kubectl scale deployment my-app --replicas=5
kubectl autoscale deployment my-app --min=2 --max=10 --cpu-percent=80
Terminal window
kubectl config get-contexts # list available clusters/contexts
kubectl config use-context my-cluster # switch context
kubectl config set-context --current --namespace=staging # set default namespace
Terminal window
kubectl get events # cluster events (scheduling, errors)
kubectl get events --sort-by='.lastTimestamp'
kubectl top pods # CPU/memory usage (requires metrics-server)
kubectl top nodes
kubectl run debug --image=busybox -it --rm -- /bin/sh # temporary debug pod

For a full production triage sequence, see Troubleshooting and Debugging.

FlagDescription
-n <namespace>Target a specific namespace
-A / --all-namespacesAll namespaces
-o wideExtra columns
-o yaml / -o jsonFull output
-l key=valueFilter by label
--watch / -wWatch for changes
--dry-run=client -o yamlGenerate YAML without creating