Skip to main content
Welcome back. In this lesson we cover scaling behavior for the Horizontal Pod Autoscaler (HPA) in Kubernetes — how it decides when to add or remove pods, the policy knobs you can tune, and best practices for stable autoscaling. Overview
  • The Horizontal Pod Autoscaler (HPA) adjusts the replica count of Deployments and other scalable controllers to match observed load.
  • Goals: keep application performance predictable while balancing resource usage and cost.
  • HPA can scale using:
    • native resource metrics (CPU, memory),
    • custom in-cluster metrics,
    • external metrics (e.g., cloud provider or external systems).
How scaling is triggered HPA periodically evaluates the configured metrics (default every ~15 seconds). When an observed metric diverges from the configured target (for example, average CPU utilization rises above the target), the controller computes a target replica count and then applies scaling decisions subject to configured limits and policies. Key stages in a scaling decision:
  1. Observe metrics across the target pods.
  2. Calculate desired replicas based on metric type and target.
  3. Apply minReplicas/maxReplicas bounds.
  4. Respect behavior policies (stabilization windows and rate limits).
  5. Execute the scale event (create or terminate pods).
Important policy knobs you configure Behavior policy types (examples) Stabilization window and conservative scale-down A stabilization window introduces a delay in evaluating the candidate replica count, which smooths out transient metric spikes or drops and prevents oscillation (rapid scale-up then immediate scale-down). Typical defaults used by many clusters: This means HPA will usually scale up quickly to handle load, but it will wait (by default ~5 minutes) of sustained low utilization before scaling down — a conservative approach that reduces oscillations in noisy environments.
A presentation slide titled "Scale-Down Scaling Behavior" showing three colored panels: "Trigger" (metric falls below target), "Action" (HPA decreases pod replicas), and "Stabilization Window" (delay to prevent rapid pod fluctuations).
Example HPA manifest with behavior settings Below is a practical HPA manifest that demonstrates common behavior settings. It targets a Deployment, defines min/max replicas, and configures both scaleUp and scaleDown policies to constrain how fast replicas can change.
Notes about the example
  • averageUtilization: 50 tells HPA to target roughly 50% CPU utilization per pod.
  • scaleUp:
    • stabilizationWindowSeconds: 0 means no delay for scale-up actions — HPA may respond immediately to rising load.
    • The Percent policy with value: 100 allows doubling the replica count every periodSeconds: 60, bounded by maxReplicas.
  • scaleDown:
    • stabilizationWindowSeconds: 300 means HPA will observe low utilization for 5 minutes before acting.
    • The Percent policy with value: 10 limits downscaling to at most 10% of current replicas per 60-second period.
  • Policies are limits (caps), not guarantees: the HPA only changes replicas as much as needed, up to the specified limits.
Defaults and behavior can vary by Kubernetes version and cloud controller implementations. As a rule of thumb, prefer aggressive scale-up (short stabilization window) and conservative scale-down (longer stabilization window) to reduce oscillation.
Best practices
  • Align stabilization windows and rate limits with observed traffic patterns:
    • Short windows for bursty, latency-sensitive workloads.
    • Longer windows for noisy or highly variable workloads.
  • Use meaningful metrics:
    • Resource metrics (CPU/memory) are easy to start with.
    • Add application-level metrics (latency, error rate, request queue length) for user-facing performance signals.
  • Start conservative, observe, and iterate:
    • Monitor HPA events and scaling actions (kubectl describe hpa <name>).
    • Tune policies and stabilizationWindowSeconds gradually based on metrics.
  • Avoid overly aggressive downscaling that removes capacity needed for short spikes.
  • Combine HPA with Cluster Autoscaler (if using autoscaling nodes) to ensure node capacity scales with pod demand.
A presentation slide titled "Scaling Behavior – Best Practices" showing three numbered tips: 01 Avoid rapid scaling, 02 Clear policies, and 03 Monitor and adjust, each with a simple icon. The layout uses colorful rounded boxes and gradient number badges.
Be aware of control-plane and metrics-server differences across Kubernetes versions. Some behavior fields and defaults changed between autoscaling/v2beta2 and autoscaling/v2. Always consult your cluster’s API documentation before applying HPA manifests.
Further reading and references That concludes this lesson on HPA scaling behavior. Hope you found it useful and actionable.

Watch Video

Practice Lab