3

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Cesar Justo
  • 707
  • 2
  • 11
  • 35
  • [Kubernetes HPA not downscaling as expected](https://stackoverflow.com/questions/64530209/kubernetes-hpa-not-downscaling-as-expected/64535935#64535935) is very similar. According to the [formula in the HPA documentation](https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#algorithm-details), the target replica count is `ceil[currentReplicas * currentValue / targetValue]`, or `ceil[4 * 65% / 70%]`, which still rounds up to 4; you'd need to get down to 52% memory utilization to scale down. – David Maze Nov 01 '20 at 12:56

1 Answers1

1

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%
kool
  • 3,214
  • 1
  • 10
  • 26