How to see logs of terminated pods

ghz 1years ago ⋅ 8797 views

Question

I am running selenium hubs and my pods are getting terminated frequently. I would like to look at the logs of the pods which are terminated. How to do it?

NAME                                               READY     STATUS              RESTARTS   AGE
chrome-75-0-0e5d3b3d-3580-49d1-bc25-3296fdb52666   0/2       Terminating         0          49s
chrome-75-0-29bea6df-1b1a-458c-ad10-701fe44bb478   0/2       Terminating         0          23s
chrome-75-0-8929d8c8-1f7b-4eba-96f2-918f7a0d77f5   0/2       ContainerCreating   0          7s

kubectl logs chrome-75-0-8929d8c8-1f7b-4eba-96f2-918f7a0d77f5
Error from server (NotFound): pods "chrome-75-0-8929d8c8-1f7b-4eba-96f2-918f7a0d77f5" not found
$ kubectl logs chrome-75-0-8929d8c8-1f7b-4eba-96f2-918f7a0d77f5 --previous
Error from server (NotFound): pods "chrome-75-0-8929d8c8-1f7b-4eba-96f2-918f7a0d77f5" not found

Answer

Running kubectl logs -p will fetch logs from existing resources at API level. This means that terminated pods' logs will be unavailable using this command.

As mentioned in other answers, the best way is to have your logs centralized via [logging agents or directly pushing these logs into an external service](https://kubernetes.io/docs/concepts/cluster- administration/logging/#cluster-level-logging-architectures).

Alternatively and given the [logging architecture in Kubernetes](https://kubernetes.io/docs/concepts/cluster- administration/logging/#logging-at-the-node-level), you might be able to [fetch the logs directly from the log-rotate files](https://kubernetes.io/docs/concepts/cluster- administration/logging/#system-component-logs) in the node hosting the pods. However, this option might depend on the Kubernetes implementation as log files might be deleted when the pod eviction is triggered.