Kubernetes HPA Scaling Behavior
Kubernetes HPA Scaling Behavior automatically adjusts pod counts based on CPU or memory metrics, ensuring optimal resource usage and application performance.
Kubernetes HPA Scaling Behavior is the dedicated behavior field on a HorizontalPodAutoscaler that governs the rate, timing, and stability of replica changes independently from the metric-driven calculation of the desired replica count itself. Where the metrics configuration determines what target the HPA aims for, the behavior configuration determines how quickly and how cautiously it moves toward that target, which is often just as consequential for a workload's real-world stability as the metric choice itself.
Default Behavior Without Explicit Configuration
Built-In Defaults
When behavior is omitted, the HPA applies Kubernetes' own default policies: scale-up has no stabilization window and can increase replicas immediately and substantially, while scale-down uses a five-minute stabilization window by default, reflecting an assumption that responding quickly to increased load matters more than avoiding brief over-provisioning after a demand drop.
When Defaults Are Insufficient
Workloads with expensive-to-start replicas, tight cost constraints, or historically noisy metrics often need explicit behavior configuration to avoid the default policies producing either sluggish response to genuine spikes or premature scale-down during short lulls that reappear moments later.
Structuring scaleUp and scaleDown
Independent Configuration for Each Direction
scaleUp and scaleDown are configured entirely independently, each with its own stabilizationWindowSeconds, list of policies, and selectPolicy, allowing asymmetric behavior — a common pattern configures fast, unstabilized scale-up alongside slow, heavily stabilized scale-down.
behavior:
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 100
periodSeconds: 15
selectPolicy: Max
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Pods
value: 2
periodSeconds: 60
selectPolicy: Min
Policy Types: Pods and Percent
A policies entry can bound the change either as an absolute number of pods (type: Pods) or a percentage of current replicas (type: Percent) within a given periodSeconds window, and multiple policies can be listed together, with selectPolicy determining whether the largest (Max) or smallest (Min) resulting change among them is applied.
scaleUp:
policies:
- type: Pods
value: 4
periodSeconds: 60
- type: Percent
value: 50
periodSeconds: 60
selectPolicy: Max
Stabilization Windows in Detail
How the Window Selects a Value, Not Just Delays Action
A stabilization window does not simply pause scaling for its duration; it looks back over the window and selects the highest recommended replica count (for scale-up) or the lowest (for scale-down) from all calculations made during that period, meaning a brief spike within a long scale-down stabilization window still prevents scale-down until the window has fully passed without a lower recommendation appearing.
Choosing Window Lengths
A short or zero scale-up stabilization window favors responsiveness to sudden demand; a longer scale-down stabilization window (commonly 300-600 seconds) protects against flapping when demand is naturally bursty, since prematurely removing capacity after a brief lull risks needing to scale back up again moments later, incurring pod startup latency twice in quick succession.
Disabling Scaling in One Direction
selectPolicy: Disabled
Setting selectPolicy: Disabled on either scaleUp or scaleDown turns off automatic scaling in that direction entirely, useful for workloads where scale-up should remain automatic but scale-down should be a deliberate, manual, or separately-scheduled operation (for cost or capacity-planning reasons specific to the workload).
behavior:
scaleDown:
selectPolicy: Disabled
Tuning Behavior for Specific Workload Characteristics
Expensive Pod Startup
Workloads with slow-starting pods (large images, lengthy initialization) benefit from more conservative scale-down stabilization, since prematurely removing a replica that will likely be needed again shortly is more costly than for a workload with near-instant startup.
Cost-Sensitive Batch or Background Workloads
Workloads where cost matters more than immediate responsiveness might invert the typical asymmetry, using a more conservative scale-up policy alongside a faster scale-down, accepting slightly slower response to load increases in exchange for minimizing time spent running excess capacity.
Observing Behavior in Practice
Correlating Configuration With Actual Scaling Events
Reviewing the timestamps and magnitudes of actual scaling events recorded in HPA-related Kubernetes events against the configured behavior policies confirms the tuned parameters are producing the intended cadence of change, rather than assuming a configuration change had the desired effect without observing its real-world outcome.
kubectl get events --field-selector involvedObject.kind=HorizontalPodAutoscaler