5

Is there any recommended way to gracefully shutdown a Spring:boot 2 app in Kubernetes.

  1. Catch a termination signal SIGTERM
  2. Tell Tomcat to stop taking new requests. (or Jetty, Undertow or Netty/WebFlux depending on the embedded web server used). Or tell SCS to stop sending/listening for messages on Kafka.
  3. Tell Actuator health endpoint to go SERVICE_UNAVAILABLE (503)
  4. And then after a X seconds shutdown the application or (SIGKILL)

I'm trying to do a graceful shutdown Rest apps and SCS (kafka consumer&producer) apps

DarVar
  • 16,882
  • 29
  • 97
  • 146

1 Answers1

0

If you are on the latest version of spring-boot i.e. 2.3.5.RELEASE, then all you need to do it add the below properties to the application.properties file and you are done.

server.shutdown=graceful spring.lifecycle.timeout-per-shutdown-phase=30s

In Kubernetes world, you can use the preStop hook. But use this when you actually want a hold before SIGTERM is initiated.

This hook is called immediately before a container is terminated. No parameters are passed to the handler. This event handler is blocking, and must complete before the call to delete the container is sent to the Docker daemon. The SIGTERM notification sent by Docker is also still sent. A more complete description of termination behavior can be found in Termination of Pods.