Spring Boot 1.X elegant way to achieve a shutdown

Currently deployed on k8s the springboot application notice the following way through RollingUpdate update the application:

A new pod up after health check and a check will be removed after the old pod can receive traffic. However, when you remove the old pod, although there is no new traffic into the old pod, java old pod in the process and some business is not dealt with directly kill off.

Therefore, we need to do when the new pod up, old pod does not receive new traffic, but already you can remove the old pod after the request processing flow processed.

In fact, k8s for the pod itself is a terminationGracePeriodSeconds of configuration items (default is 30 seconds). When RollingUpdate update the application, a new pod up and after a health check (if configured health check), will inform k8s terminate a pod. k8s following termination of the life cycle of pod (of 5):

  1. Pod Terminating state is set, and deleted from the service / endpoint list of k8s. At this point, Pod stop to get new traffic. Running in the Pod in the container will not be affected.
  2. preStop Hook is executed (if configured in the deployment yaml, no default configuration). preStop Hook Pod is to send a special command in the container or Http request.
  3. K8S SIGTERM signal transmitted to the Pod in the container. This signal allows the container to know that they will soon be closed.
    SIGTERM more friendly, the process can catch this signal to close the program according to your needs. Before closing the program, you can open the log file and end the task being done. In some cases, if the process is under way and the job can not be interrupted, then the process may even ignore SIGTERM signal. With respect to kill $ {pid} commands.
    For SIGKILL signal, the process can not be ignored. This is a '' I do not care what you are doing, stop right now "signal. If you send SIGKILL to a process,
    FreeBSD will stop that process there. With respect to kill -9 $ {pid} command
    when SpringBoot java process applied to capture the signal SIGTERM themselves will start an orderly shutdown.
  4. K8S wait elegant terminated (waiting time terminationGracePeriod

Guess you like

Origin blog.csdn.net/luliuliu1234/article/details/103738633