kubernetesautoscalinghpahorizontal-pod-autoscaling

Kubernetes HPA won't scale up even when the used memory is more than the average memory limit specified


I have following HPA configuration

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
spec:
  behavior:
    scaleUp:
      policies:
      - periodSeconds: 15
        type: Pods
        value: 4
      - periodSeconds: 15
        type: Percent
        value: 100
      selectPolicy: Max
      stabilizationWindowSeconds: 0
  metrics:
  - resource:
      name: memory
      target:
        averageUtilization: 70
        type: Utilization
    type: Resource
  minReplicas: 1
  maxReplicas: 10

When the deployment has a single pod, if I increase the pod memory to 71%, the HPA doesn't scale up the pod count. When I execute kubectl describe hpa, I can see following.

the desired count is within the acceptable range

But according to the following formula kubernetes uses to calculate the desired pod count, the desired pod count should be 2.

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

I waited few minutes to see there is some stabilization period is used (even though the stabilizationWindowSeconds specified is 0), but it didn't scale up the number of pods.

Then I tried reducing the averageUtilization gradually, and only when I set it to 64 it scaled up and new pod is created.

Why doesn't kubernetes scale up the pod count when the memory usage is below a certain value, even though the memory usage is higher than the specified average value?


Solution

  • Your current desired pod count= 1*(71/70)=1.014

    As per this kubernetes document:

    The control plane skips any scaling action if the ratio is sufficiently close to 1.0 (within a globally-configurable tolerance, 0.1 by default).

    So maybe that is the reason you are not getting new pod scaled up.

    And you have also mentioned that reducing the averageUtilization to 64 is scaling a new pod. The desired count for it is 1.1, so that is the reason you are getting a new pod scaled up.