<p>I have a node.js container running on kubernetes that handles websocket connections that will normally be kept open until the user quits. Now when I do a rolling update or when the deployment scales down, is there a way to stop the pod from being killed until the last user has disconnected? </p>

<p>At the moment, Kubernetes does not support graceful connection closing.  </p>

<p><code>terminationGracePeriodSeconds</code> only define the timer before the Pod termination. It doesn't take care of Pod connections. </p>

<p>The only way to deal with rolling updates is to adapt your application to be able to switch clients to other pods transparently.  </p>

<p>You may also want to take a look at <a href="https://kubernetes.io/docs/concepts/workloads/pods/disruptions/" rel="nofollow noreferrer">Disruption Budget</a>. It may help in some cases to have more reliable setup.</p>

<blockquote>
  <p>A PDB specifies the number of replicas that an application can
  tolerate having, relative to how many it is intended to have. For
  example, a Deployment which has a .spec.replicas: 5 is supposed to
  have 5 pods at any given time. If its PDB allows for there to be 4 at
  a time, then the Eviction API will allow voluntary disruption of one,
  but not two pods, at a time.</p>
  
  <p>PDBs cannot prevent <a href="https://kubernetes.io/docs/concepts/workloads/pods/disruptions/#voluntary-and-involuntary-disruptions" rel="nofollow noreferrer">involuntary disruptions</a> from occurring, but
  they do count against the budget.</p>
  
  <p>Pods which are deleted or unavailable due to a rolling upgrade to an
  application do count against the disruption budget, but <em>controllers</em>
  (like <em>deployment</em> and <em>stateful-set</em>) are not limited by PDBs when doing
  rolling upgrades – the handling of failures during application updates
  is configured in the controller spec. (Learn about updating a
  <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#updating-a-deployment" rel="nofollow noreferrer">deployment</a>.)</p>
  
  <p>When a pod is evicted using the eviction API, it is gracefully
  terminated (see <code>terminationGracePeriodSeconds</code> in <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#podspec-v1-core" rel="nofollow noreferrer">PodSpec</a>.)</p>
</blockquote>
