The Springboot-Graceful-Shutdown enables your spring boot application to do a rolling deployment without any downtime on OpenShift. We (SBB) use this in a dockerized Cloud environment on OpenShift - it should also work on Kubernetes. We use here the terminology of OpenShift/Kubernetes: Pods (roughly containers) and Services (logical load balancers that combine N containers). OpenShift needs to know when a Pod is ready to respond to requests. That is done via the readiness probe. As soon as the readiness probe of a Pod fails, OpenShift takes the Pod off the service, so user requests are no longer sent to this Pod.
In Springboot Version 2.3 Graceful shutdown is implemented out of the box. We didn't test it yet, but suggest you to at least try it out, because it wouldn't need the dependency to this project: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#new-and-noteworthy
Here is an example of how the graceful shutdown workflow works:
- OpenShift sends the Pod the SIGTERM signal which tells the Docker instance to shutdown all its processes. This can happen when scaling down a Pod by hand or automatically during a rolling deployment of OpenShift.
- The JVM receives the SIGTERM signal and the graceful shutdown hook sets the readiness probe to false
- The process waits for a defined time to initiate the shutdown of the spring context, e.g. for 20 seconds. This time is needed for OpenShift to detect that the Pod is no longer ready and to remove the pod from the service. The readiness probe check interval must in this case be configured to be less than 20 seconds.
- OpenShift removes the Pod from the Service.
- After the configured wait time, for example 20 seconds, the spring context will be shut down. Open transactions will be properly finished.
- After the Spring Boot application is shutdown, the liveness probe will automatically be set to false.
- Pod is shutdown and removed.
- Add the maven dependency for Spring Boot actuator and this graceful shutdown starters:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>X.X.X</version>
</dependency>
<dependency>
<groupId>ch.sbb</groupId>
<artifactId>springboot-graceful-shutdown</artifactId>
<version>X.X</version>
</dependency>
- Start your application with the alternative method
GracefulshutdownSpringApplication.run
instead ofSpringApplication.run
, e.g.
@SpringBootApplication
public class EstaGracefullshutdownTesterApp {
public static void main(String[] args) {
GracefulshutdownSpringApplication.run(EstaGracefullshutdownTesterApp.class, args);
}
}
- Optionally, adapt the shutdown delay (in your yaml, properties or system property) - the default is 20 seconds:
- Yaml or Properties: estaGracefulShutdownWaitSeconds: 30
- System Property: -DestaGracefulShutdownWaitSeconds=30
Start now your application and point your readiness probe to http://yourhost:8282/health
and set the check interval lower than the shutdown delay, for example, to 10 seconds.
In your developement environment (Eclipse or IntelliJ) you can simulate a SIGTERM: For this use the exit operation (do not terminate it via the stop icon). Then check what
happens with the health check and the shutdown of the spring context.
If you want to implement your own readiness check implement your RestController with the ch.sbb.esta.openshift.gracefullshutdown.IProbeController interface.
- This nice implementation can do a Graceful-Shutdown triggered by REST, JMX or the shell: https://github.com/corentin59/spring-boot-graceful-shutdown
If you do a rolling deployment, the risk of having failing service calls is minimized with this strategy. It is still possible that service calls fail because of the OpenShift internal routing, network issues, or other components between service caller and service host.
If you want to create a new release, you need to do following steps:
- Generate your PGP Keys with Gnugpg:
gpg --gen-key
- Publish your PGP Key to a public server:
gpg --keyserver keyserver.ubuntu.com --send-key YOURKEYID
- Copy the prepared Maven
settings.xml
from this repo to your Maven home directory - Adjust all the user and password configurations in the
settings.xml
- You find the sonatype credentials on the protected password page of the SBB ESTA Team
- Adjust the version in the pom.xml -SNAPSHOT for a snapshot deployment, without the -SNAPSHOT for a release deployment.
- Execute
mvn clean deploy
- If the build was successful, check on oss.sonatype.org whether your version was published
- In case of troubles contact the owner of the repository
Be aware that releasing behind the proxy might not work as the URL is blocked. Publishing a SNAPSHOT publishing worked fine from behind the proxy.
This Shutdownhook was created, maintained and used by the SBB ESTA team in Switzerland.