Horizontal autoscaling
Choose the number of copies
A bookstore serves requests through a Deployment. When more readers arrive, a fixed replica count may become insufficient. A HorizontalPodAutoscaler (HPA) changes the desired replica count of a scalable workload in response to metrics. It does not make an individual container larger or add worker nodes. If four ready replicas have an average CPU utilization of 75% and the target is 50%, the simplified recommendation is six replicas. Actual traffic handling also depends on readiness, startup time, application behavior and available node capacity.
Read a bounded autoscaling/v2 configuration
This static object targets a Deployment named catalog in the same namespace. The CPU metric target is 50% of requested CPU, not 50% of a node or of the CPU limit. The configured range is two to eight replicas; the downscale policy limits removals to one Pod per 60-second policy period, and the stabilization window is five minutes. These are example choices, not universal defaults. The corresponding catalog-deployment.yaml requests 200m CPU per container and deliberately uses a non-pullable image. Neither manifest should be applied as provided.
apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata: name: catalog namespace: scaling-studyspec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: catalog minReplicas: 2 maxReplicas: 8 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 50 behavior: scaleDown: stabilizationWindowSeconds: 300 policies: - type: Pods value: 1 periodSeconds: 60Calculate a simplified recommendation
Start with current replicas multiplied by the ratio of observed metric to target, and round upward to a whole replica. Then bound the result by minReplicas and maxReplicas. Four replicas each requesting 200m and each using 150m have 75% utilization; 4 × 75 / 50 = 6. If all six later average 25%, the raw recommendation is 3. If four replicas average 150%, the raw recommendation is 12, but a maximum of eight bounds the requested scale. These examples assume complete measurements, ready Pods, positive requests and targets, and ignore controller tolerance, policy limits and stabilization.
raw = ceil(currentReplicas × observedMetric / targetMetric)bounded = min(maxReplicas, max(minReplicas, raw))4 × 75 / 50 = 6Select a metric that represents demand
Resource metrics such as CPU and memory are obtained through the resource metrics API, commonly supplied by Metrics Server. A utilization target requires appropriate resource requests; if relevant containers lack CPU requests the corresponding utilization may be undefined. AverageValue uses an absolute per-Pod target rather than request-relative utilization. Pods or Object metrics can represent application-specific observations through a custom metrics adapter; External metrics can represent a queue outside the cluster. ContainerResource can focus on a named container. Verify adapter support and whether the metric actually responds to added replicas. When multiple metrics are valid, HPA uses the largest recommendation; if one fails and the remaining metrics suggest a downscale, it skips that downscale. KEDA is a separate add-on that can expose event-source metrics and manage HPA configuration; it is not installed merely by enabling Kubernetes.
Avoid oscillation and diagnose missing metrics
The real controller adjusts on a periodic loop, uses a tolerance around the target, accounts conservatively for missing or not-yet-ready measurements, and applies configured rate policies and recommendation history. For a downscale stabilization window it considers the highest recent recommendation. Thus a recommendation of three may leave six replicas temporarily. Status can report metric acquisition errors or scaling constraints. In an approved disposable lab only, replace the explicit fixture kubeconfig and context with verified values before using these read-only commands. Inspect ScalingActive, AbleToScale and ScalingLimited conditions when present. Avoid repeatedly applying a fixed spec.replicas value from Git while HPA owns scaling; coordinate migration of ownership to avoid an unintended reset.
kubectl --kubeconfig ./lab.kubeconfig --context scaling-lab -n scaling-study get hpa catalogkubectl --kubeconfig ./lab.kubeconfig --context scaling-lab -n scaling-study describe hpa catalogkubectl --kubeconfig ./lab.kubeconfig --context scaling-lab -n scaling-study get deployment catalogPractice and capacity guardrails
Use hpa-cases.json and metric-cases.json to calculate recommendations and decide whether a failed metric blocks scale down. Check a maximum count against namespace quota, node capacity and the budget for external node provisioning. A replica maximum is not a hard cloud spending cap; surge Pods, other workloads, and node resources can contribute costs. Do not generate load or enable automatic provisioning to test this lesson. An offline calculation is not evidence that metrics, the controller or an adapter are installed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Horizontal autoscaling — knowledge check
Apply autoscaling and disruption concepts to offline scenarios.
Four ready Pods average 75% CPU utilization, target 50%. Ignoring tolerance and policies, what is the raw recommendation?
CPU utilization for an HPA resource target is normally relative to what?
A raw recommendation is 12 and maxReplicas is 8. What is the bounded recommendation?
One metric fails and every successful metric recommends a downscale. What should the HPA do?
What does a downscale stabilization window consider?