<p>According to <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-termination" rel="nofollow noreferrer">Termination of Pods</a>, step 7 occurs simultaneously with 3. Is there any way I can prevent this from happening and have 7 occur only <em>after</em> the <code>Pod</code>'s graceful termination (or expiration of the grace period)?</p>
<p>The reason why I need this is that my <code>Pod</code>'s termination routine requires <code>my-service-X.my-namespace.svc.cluster.local</code> to resolve to the <code>Pod</code>'s IP during the whole process, but the corresponding <code>Endpoint</code> gets removed as soon as I run <code>kubectl delete</code> on the <code>Pod</code> / <code>Deployment</code>.</p>
<p><em>Note:</em> In case it helps making this clear, I'm running a bunch of clustered VerneMQ (Erlang) nodes which, on termination, dump their contents to other nodes on the cluster — hence the need for the nodenames to resolve correctly during the whole termination process. Only then should the corresponding <code>Endpoint</code>s be removed.</p>

<p>Unfortunately kubernetes was designed to remove the Pod from the endpoints at the same time as the prestop hook is started (see link in question to <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-termination" rel="nofollow noreferrer">kubernetes docs</a>):</p>
<blockquote>
<p>At the same time as the kubelet is starting graceful shutdown, the
control plane removes that shutting-down Pod from Endpoints</p>
</blockquote>
<p>This <a href="https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-terminating-with-grace" rel="nofollow noreferrer">google kubernetes docs</a> says it even more clearly:</p>
<blockquote>
<ol>
<li>Pod is set to the “Terminating” State and removed from the
endpoints list of all Services</li>
</ol>
</blockquote>
<p>There also was also a <a href="https://github.com/kubernetes/kubernetes/issues/88085" rel="nofollow noreferrer">feature request for that.</a> which was not recognized.</p>
<p><strong>Solution for helm users</strong></p>
<p>But if you are using helm, you can use <a href="https://helm.sh/docs/topics/charts_hooks/" rel="nofollow noreferrer"><em><strong>hooks</strong></em></a> (e.g. pre-delete,pre-upgrade,pre-rollback). Unfortunately this helm hook is an extra pod which can not access all pod resources.</p>
<p>This is an example for a <em>hook</em>:</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: graceful-shutdown-hook
  annotations:
    &quot;helm.sh/hook&quot;: pre-delete,pre-upgrade,pre-rollback
  labels:
    app.kubernetes.io/name: graceful-shutdown-hook
spec:
  template:
    spec:
      containers:
        - name: graceful-shutdown
          image: busybox:1.28.2
          command: ['sh', '-cx', '/bin/sleep 15']
      restartPolicy: Never
  backoffLimit: 0
</code></pre>
