Gracefully shutdown Spring application running in Kubernetes

11/18/2019

I've implemented graceful shutdown logic in my Spring application and it works locally if I send a SIGTERM to the Java process.

However when I'm running it in Kubernetes if I delete the pod or deploy a new one, the logic is not running. First I thought that it's sending SIGKILL instead of SIGTERM but as I've researched, the Docker CMD gets the SIGTERM but does not delegate it to the application. How should I run it correctly?

Right now I'm using this command in my Dockerfile:

CMD [ "java", "-jar", "/app.jar" ]
-- OCPi
docker
java
kubernetes
spring

2 Answers

11/19/2019

You could try dumb-init or something similar. The README at the given link elaborates a bit on "Why you need an init system".

-- apisim
Source: StackOverflow

11/18/2019

You can make use of container handlers for graceful shutdown. k8s supports post-start and pre-stop hooks as container handlers. call your logic in prestop hook

-- P Ekambaram
Source: StackOverflow