Fetch logs from containers in Kubernetes pods.
low risk
kubectl logs streams container logs from pods. The -c flag targets a specific container in multi-container pods. --previous shows logs from the last terminated container — essential for debugging crashes. -f follows live.
| Flag | Meaning |
|---|---|
-f --follow | Stream logs in real time |
-c --container | Specific container in multi-container pod |
--previous | Print logs from previously terminated container |
--tail | Lines of recent log to show |
--since | Show logs since duration (e.g., 1h) |
-l --selector | Stream logs from all pods matching label |
kubectl logs -f mypod -n productionFollow live pod logs · explain & risk-check →kubectl logs mypod --previousLogs from the crashed previous container · explain & risk-check →kubectl logs -l app=nginx --all-containers --tail=50Last 50 lines from all nginx pods · explain & risk-check →kubectl logs deployment/myapp --since=1h | grep ERRORRecent errors from a deployment · explain & risk-check →Use `kubectl logs --previous` immediately when a pod restarts — the current container starts fresh, and crash logs disappear if you wait too long (especially with short log rotation).
Reference text is hand-written and may be incomplete — check man kubectl for your version. Test destructive commands on a non-production system first.