2 min read | by Jordi Prats
If we need to investigate why a container keeps restarting, a good place to start is taking a look at the logs when it crashes. Since Kubernetes will automatically restart a failed container, if we use kubectl logs we will get the logs of the restarted container instead of the one that have crashed:
$ kubectl logs ampa-7d98c84675-dpzjw -c ampa
[2021-10-15 14:20:41 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2021-10-15 14:20:41 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2021-10-15 14:20:41 +0000] [1] [INFO] Using worker: sync
[2021-10-15 14:20:41 +0000] [8] [INFO] Booting worker with pid: 8
How can we retrieve the logs of the previous container?
We can add the flag --previous to kubectl logs to retrieve them:
$ kubectl logs ampa-7d98c84675-dpzjw -c ampa --previous
[2021-10-15 14:20:22 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2021-10-15 14:20:22 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2021-10-15 14:20:22 +0000] [1] [INFO] Using worker: sync
[2021-10-15 14:20:22 +0000] [8] [INFO] Booting worker with pid: 8
[2021-10-15 14:20:34 +0000] [1] [INFO] Handling signal: term
[2021-10-15 14:20:36 +0000] [8] [INFO] Worker exiting (pid: 8)
[2021-10-15 14:20:37 +0000] [1] [INFO] Shutting down: Master
On this example I have sent a TERM signal to restart the container, but otherwise the logs might be helpful
Posted on 18/10/2021