OCPi
OCPi

Reputation: 346

Gracefully shutdown Spring application running in Kubernetes

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" ]

Upvotes: 4

Views: 2044

Answers (2)

apisim
apisim

Reputation: 4586

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

Upvotes: 3

P Ekambaram
P Ekambaram

Reputation: 17621

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

Upvotes: 1

Related Questions