The day-to-day kubectl commands: get, describe, apply, diff, logs, exec, port-forward, top, explain and generating YAML with dry-run.
Pattern: kubectl <verb> <resource> [name] [-n ns] [-o wide|yaml|json]
| Task | Command |
|---|---|
| List things | kubectl get pods,svc,deploy -A |
| Details + events | kubectl describe pod <name> |
| Apply manifests | kubectl apply -f app.yaml (or -f dir/, -k overlay/) |
| Preview changes | kubectl diff -f app.yaml |
| Delete | kubectl delete -f app.yaml |
| Logs | kubectl logs -f deploy/api --tail=100 |
| Logs of crashed run | kubectl logs <pod> --previous |
| Shell into pod | kubectl exec -it <pod> -- sh |
| Port-forward | kubectl port-forward svc/api 8080:80 |
| Copy files | kubectl cp <pod>:/tmp/dump.txt ./dump.txt |
| Watch | kubectl get pods -w |
| Resource usage | kubectl top pods (needs metrics-server) |
| Explain a field | kubectl explain deployment.spec.strategy |
| Filter by label | kubectl get pods -l app=api |
| Events | kubectl get events --sort-by=.lastTimestamp |
Always check the current context before delete on shared clusters: kubectl config current-context