<p>We have defined HPA for an application to have min 1 and max 4 replicas with 80% cpu as the threshold.</p>

<p>What we wanted was, if the pod cpu goes beyond 80%, the app needs to be scaled up 1 at a time. 
Instead what is happening is the application is getting scaled up to max number of replicas.</p>

<p>How can we define the scale velocity to scale 1 pod at a time. And again if one of the pod consumes more than 80% cpu then scale one more pod up but not maximum replicas.</p>

<p>Let me know how do we achieve this.</p>

P Ekambaram
<p>First of all, the 80% CPU utilisation is not a threshold but a target value.</p>

<p>The <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#algorithm-details" rel="nofollow noreferrer">HPA algorithm</a> for calculating the desired number of replicas is based on the following formula:</p>

<blockquote>
  <p><code>X = N * (C/T)</code></p>
</blockquote>

<p>Where:</p>

<ul>
<li><code>X</code>: desired number of replicas</li>
<li><code>N</code>: current number of replicas</li>
<li><code>C</code>: current value of the metric</li>
<li><code>T</code>: target value for the metric</li>
</ul>

<p>In other words, the algorithm aims at calculating a replica count that keeps the observed metric value as close as possible to the target value.</p>

<p>In your case, this means if the average CPU utilisation across the pods of your app is below 80%, the HPA tends to decrease the number of replicas (to make the CPU utilisation of the remaining pods go up). On the other hand, if the average CPU utilisation across the pods is above 80%, the HPA tends to increase the number of replicas, so that the CPU utilisation of the individual pods decreases.</p>

<p>The number of replicas that are added or removed in a single step depends on how far apart the current metric value is from the target value and on the current number of replicas. This decision is internal to the HPA algorithm and you can't directly influence it. The only contract that the HPA has with its users is to keep the metric value as close as possible to the target value.</p>

<p>If you need a very specific autoscaling behaviour, you can write a custom controller (or operator) to autoscale your application instead of using the HPA.</p>
