I have a pod in a state of CrashLoopBackOff
, the logs I'm seeing from kubectl logs <pod-name> -p
present only a partial picutre. Other logs are found in other files (e.g. /var/log/something/something.log
).
Since this pod is crashed, I can't kubectl exec
into a shell there and look at the files.
How can I look at the log files produced by a container that is no longer running?
To be more specific, I'm looking for log files file under $HOME/logs/es.log
(in the container that failed)
Have you tried the
--previous
flag?It's like
I was so frustrated from finding no solution to this seemingly common problem that I built a docker image that tails log files and sends them to stdout, to be used as a sidecar container.
Here's what I did:
emptyDir{}
to the podmountPath
being the directory to which it writes the logs tolutraman/logger-sidecar:v2
), and mounted the same volume to/logs
(I programmed the script to read the logs from this directory)then, all the logs written to that directory, can be accessed by
kubectl logs <pod-name> -c logger
Here is an example yaml:
For anyone who is interested, here is how I made the sidecar container:
Dockerfile:
addTail:
logtrack.sh:
Basically you have several options here.
If you want to proceed as is with you setup,
you can access files of exited container from the host where the container run.
Find out the worker the container is on:
Then if you have the access to this node (e.g. via shh) you can find the container:
If you don't have ssh access to the node you can use plugins like this one: https://github.com/kvaps/kubectl-enter
But generally this is not the best practice
You shouldn't write logs into files, instead your app should write in into stdout/stderr, then it would be much easier to debug