kubectl logs: Capture Container Logs in CI
Print logs from a pod container, including from a crashed previous instance.
kubectl logs streams or dumps a container's stdout/stderr. In CI it is the primary way to surface why an app failed to start: combine --previous to read a crashed container and -l to grab logs across a deployment.
Common flags
PODor-l key=value- a pod, or all pods matching a label-c CONTAINER- pick a container in a multi-container pod--previous- logs from the last terminated instance--tail=N/--since=DURATION- limit output
Example in CI
Dump the last 200 lines from a crashed container.
shell
kubectl logs -l app=api -n prod --previous --tail=200Common errors in CI
- previous terminated container ... not found - pod never restarted, so no previous logs
- container "X" in pod ... is waiting to start: ContainerCreating - logs not available yet
- a container name must be specified for pod ... - multi-container pod needs -c
Key takeaways
- Dumps container logs; the first place to look when a pod fails.
- Use --previous to read the logs of a crashed-and-restarted container.
- Multi-container pods require -c to disambiguate.
Related guides
kubectl describe: Debug Resources in CIShow detailed resource state and events in CI: usage, a debugging example, and the errors that indicate the w…
kubectl get pods: Inspect Pods in CIList and inspect pods in CI: output and selector flags, a scripting example, and the errors that point to acc…
kubectl exec: Run Commands in Pods in CIExecute a command inside a running pod from CI: container and stdin flags, a migration example, and the error…