Tools › Cheat sheets › Containers & DevOps › kubectl
☸️

kubectl cheat sheet

kubectl talks to a Kubernetes cluster. Add -n NAMESPACE to target a namespace, or -A for all namespaces. Confirm your context before running anything that changes state.

Click any command to copy it.

Context and namespace

CommandWhat it does
kubectl config get-contextsList contexts (clusters).
kubectl config current-contextShow the active context.
kubectl config use-context NAMESwitch cluster.
kubectl config set-context --current --namespace=NAMESet the default namespace for this context.
kubectl get nsList namespaces.

Inspect

CommandWhat it does
kubectl get pods -APods in all namespaces.
kubectl get pods -o wideInclude node and IP.
kubectl get deploy,svc,ingSeveral resource types at once.
kubectl get allCommon resources in the namespace.
kubectl describe pod NAMEDetailed state and recent events for a pod.
kubectl get events --sort-by=.lastTimestampRecent cluster events, newest last.
kubectl top podsCPU and memory use (needs metrics-server).
kubectl get nodesList nodes and their status.
kubectl get pod NAME -o yamlFull definition as YAML.

Logs and debugging

CommandWhat it does
kubectl logs -f pod/NAMEFollow a pod's logs.
kubectl logs pod/NAME -c CONTAINERLogs from a specific container.
kubectl logs pod/NAME --previousLogs from the previous, crashed container.
kubectl exec -it pod/NAME -- shOpen a shell in a pod.
kubectl port-forward pod/NAME 8080:80Forward a local port to a pod.
kubectl run test --image=busybox -it --rm -- shStart a temporary pod for debugging.
kubectl cp NAME:/path ./localCopy files from a pod (use NAMESPACE/NAME:/path for another namespace).

Apply and change

CommandWhat it does
kubectl apply -f file.yamlCreate or update resources from a file.
kubectl delete -f file.yamlDestructive: Delete the resources defined in a file.
kubectl delete pod NAMEDestructive: Delete a pod (a Deployment will recreate it).
kubectl scale deploy/NAME --replicas=3Change the number of replicas.
kubectl edit deploy/NAMEEdit a resource in your editor.
kubectl apply -f file.yaml --dry-run=clientValidate without changing anything.
kubectl create deploy web --image=nginx --dry-run=client -o yamlGenerate YAML to start from.

Rollouts and nodes

CommandWhat it does
kubectl rollout status deploy/NAMEWatch a rollout until it finishes.
kubectl rollout restart deploy/NAMERestart all pods of a deployment gradually.
kubectl rollout undo deploy/NAMERoll back to the previous revision.
kubectl rollout history deploy/NAMEShow revision history.
kubectl cordon NODEMark a node unschedulable.
kubectl drain NODE --ignore-daemonsetsDestructive: Evict pods from a node to prepare for maintenance.
kubectl auth can-i create podsCheck whether you have a permission.

Frequently asked questions

Why is my pod in CrashLoopBackOff?

The container keeps exiting. Check kubectl logs pod/NAME --previous for the crash output and kubectl describe pod NAME for events such as failing probes or out-of-memory kills.

How do I read a secret value?

Run kubectl get secret NAME -o jsonpath='{.data.KEY}' | base64 -d. Remember that Secrets are only base64-encoded, not encrypted, by default.

More Containers & DevOps cheat sheets

🌿 Git🐳 Docker🅰️ Ansible🏗️ TerraformAll Containers & DevOps cheat sheets →
Copied to clipboard