<p>I am playing around with the Horizontal Pod Autoscaler in Kubernetes. I've set the HPA to start up new instances once the average CPU Utilization passes 35%. However this does not seem to work as expected.
The HPA triggers a rescale even though the CPU Utilization is far below the defined target utilization. As seen below the &quot;current&quot; utilization is 10% which is far away from 35%. But still, it rescaled the number of pods from 5 to 6.
<a href="https://i.stack.imgur.com/JnAA0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JnAA0.png" alt="enter image description here" /></a></p>
<p>I've also checked the metrics in my Google Cloud Platform dashboard (the place at which we host the application). This also shows me that the requested CPU utilization hasn't surpassed the threshold of 35%. But still, several rescales occurred.
<a href="https://i.stack.imgur.com/wWYMK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wWYMK.png" alt="enter image description here" /></a></p>
<p>The content of my HPA</p>
<pre><code>apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
 name: django
spec:
{{ if eq .Values.env &quot;prod&quot; }}
 minReplicas: 5
 maxReplicas: 35
{{ else if eq .Values.env &quot;staging&quot; }}
 minReplicas: 1
 maxReplicas: 3
{{ end }}
 scaleTargetRef:
   apiVersion: apps/v1
   kind: Deployment
   name: django-app
 targetCPUUtilizationPercentage: 35
</code></pre>
<p>Does anyone know what the cause of this might be?</p>

<p>Scaling is based on % of <code>requests</code> not <code>limits</code>. I think we should change this answer as the examples in the accepted answer show:</p>
<pre><code> limits:
   cpu: 1000m
</code></pre>
<p>But the <code>targetCPUUtilizationPercentage</code> is based on <code>requests</code> like:</p>
<pre><code>requests:
   cpu: 1000m
</code></pre>
<blockquote>
<p>For per-pod resource metrics (like CPU), the controller fetches the metrics from the resource metrics API for each Pod targeted by the HorizontalPodAutoscaler. Then, if a target utilization value is set, the controller calculates the utilization value as a percentage of the equivalent resource request on the containers in each Pod. If a target raw value is set, the raw metric values are used directly. The controller then takes the mean of the utilization or the raw value (depending on the type of target specified) across all targeted Pods, and produces a ratio used to scale the number of desired replicas.</p>
</blockquote>
<p><a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#how-does-a-horizontalpodautoscaler-work" rel="noreferrer">https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#how-does-a-horizontalpodautoscaler-work</a></p>
