Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 32Kubernetes · kubectl

How to get the logs of a crashed Pod

The Pod restarted, and kubectl logs shows only the fresh start — the error is gone. Kubernetes keeps the log of the last run of each container, and with the right options you also see sidecars, init containers, all replicas of a Deployment and a time window. This how-to shows all of them, how to read the exit reason, and what to do when the Pod is already deleted.

  • 5 Lessons
  • 1 Commands

$ run as a normal user# run as root (or with sudo)

32.1

The quick answer

kubectl logs api-6d8f9c7b5d-x2kq9 -n shop --previous
kubectl logs api-6d8f9c7b5d-x2kq9 -n shop -c api --previous --tail=100
kubectl logs deploy/api -n shop --since=30m --timestamps
kubectl logs -n shop -l app=api --all-containers --prefix --tail=50

--previous (-p) = the run before the current one, the one that crashed.

32.2

Step by step

  1. Find the right Pod and container — a Pod can have several containers (app, sidecar, init):
kubectl get pod api-6d8f9c7b5d-x2kq9 -n shop -o jsonpath='{.spec.initContainers[*].name}{" | "}{.spec.containers[*].name}{"\n"}'
migrate | api istio-proxy
  1. Read why it stopped (exit code and reason of the last run):
kubectl get pod api-6d8f9c7b5d-x2kq9 -n shop -o jsonpath='{range .status.containerStatuses[*]}{.name}{"  "}{.restartCount}{"  "}{.lastState.terminated.reason}{"  "}{.lastState.terminated.exitCode}{"\n"}{end}'
api  6  OOMKilled  137
Exit code / reasonMeaning
Error, code 1The app ended with an error — read the log.
OOMKilled, 137Memory limit reached — the log often ends without an error.
Completed, 0The process ended normally (a server must not end).
137 without OOMKilled by SIGKILL — often a failed liveness probe.
143SIGTERM — a normal stop.
  1. The useful options:
OptionUse
-p, --previousLog of the crashed run.
-c NAMEOne container (needed when there are several).
--all-containersAll containers of the Pod.
-l app=api + --prefixAll Pods of an app; each line shows its Pod.
--since=1h, --since-time=…Only a time window.
--tail=200, -fLast lines / follow live.
--timestampsAdd the time to each line.
  1. Init container failed? Read its log by name:
kubectl logs api-6d8f9c7b5d-x2kq9 -n shop -c migrate
  1. The app dies too fast to look inside? Start a copy with a shell instead of the app command:
kubectl debug api-6d8f9c7b5d-x2kq9 -n shop -it --copy-to=api-debug --container=api -- sh
kubectl delete pod api-debug -n shop
  1. The Pod is deleted? Its logs are gone from the node. Only a central log system (Loki, Elasticsearch, a cloud log service) still has them — set one up before you need it.

32.3

Check that it worked

You found the line with the error (or the OOMKilled reason). After the fix the restart count stops growing:

kubectl get pods -n shop -l app=api

32.4

Common problems

ProblemCauseFix
a container name must be specifiedThe Pod has several containers.Add -c NAME or --all-containers.
previous terminated container … not foundThe container did not restart yet (no previous run).Use the normal kubectl logs.
Log ends with no error, reason OOMKilledThe kernel killed it — no time to log.Raise the memory limit; check with kubectl top pod.
Logs of last week are missingNode log rotation and Pod deletion remove them.Central logging (Loki, Fluent Bit + Elasticsearch).
Error from server (Forbidden)RBAC: no pods/log right.Ask for a Role with get on pods/log.
Logs are emptyThe app writes to a file, not to stdout.Log to stdout/stderr; or read the file with kubectl exec … cat.

32.5

Go further

1Which option shows the log of the run that crashed?

--previous (-p).

2The last state says OOMKilled, exit code 137. What is the fix?

The container hit its memory limit: raise resources.limits.memory or fix the memory use.

All commands in this chapter