<p>I am trying to understand how hpa works but I have some concerns:</p>
<p>In case my service is set like this:</p>
<pre><code>resources:
  limits:
   cpu: 500m
   memory: 1Gi
  requests:
   cpu: 250m
   memory: 512Mi
</code></pre>
<p>and I configure hpa in this way:</p>
<pre><code>spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: test-service
  minReplicas: 3
  maxReplicas: 6
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
   
</code></pre>
<p>Is it preventing my service to reach the limits (500m), right?<br />
Is it better to configure by putting a higher value like 80%?</p>
<p>I have this doubt because with this configuration I see pods scaled to the maximum number even if they are using less cpu than limits:</p>
<pre><code>NAME                                  CPU(cores)   MEMORY(bytes)   
test-service-76f8b8c894-2f944            189m         283Mi           
test-service-76f8b8c894-2ztt6            183m         278Mi           
test-service-76f8b8c894-4htzg            117m         233Mi           
test-service-76f8b8c894-5hxhv            142m         193Mi           
test-service-76f8b8c894-6bzbj            140m         200Mi           
test-service-76f8b8c894-6sj5m            149m         261Mi    
</code></pre>
<p>The amount of CPU used is less than the request configured in the definition of the service.</p>
<p>Moreover, I have seen that it has been discussed here as well but I didn't get the answer.
<a href="https://stackoverflow.com/questions/56198941/using-horizontal-pod-autoscaling-along-with-resource-requests-and-limits">Using Horizontal Pod Autoscaling along with resource requests and limits</a></p>

user1971444
<blockquote>
<p>Is it preventing my service to reach the limits (500m), right?</p>
</blockquote>
<p>No, hpa is not preventing it (althogh <strong>resources.limits</strong> is). What hpa does is starting new replicas when the average cpu utilization across all pods gets above 50% of <strong>requested</strong> cpu resources, i.e. above 125m.</p>
<blockquote>
<p>Is it better to configure by putting a higher value like 80%?</p>
</blockquote>
<p>Can't say, it is application specific.</p>
<p>Horizontal autoscaling is pretty well described in the <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/" rel="nofollow noreferrer">documentation</a>.</p>
