kuberneteshpa

HPA ScalingLimited=True TooFewReplicas


That is my HPA. I want to start the deployment with default replicas=3

apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: backend-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: backend

  minReplicas: 3
  maxReplicas: 20

  metrics:
    - type: Resource
      resource:
        name: memory
        target:
          type: AverageValue
          averageValue: 500Mi
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70

  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
        - type: Pods
          value: 1
          periodSeconds: 100

    scaleUp:
      stabilizationWindowSeconds: 60
      policies:
        - type: Pods
          value: 1
          periodSeconds: 30
        - type: Percent
          value: 10
          periodSeconds: 60
      selectPolicy: Max

But it always says: ScalingLimited True TooFewReplicas the desired replica count is less than the minimum replica count and I can't understand why.

➜ kg hpa
NAME                  REFERENCE                    TARGETS                       MINPODS   MAXPODS   REPLICAS   AGE
prod-backend-hpa-v1   Deployment/prod-backend-v1   145881770666m/500Mi, 2%/70%   3         20        3          7m16s


➜ kd hpa
Name:                                                  prod-backend-hpa-v1
Namespace:                                             prod
Labels:                                                argocd.argoproj.io/instance=backend-prod
Annotations:                                           <none>
CreationTimestamp:                                     Thu, 02 Jun 2022 19:34:30 -0500
Reference:                                             Deployment/prod-backend-v1
Metrics:                                               ( current / target )
  resource memory on pods:                             145596416 / 500Mi
  resource cpu on pods  (as a percentage of request):  1% (3m) / 70%
Min replicas:                                          3
Max replicas:                                          20
Behavior:
  Scale Up:
    Stabilization Window: 60 seconds
    Select Policy: Max
    Policies:
      - Type: Pods     Value: 1   Period: 30 seconds
      - Type: Percent  Value: 10  Period: 60 seconds
  Scale Down:
    Stabilization Window: 300 seconds
    Select Policy: Max
    Policies:
      - Type: Pods  Value: 1  Period: 100 seconds
Deployment pods:    3 current / 3 desired
Conditions:
  Type            Status  Reason            Message
  ----            ------  ------            -------
  AbleToScale     True    ReadyForNewScale  recommended size matches current size
  ScalingActive   True    ValidMetricFound  the HPA was able to successfully calculate a replica count from memory resource
  ScalingLimited  True    TooFewReplicas    the desired replica count is less than the minimum replica count
Events:
  Type    Reason             Age    From                       Message
  ----    ------             ----   ----                       -------
  Normal  SuccessfulRescale  9m22s  horizontal-pod-autoscaler  New size: 3; reason: Current number of replicas below Spec.MinReplicas



➜ kgpo  
NAME                              READY   STATUS    RESTARTS   AGE
prod-backend-v1-8dd687999-54mzp   1/1     Running   0          58m
prod-backend-v1-8dd687999-nn7c2   1/1     Running   0          2d17h
prod-backend-v1-8dd687999-rcxsw   1/1     Running   0          2d17h

➜ kg rs 
NAME                         DESIRED   CURRENT   READY   AGE
prod-backend-v1-566b9c8856   0         0         0       2d19h
prod-backend-v1-578d699c45   0         0         0       2d19h
prod-backend-v1-64859b74c9   0         0         0       2d18h
prod-backend-v1-6498b4b45c   0         0         0       2d19h
prod-backend-v1-656cccdc4b   0         0         0       2d19h
prod-backend-v1-66cc5cf44    0         0         0       2d19h
prod-backend-v1-698c7ddc7d   0         0         0       2d19h
prod-backend-v1-6bdbc77f5d   0         0         0       2d19h
prod-backend-v1-7486c95664   0         0         0       2d19h
prod-backend-v1-774cdbdcdc   0         0         0       2d19h
prod-backend-v1-8dd687999    3         3         3       2d17



Solution

  • A horizontal pod autoscaler, defined by a HorizontalPodAutoscaler object, specifies how the system should automatically increase or decrease the scale of a replication controller or deployment configuration, based on metrics collected from the pods that belong to that replication controller or deployment configuration.

    ScalingLimited indicates that autoscaling is not allowed because a maximum or minimum replica count was reached.

    1. A True condition indicates that you need to raise or lower the minimum or maximum replica count in order to scale.
    2. A False condition indicates that the requested scaling is allowed.

    As per your use case the ScalingLimited shows status as “True” with message “desired replica count is less than the minimum replica count”. So, as a workaround you can increase minReplicas or set scaleDown policy with large periodSeconds to increase stabilization periods.

    Refer Pod Autoscaling for more information.