<p>Good afternoon. I am working with HPA (HorizontalPodAutoscaler) for the automatic scaling of replicas of a pods, in this case I am using memory usage as a reference, I declare it as follows:</p>
<pre><code>apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: find-complementary-account-info-1
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: find-complementary-account-info-1
  minReplicas: 2
  maxReplicas: 5
  metrics:
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 70
</code></pre>
<p>What I want is to automatically scale my pods when the use of the pod's memory percentage is greater than 70%, and when this percentage drops my replicas return to the declared minimum, however, doing tests, I place a limit at 70, without However, when memory usage is lower than this value, the replicas continue in 4.</p>
<p>5 minutes have passed, and this is what the HPA shows:</p>
<pre><code>[dockermd@tmp108 APP-MM-ConsultaCuentaPagadoraPospago]$ kubectl get hpa -o wide
NAME                                REFERENCE                                      TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
find-complementary-account-info-1   Deployment/find-complementary-account-info-1   65%/70%   2         5         4          2d4h
</code></pre>
<p>I have a wrong concept of HPA or I am declaring the configuration for the HPA in the wrong way, or how to reduce the number of replicas as soon as the memory usage is below the indicated</p>
<p>My environment is on-premise and is configured with metalb, and I use LoadBalancer to expose my services</p>

<p>It is working as designed. The <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#algorithm-details" rel="nofollow noreferrer">algorithm counts</a> desired number of replicas by using this formula:</p>
<pre><code>desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]
</code></pre>
<p>So if your <code>desiredMetricValue</code> is 70% and <code>currentMetricValue</code> is 65% than</p>
<pre><code>desiredReplicas=4*(65%/70%)=~3.7
</code></pre>
<p>The CEIL() function returns the smallest integer value that is bigger than or equal to a number so in this case it is 4.</p>
<p>If you want to lower the number to 3 replicas you have to lower your utilization to 52.5%:</p>
<pre><code>3=4*(`currentMetricValue`/70)
currentMetricValue=52.5%
</code></pre>
