Skip to content
Latchkey

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

  • POD or -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=200

Common 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →