Kubectl Reference
kubectl is the CLI for interacting with a Kubernetes cluster. Commands follow the pattern:
kubectl <verb> <resource> [name] [flags]Getting Information
Section titled “Getting Information”kubectl get pods # list pods in default namespacekubectl get pods -n staging # list pods in "staging" namespacekubectl get pods -A # list pods in all namespaceskubectl get pods -o wide # extra columns (node, IP)kubectl get pods -o yaml # full YAML outputkubectl get pods -l app=web # filter by label
kubectl get deploymentskubectl get serviceskubectl get nodeskubectl get namespaceskubectl get all # pods, services, deployments, replicasetsInspecting Resources
Section titled “Inspecting Resources”kubectl describe pod my-pod # detailed info, events, conditionskubectl describe deployment my-appkubectl describe node node-1
kubectl get pod my-pod -o yaml # full spec + statuskubectl explain deployment.spec # documentation for a fieldkubectl logs my-pod # logs from the podkubectl logs my-pod -c my-container # specific container in a multi-container podkubectl logs my-pod --tail=50 # last 50 lineskubectl logs my-pod -f # follow (stream) logskubectl logs -l app=web # logs from all pods matching labelExec (Run Commands in a Pod)
Section titled “Exec (Run Commands in a Pod)”kubectl exec my-pod -- ls /app # run a commandkubectl exec -it my-pod -- /bin/sh # interactive shellkubectl exec -it my-pod -c app -- bash # specific containerCreating and Applying
Section titled “Creating and Applying”kubectl apply -f deployment.yaml # create or update from filekubectl apply -f ./k8s/ # apply all files in a directorykubectl apply -f https://example.com/manifest.yaml # from URL
kubectl create namespace staging # create imperativelykubectl create configmap my-config --from-literal=KEY=valuekubectl create secret generic my-secret --from-literal=PASSWORD=abc123Editing and Patching
Section titled “Editing and Patching”kubectl edit deployment my-app # open in editor, save to applykubectl patch deployment my-app -p '{"spec":{"replicas":5}}'kubectl set image deployment/my-app app=nginx:1.26 # update imageDeleting
Section titled “Deleting”kubectl delete pod my-podkubectl delete -f deployment.yaml # delete resources defined in filekubectl delete deployment my-appkubectl delete namespace staging # deletes everything in the namespaceScaling
Section titled “Scaling”kubectl scale deployment my-app --replicas=5kubectl autoscale deployment my-app --min=2 --max=10 --cpu-percent=80Context and Namespace
Section titled “Context and Namespace”kubectl config get-contexts # list available clusters/contextskubectl config use-context my-cluster # switch contextkubectl config set-context --current --namespace=staging # set default namespaceDebugging
Section titled “Debugging”kubectl get events # cluster events (scheduling, errors)kubectl get events --sort-by='.lastTimestamp'kubectl top pods # CPU/memory usage (requires metrics-server)kubectl top nodeskubectl run debug --image=busybox -it --rm -- /bin/sh # temporary debug podFor a full production triage sequence, see Troubleshooting and Debugging.
Useful Flags
Section titled “Useful Flags”| Flag | Description |
|---|---|
-n <namespace> | Target a specific namespace |
-A / --all-namespaces | All namespaces |
-o wide | Extra columns |
-o yaml / -o json | Full output |
-l key=value | Filter by label |
--watch / -w | Watch for changes |
--dry-run=client -o yaml | Generate YAML without creating |