<p>We have a Spring Boot (2.0.4) application exposing a number of endpoints, one of which enables clients to retrieve sometimes very large files (~200 GB). The application is exposed in a Pod via a Kubernetes deployment configured with the rolling-update strategy. </p>

<p>When we update our deployment by setting the image to the latest version the pods get destroyed and new ones spun up. Our service provision is seamless for new requests. However current requests can and do get severed and this can be annoying for clients in the middle of downloading very large files.</p>

<p>We can configure Container Lifecycle Pre-Stop hooks in our deployment spec to inject a pause before sending shutdown signals to the app via it's PID. This helps prevent any new traffic going to pods which have been set to Terminate. Is there a way to then pause the application shutdown process until all current requests have been completed (this may take tens of minutes)?</p>

<p>Here's what we have tried from within the Spring Boot application:</p>

<ul>
<li><p>Implementing a shutdown listener which intercepts <code>ContextCloseEvents</code>; unfortunately we can't reliably retrieve a list of active requests. Any Actuator metrics which may have been useful are unavailable at this stage of the shutdown process. </p></li>
<li><p>Count active sessions by implementing a <code>HttpSessionListener</code> and overriding <code>sessionCreated/Destroy</code> methods to update a counter. This fails because the methods are not invoked on a separate thread so always report the same value in the shutdown listener.</p></li>
</ul>

<p>Any other strategy we should try? From within the app itself, or the container, or directly through Kubernetes resource descriptors? Advice/Help/Pointers would be much appreciated.</p>

<p>Edit: We manage the cluster so we're only trying to mitigate service outages to currently connected clients during a <em>managed update of our deployment via a modified pod spec</em> </p>

<p>You could increase the <code>terminationGracePeriodSeconds</code>, the default is 30 seconds. But unfortunately, there's nothing to prevent a cluster admin from force deleting your pod, and there's all sorts of reasons the whole node could go away. </p>
