<p>In a project, I'm enabling the cluster autoscaler functionality from Kubernetes.</p>
<p>According to the documentation: <a href="https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/FAQ.md#how-does-scale-down-work" rel="nofollow noreferrer">How does scale down work</a>, I understand that when a node is used for a given time less than 50% of its capacity, then it is removed, together with all of its pods, which will be replicated in a different node if needed.</p>
<p>But the following problem can happen: what if all the pods related to a specific deployment are contained in a node that is being removed? That would mean users might experience downtime for the application of this deployment.</p>
<p>Is there a way to avoid that the scale down deletes a node whenever there is a deployment which only contains pods running on that node?</p>
<p>I have checked the documentation, and one possible (but not good) solution, is to add an annotation to all of the pods containing applications <a href="https://github.com/kubernetes/autoscaler/blob/master/cluster-autoscaler/FAQ.md#what-types-of-pods-can-prevent-ca-from-removing-a-node" rel="nofollow noreferrer">here</a>, but this clearly would not down scale the cluster in an optimal way.</p>

<p>In the same documentation:</p>
<blockquote>
<p>What happens when a non-empty node is terminated? As mentioned above, all pods should be migrated elsewhere. Cluster Autoscaler does this by evicting them and tainting the node, so they aren't scheduled there again.</p>
</blockquote>
<p>What is the <a href="https://kubernetes.io/docs/tasks/administer-cluster/safely-drain-node/#the-eviction-api" rel="nofollow noreferrer">Eviction</a> ?:</p>
<blockquote>
<p>The eviction subresource of a pod can be thought of as a kind of policy-controlled DELETE operation on the pod itself.</p>
</blockquote>
<p>Ok, but what if all pods get evicted at the same time on the node?
You can use Pod Disruption Budget to make sure minimum replicas are always working:</p>
<p>What is PDB?:</p>
<blockquote>
<p>A PDB limits the number of Pods of a replicated application that are down simultaneously from voluntary disruptions.</p>
</blockquote>
<p>In <a href="https://kubernetes.io/docs/tasks/run-application/configure-pdb/#specifying-a-poddisruptionbudget" rel="nofollow noreferrer">k8s docs</a> you can also read:</p>
<blockquote>
<p>A PodDisruptionBudget has three fields:</p>
<p>A label selector .spec.selector to specify the set of pods to which it applies. This field is required.</p>
<p><code>.spec.minAvailable which is a description of the number of pods from that set that must still be available after the eviction</code>, even in the absence of the evicted pod. minAvailable can be either an absolute number or a percentage.</p>
<p>.spec.maxUnavailable (available in Kubernetes 1.7 and higher) which is a description of the number of pods from that set that can be unavailable after the eviction. It can be either an absolute number or a percentage.</p>
</blockquote>
<p>So if you use PDB for your deployment it should not get deleted all at once.</p>
<p>But please notice that if the node fails for some other reason (e.g hardware failure), you will still experience downtime. If you really care about High Availability consider using pod antiaffinity to make sure the pods are not scheduled all on one node.</p>
