From 7f9b4c6ff62073e6cfbb78840a36d6be2a0655d4 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Thu, 30 Jan 2025 10:22:25 +0100 Subject: [PATCH] Document Kubernetes' preStop sleep Closes gh-43830 --- .../how-to/pages/deployment/cloud.adoc | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/deployment/cloud.adoc b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/deployment/cloud.adoc index 9874a470bb..0780827212 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/deployment/cloud.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/how-to/pages/deployment/cloud.adoc @@ -124,7 +124,22 @@ Because this shutdown processing happens in parallel (and due to the nature of d You can configure a sleep execution in a preStop handler to avoid requests being routed to a pod that has already begun shutting down. This sleep should be long enough for new requests to stop being routed to the pod and its duration will vary from deployment to deployment. -The preStop handler can be configured by using the PodSpec in the pod's configuration file as follows: + +If you're using Kubernetes 1.32 or up, the preStop handler can be configured by using the PodSpec in the pod's configuration file as follows: + +[source,yaml] +---- +spec: + containers: + - name: "example-container" + image: "example-image" + lifecycle: + preStop: + sleep: + seconds: 10 +---- + +If you're not on Kubernetes 1.32 yet, you can use an `exec` command to invoke `sleep`. [source,yaml] ---- @@ -138,6 +153,8 @@ spec: command: ["sh", "-c", "sleep 10"] ---- +NOTE: The container needs to have a shell for this to work. + Once the pre-stop hook has completed, SIGTERM will be sent to the container and xref:reference:web/graceful-shutdown.adoc[graceful shutdown] will begin, allowing any remaining in-flight requests to complete. NOTE: When Kubernetes sends a SIGTERM signal to the pod, it waits for a specified time called the termination grace period (the default for which is 30 seconds).