Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
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.
Step by step
- 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- 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 / reason | Meaning |
|---|---|
Error, code 1 | The app ended with an error — read the log. |
OOMKilled, 137 | Memory limit reached — the log often ends without an error. |
Completed, 0 | The process ended normally (a server must not end). |
137 without OOM | Killed by SIGKILL — often a failed liveness probe. |
143 | SIGTERM — a normal stop. |
- The useful options:
| Option | Use |
|---|---|
-p, --previous | Log of the crashed run. |
-c NAME | One container (needed when there are several). |
--all-containers | All containers of the Pod. |
-l app=api + --prefix | All Pods of an app; each line shows its Pod. |
--since=1h, --since-time=… | Only a time window. |
--tail=200, -f | Last lines / follow live. |
--timestamps | Add the time to each line. |
- Init container failed? Read its log by name:
kubectl logs api-6d8f9c7b5d-x2kq9 -n shop -c migrate- 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- 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.
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=apiCommon problems
| Problem | Cause | Fix |
|---|---|---|
a container name must be specified | The Pod has several containers. | Add -c NAME or --all-containers. |
previous terminated container … not found | The container did not restart yet (no previous run). | Use the normal kubectl logs. |
Log ends with no error, reason OOMKilled | The kernel killed it — no time to log. | Raise the memory limit; check with kubectl top pod. |
| Logs of last week are missing | Node 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 empty | The app writes to a file, not to stdout. | Log to stdout/stderr; or read the file with kubectl exec … cat. |
Go further
- Command pages: kubectl.
- Next how-tos: Find why a Pod does not start, Open a shell in a running Pod, See the logs of a container (Docker).
- Learn it: CKA — Troubleshoot Pods, kubectl debug in daily work.
- Central logs: Loki, Fluent Bit, Elasticsearch.
- Practise: Lab: troubleshooting drill.
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.