<p>According to the K8s <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#stabilization-window" rel="noreferrer">documentation</a>, to avoid flapping of replicas property <code>stabilizationWindowSeconds</code> can be used</p>
<blockquote>
<p>The stabilization window is used to restrict the flapping of replicas when the metrics used for scaling keep fluctuating. The stabilization window is used by the autoscaling algorithm to consider the computed desired state from the past to prevent scaling.</p>
</blockquote>
<blockquote>
<p>When the metrics indicate that the target should be scaled down the algorithm looks into previously computed desired states and uses the highest value from the specified interval.</p>
</blockquote>
<p>From what I understand from documentation, with the following hpa configuration:</p>
<pre><code>  horizontalPodAutoscaler:
    enabled: true
    minReplicas: 2
    maxReplicas: 14
    targetCPUUtilizationPercentage: 70
    behavior:
      scaleDown:
        stabilizationWindowSeconds: 1800
        policies:
          - type: Pods
            value: 1
            periodSeconds: 300
      scaleUp:
        stabilizationWindowSeconds: 60
        policies:
          - type: Pods
            value: 2
            periodSeconds: 60

</code></pre>
<p>Scaling down of my deployment (let's say from 7 pods to 6) shouldn't happen, if at any time during the last 1800 seconds (30 minutes) hpa calculated target pods number equal to 7 pods. But I'm still observing the flapping of replicas in the deployment.</p>
<p><a href="https://i.stack.imgur.com/6KuNO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/6KuNO.png" alt="Deployment Replicas" /></a></p>
<p>What I misunderstood in the documentation and how to avoid continuous scaling up/down of 1 pod?</p>
<p>Kubernetes <em>v1.20</em></p>
<p>HPA description:</p>
<pre><code>CreationTimestamp:                                     Thu, 14 Oct 2021 12:14:37 +0200
Reference:                                             Deployment/my-deployment
Metrics:                                               ( current / target )
  resource cpu on pods  (as a percentage of request):  64% (1621m) / 70%
Min replicas:                                          2
Max replicas:                                          14
Behavior:
  Scale Up:
    Stabilization Window: 60 seconds
    Select Policy: Max
    Policies:
      - Type: Pods  Value: 2  Period: 60 seconds
  Scale Down:
    Stabilization Window: 1800 seconds
    Select Policy: Max
    Policies:
      - Type: Pods  Value: 1  Period: 300 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 cpu resource utilization (percentage of request)
  ScalingLimited  False   DesiredWithinRange  the desired count is within the acceptable range
Events:           &lt;none&gt;
</code></pre>

Dmytro Patserkovskyi
<p>There is a bug in k8s HPA in v1.20, check the <a href="https://github.com/kubernetes/kubernetes/issues/96671" rel="noreferrer">issue</a>. Upgrading to v1.21 fixed the problem, deployment is scaling without flapping after the upgrade.</p>
<p>On the picture scaling of the deployment over 2 days:</p>
<p><a href="https://i.stack.imgur.com/OKTTE.png" rel="noreferrer"><img src="https://i.stack.imgur.com/OKTTE.png" alt="Deployment scale over 2 days" /></a></p>
