Sub topic 7
Tuning scale-up and scale-down behavior
This chapter assumes you can create an HPA with the autoscaling/v2 API and that you know the formula desiredReplicas = ceil[currentReplicas * (currentMetricValue / desiredMetricValue)]. The formula decides where the HPA wants to go. The behavior field decides how fast it gets there.
Why flapping happens
Metrics are noisy. A traffic spike can push the formula to 10 replicas, and a minute later it can say 3. If the HPA followed every result, replicas would rise and fall repeatedly. This is called flapping. Each swing starts and stops Pods, which wastes capacity and can disrupt users.
The behavior field under spec gives you two controls for each direction: stabilization windows and scaling policies.
Stabilization windows
A stabilization window is a period during which the HPA remembers its recent recommendations. It uses them to stop a brief change from causing a scaling action.
- When scaling down, the HPA picks the highest recommendation in the window. One low reading cannot shrink the Deployment.
- When scaling up, it picks the lowest recommendation in the window. One high reading cannot grow it.
The defaults are 300 seconds for scale-down and 0 seconds for scale-up. The HPA reacts quickly to load and releases capacity slowly.
Scaling policies
A policy limits how much the replica count can change in one period. A policy has a type (Pods or Percent), a value, and a periodSeconds.
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
behavior:
scaleUp:
policies:
- type: Pods
value: 4
periodSeconds: 60
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 50
periodSeconds: 60This HPA adds at most 4 Pods per minute. On the way down, it waits 300 seconds, then removes at most half of the replicas per minute.
If a scale-down policy removes half the replicas, a PodDisruptionBudget (a limit on voluntary Pod evictions) can still slow the actual Pod removal. The two mechanisms work independently.
How the pieces combine
If you list several policies for one direction, selectPolicy chooses between them:
Max(the default) applies the policy that allows the biggest change.Minapplies the policy that allows the smallest change.Disabledstops scaling in that direction.
For example, a scale-down with selectPolicy: Disabled turns off automatic scale-down. Scale-up still works.
Common errors
- Setting
stabilizationWindowSecondstoo low on scale-down. The Deployment shrinks during a short lull and then has to grow again. - Setting scale-up policies too tight. With 1 Pod per 60 seconds, a spike can take ten minutes to absorb while users see errors.
- Expecting immediate results. The HPA also ignores changes when the ratio of current to target metric is within the tolerance of 0.1 (10%) around 1.0. A 4-replica Deployment at 72% CPU against a 70% target will not change.
- Using
Percentwith small replica counts. 50% of 2 replicas is 1 Pod. The HPA rounds down when scaling down, so it still removes at least what the policy allows, but the steps are coarse.
Check what happened
kubectl describe hpa webThe Events and Conditions sections show messages such as ScalingLimited and the reason, for example that the scale-up policy capped the change. This tells you whether the formula or your behavior settings limited the result.
Exercise
An HPA has 4 replicas, a CPU target of 60%, and a measured average of 90%. Its scaleUp policy allows 1 Pod per 60 seconds. How many replicas does the formula want, and how many will the Deployment have after the first minute?
Show answer
The formula gives ceil[4 * (90 / 60)] = 6 replicas. The policy allows only 1 added Pod per 60 seconds, so the Deployment has 5 replicas after the first minute and reaches 6 after the second.
Exercise
Load drops for two minutes and then returns. The scaleDown stabilizationWindowSeconds is 300. Does the Deployment shrink during the lull?
Show answer
No. During scale-down the HPA uses the highest recommendation in the 300-second window, which still includes the earlier high-load recommendations, so the replica count stays the same.