Kubernetes / kubectl Cheatsheet

Operate clusters from the command line. Most commands take -n <namespace> and --context <name>.

1 credit

Inspect

5 items
All resources in ns
kubectl get all -n <ns>
Pods wide (show node)
kubectl get pods -o wide
Describe (events + spec)
kubectl describe pod <name>
YAML export
kubectl get deploy <n> -o yaml
Cluster-wide resources
kubectl api-resources --namespaced=false

Logs & shell

5 items
Logs (follow, last 100)
kubectl logs -f --tail=100 <pod>
Logs of crashed container
kubectl logs <pod> -c <container> --previous
Exec into container
kubectl exec -it <pod> -- sh
Port-forward local → pod
kubectl port-forward svc/web 8080:80
Copy files
kubectl cp <pod>:/path/in/pod ./local

Apply & scale

5 items
Apply manifest
kubectl apply -f deploy.yaml
Apply dir recursively
kubectl apply -R -f ./k8s
Scale deployment
kubectl scale deploy/web --replicas=5
Rolling restart
kubectl rollout restart deploy/web
Rollback
kubectl rollout undo deploy/web

Context & namespace

3 items
List contexts
kubectl config get-contexts
Switch context
kubectl config use-context <name>
Default namespace
kubectl config set-context --current --namespace=<ns>

Further reading