<p>We run a kubernetes cluster provisioned with kubespray and discovered that each time when a faulty node goes down (we had this due to hardware issue recently) the pods executing on this node stuck in Terminating state indefinitely. Even after many hours the pods are not being redeploying on healthy nodes and thus our entire application is malfunctioning and the users are affected for a prolonged period of time.</p>
<p>How it is possible to configure kubernetes to perform failover in situations like this?</p>
<p>Below is our statefulset manifest.</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: project-stock
  name: ps-ra
spec:
  selector:
    matchLabels:
      infrastructure: ps
      application: report-api
      environment: staging
  serviceName: hl-ps-report-api
  replicas: 1
  template:
    metadata:
      namespace: project-stock
      labels:
        infrastructure: ps
        application: report-api
        environment: staging
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: ps-report-api
          image: localhost:5000/ps/nodejs-chrome-application:latest
          ports:
            - containerPort: 3000
              protocol: TCP
              name: nodejs-rest-api
          volumeMounts:
          resources:
            limits:
              cpu: 1000m
              memory: 8192Mi
            requests:
              cpu: 333m
              memory: 8192Mi
          livenessProbe:
            httpGet:
              path: /health/
              port: 3000
            initialDelaySeconds: 180
            periodSeconds: 10
            failureThreshold: 12
            timeoutSeconds: 10
</code></pre>

roman
<p>Posted community wiki for better visibility. Feel free to expand it.</p>
<hr />
<p>In my opinion, the behaviour on your <code>kubespray</code> cluster (pod staying in <code>Terminating</code> state) is fully intentional. Based on <a href="https://kubernetes.io/docs/tasks/run-application/force-delete-stateful-set-pod/" rel="nofollow noreferrer">Kubernetes documentation</a>:</p>
<blockquote>
<p>A Pod is not deleted automatically when a node is unreachable. The Pods running on an unreachable Node enter the 'Terminating' or 'Unknown' state after a <a href="https://kubernetes.io/docs/concepts/architecture/nodes/#condition" rel="nofollow noreferrer">timeout</a>. Pods may also enter these states when the user attempts graceful deletion of a Pod on an unreachable Node.</p>
</blockquote>
<p>The same documentation introduces ways in which a Pod in <code>Terminating</code> state can be removed. Also there are some recommended best practices:</p>
<blockquote>
<p>The only ways in which a Pod in such a state can be removed from the apiserver are as follows:</p>
<ul>
<li>The Node object is deleted (either by you, or by the  <a href="https://kubernetes.io/docs/concepts/architecture/nodes/" rel="nofollow noreferrer">Node Controller</a>).</li>
<li>The kubelet on the unresponsive Node starts responding, kills the Pod and removes the entry from the apiserver.</li>
<li>Force deletion of the Pod by the user.</li>
</ul>
</blockquote>
<blockquote>
<p>The recommended best practice is to use the first or second approach. If a Node is confirmed to be dead (e.g. permanently disconnected from the network, powered down, etc), then delete the Node object. If the Node is suffering from a network partition, then try to resolve this or wait for it to resolve. When the partition heals, the kubelet will complete the deletion of the Pod and free up its name in the apiserver. Normally, the system completes the deletion once the Pod is no longer running on a Node, or the Node is deleted by an administrator. You may override this by force deleting the Pod.</p>
</blockquote>
<p>You can implement <a href="https://kubernetes.io/blog/2021/04/21/graceful-node-shutdown-beta/" rel="nofollow noreferrer">Graceful Node Shutdown</a> if your node is shutdown in one of the <a href="https://kubernetes.io/blog/2021/04/21/graceful-node-shutdown-beta/" rel="nofollow noreferrer">following ways</a>:</p>
<blockquote>
<p>On Linux, your system can shut down in many different situations. For example:</p>
<ul>
<li>A user or script running  <code>shutdown -h now</code>  or  <code>systemctl poweroff</code>  or  <code>systemctl reboot</code>.</li>
<li>Physically pressing a power button on the machine.</li>
<li>Stopping a VM instance on a cloud provider, e.g.  <code>gcloud compute instances stop</code>  on GCP.</li>
<li>A Preemptible VM or Spot Instance that your cloud provider can terminate unexpectedly, but with a brief warning.</li>
</ul>
</blockquote>
<p>Keep in mind this feature is supported from version <strong>1.20</strong> (where it is in alpha state) and up (currently in <strong>1.21</strong> is in beta state).</p>
<p>The other solution, mentioned in documentation is to manually delete a node, for example using a <code>kubectl delete node &lt;your-node-name&gt;</code>:</p>
<blockquote>
<p>If a Node is confirmed to be dead (e.g. permanently disconnected from the network, powered down, etc), then delete the Node object.</p>
</blockquote>
<p>Then pod will be re-scheduled on the other node.</p>
<p>The last workaround is to set <code>TerminationGracePeriodSeconds</code> to <code>0</code>, but this is <a href="https://kubernetes.io/docs/tasks/run-application/force-delete-stateful-set-pod/" rel="nofollow noreferrer">strongly discouraged</a>:</p>
<blockquote>
<p>For the above to lead to graceful termination, the Pod <strong>must not</strong> specify a <code>pod.Spec.TerminationGracePeriodSeconds</code> of 0. The practice of setting a <code>pod.Spec.TerminationGracePeriodSeconds</code> of 0 seconds is unsafe and strongly discouraged for StatefulSet Pods. Graceful deletion is safe and will ensure that the Pod <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-termination" rel="nofollow noreferrer">shuts down gracefully</a> before the kubelet deletes the name from the apiserver.</p>
</blockquote>
