Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HorizontalPodAutoscaler Kubernetes does not keep the minimum number of replicas [closed]

Tags:

kubernetes

hpa

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:

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

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.

5 minutes have passed, and this is what the HPA shows:

[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

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

My environment is on-premise and is configured with metalb, and I use LoadBalancer to expose my services

like image 815
Cesar Justo Avatar asked Dec 22 '25 00:12

Cesar Justo


1 Answers

It is working as designed. The algorithm counts desired number of replicas by using this formula:

desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]

So if your desiredMetricValue is 70% and currentMetricValue is 65% than

desiredReplicas=4*(65%/70%)=~3.7

The CEIL() function returns the smallest integer value that is bigger than or equal to a number so in this case it is 4.

If you want to lower the number to 3 replicas you have to lower your utilization to 52.5%:

3=4*(`currentMetricValue`/70)
currentMetricValue=52.5%
like image 63
kool Avatar answered Dec 23 '25 14:12

kool



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!