<p>I am reading <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-configurable-scaling-behavior" rel="nofollow noreferrer">following documentation</a>. I want to understand how to use <code>behavior</code> </p>

<blockquote>
  <p>One or more scaling policies can be specified in the behavior section
  of the spec. When multiple policies are specified the policy which
  allows the highest amount of change is the policy which is selected by
  default. The following example shows this behavior while scaling down:</p>
</blockquote>

<pre><code>behavior:
  scaleDown:
    policies:
    - type: Pods
      value: 4
      periodSeconds: 60
    - type: Percent
      value: 10
      periodSeconds: 60
</code></pre>

<blockquote>
  <p>When the number of pods is more than 40 the second policy will be used
  for scaling down. For instance if there are 80 replicas and the target
  has to be scaled down to 10 replicas then during the first step 8
  replicas will be reduced. In the next iteration when the number of
  replicas is 72, 10% of the pods is 7.2 but the number is rounded up to
  8. On each loop of the autoscaler controller the number of pods to be change is re-calculated based on the number of current replicas. When
  the number of replicas falls below 40 the first policy(Pods) is
  applied and 4 replicas will be reduced at a time. ...</p>
</blockquote>

<p>I understood that when autoscaler decides to scale down - it reduces amount of replics by 10% portions because of rule:</p>

<pre><code>- type: Percent
  value: 10
  periodSeconds: 60
</code></pre>

<p>but I can't understand the meaning of the first rule. Could you please clarify ?<br>
How is it possible to have 80 nodes when we have such config?  How is it possible to have 80 nodes if kubernetes starts to to reduce amount of nodes each time when we have > 40 nodes</p>

<blockquote>
  <p>How is it possible to have 80 nodes when we have such config? </p>
</blockquote>

<p>The example (in documentation you referring to) says about 80 Pods, but that is just a random number to illustrate a particular example. </p>

<blockquote>
  <p>How is it possible to have 80 nodes if kubernetes starts to to reduce amount of nodes each time when we have > 40 nodes</p>
</blockquote>

<p>Not each time. It starts reducing amount of Pods only if/when Autoscaler decides to scale down.</p>

<blockquote>
  <p>I understood that when autoscaler decides to scale down - it reduces amount of replics by 10% portions</p>
</blockquote>

<p>You are right that the documentation describes the case when there were 80 Pods and due to lack of load it is time to "deflate" cluster to lower amount of Pods.</p>

<blockquote>
  <p>but I can't understand the meaning of the first rule. Could you please clarify ?</p>
</blockquote>

<p>Lets go through it one more time.</p>

<pre><code>behavior:
  scaleDown:
    policies:
    - type: Pods
      value: 4
      periodSeconds: 60
    - type: Percent
      value: 10
      periodSeconds: 60
</code></pre>

<p>You can see two policies here. Namely <code>Pods</code> and <code>Percent</code>. When multiple policies are specified the policy which allows the highest amount of change is the policy which is selected by default.</p>

<p>That is why in case there are 80 pods and system has to downscale to let's say 10, it checks how many pods it can shutdown with each policy.</p>

<p><code>Pods</code> allow to shutdown only 4 pods at a time, while <code>Percent</code> 80Pods*10Percent=8Pods.  The <code>Percent</code> wins, 8 Pods shut down, 72 Pods left and process starts again. </p>

<p>This time <code>Percent</code> allows to shutdown 72*0.1=7.2 Pods. Still more than 4. The value is rounded up to 8. That is why, again 10% of alive Pods are shut down. 72-8=64Pods left. </p>

<p>On each loop of the autoscaler controller the number of pods to be change is re-calculated based on the number of current replicas. </p>

<p>So the progress would be like 80 (-8) --> 72 (-8) --> 64 (-7) --> 57 (-6) --> 51 (-6) --> 45  (-5) --> 40</p>

<p>When the number of replicas falls below 40 the first policy <code>Pods</code> is applied and 4 replicas will be reduced at a time.</p>

<p>With 40 pods left both <code>Pods</code> and <code>Percent</code> are about shutting down 4 Pods.</p>

<p>36 Pods left, 10% is 3.6 Pods (before rounding up) which is definitely less than 4 Pods by first rule. The <code>Pods</code> wins, 4 Pods shut down.</p>

<p>The progress would be like 36 (-4) --> 32 (-4) --> 28... and so on.</p>

<p>Hope that helps! :)</p>
