<p>This question is about graceful shutdowns in Kubernetes.</p>
<p>I have a set of servers that are eventually consistent and requires each of them to communicate with each other constantly, even if some servers are terminating.</p>
<p>Since the servers communicate via HTTP requests, I need to ensure that the terminating server needs to be able to continue to receive HTTP requests in order to pass on relevant information to its peers.</p>
<p>Based on my understanding, there will be a few things that will happen in parallel when a server is shutting down: it will be removed from endpoints list for service and that if there is a preStop hook it will be run. Originally I thought if I use a preStop handler and perform a sleep command then I can let the server continue to server traffic. But after some experimentation I realized that the server will stop serving traffic even before the sleep ends.</p>
<p>I was wondering if there are any ways I can configure the servers to keep receiving traffic while it's shutting down.</p>

<blockquote>
<p>Is there a way to still be able to reach the server while it's gracefully shutting down?</p>
</blockquote>
<p>Yes, configure your Service with:</p>
<pre class="lang-yaml prettyprint-override"><code>spec.publishNotReadyAddresses: true
</code></pre>
<p>Then your pods will receive traffic even though they are in a <code>Terminating</code> state, as the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#servicespec-v1-core" rel="nofollow noreferrer">Kubernetes docs</a> explain:</p>
<blockquote>
<p>publishNotReadyAddresses, when set to true, indicates that DNS implementations must publish the notReadyAddresses of subsets for the Endpoints associated with the Service. The default value is false. The primary use case for setting this field is to use a StatefulSet's Headless Service to propagate SRV records for its Pods without respect to their readiness for purpose of peer discovery.</p>
</blockquote>
<p>Note that <code>spec.publishNotReadyAddresses</code> <a href="https://github.com/kubernetes/kubernetes/pull/63742" rel="nofollow noreferrer">only works in Kubernetes v1.11+</a>.
In older clusters, use the <code>service.alpha.kubernetes.io/tolerate-unready-endpoints: &quot;true&quot;</code> annotation to get a <a href="https://github.com/kubernetes/kubernetes/pull/61056#issuecomment-378061680" rel="nofollow noreferrer">similar behaviour</a>.</p>
