Kubernetes: print log before being OOMKilled

8/31/2019

I noticed sometimes my containers are OOMKilled, but I'd like to print some logs before exiting. Is there a way that I can intercept the signal in my entrypoint script?

-- Dagang
kubernetes

2 Answers

9/1/2019

Nevertheless the answer from DavidPi is already accepted, I don’t think it will work. For more info you can check that question - Analyze Kubernetes pod OOMKilled, but I will add some info here. Unfortunately you cannot handle OOM event somewhere inside Kubernetes or your app. Kubernetes doesn’t manage memory limits itself, it just set settings for runtime below which actually execute and manage your payload. Events from an answer above will allow you to get event when K8s generate them, but not when something else do it. In case of OOM, Kubernetes will get information about that event after your app will be already killed and container will be stoped, and you will not be available to run any code in your container on that event because it will be already stopped.

-- Anton Kostenko
Source: StackOverflow

9/2/2019

Please follow this Kubernetes best practices: terminating with grace

To be sure - how it works in your example please perform some tests, you can combine your solution with extending terminationGracePeriodSeconds period please follow - Soft Eviction Thresholds

Note:

If not specified, the kubelet kills Pods immediately with no graceful termination.

Hope this help.

-- Hanx
Source: StackOverflow