tezvyn:

Horizontal Pod Autoscaler (HPA): Scale on Demand

AI-drafted, machine-checkedSource: kubernetes.ioadvanced
Horizontal Pod Autoscaler (HPA): Scale on Demand

A Horizontal Pod Autoscaler (HPA) is a thermostat for your app's capacity, adding or removing pods based on load. It's used to handle traffic spikes by watching metrics like CPU, but a common footgun is setting aggressive thresholds that cause flapping.

WHY IT EXISTS: Manually scaling applications is slow, error-prone, and inefficient. You either overprovision resources, wasting money, or underprovision, risking outages during traffic spikes. The HPA was created to automate this scaling process based on real-time application load.

THE MENTAL MODEL: The HPA is a control loop, like a thermostat for your application's capacity. You define a desired state (e.g., "keep average CPU usage across all pods at 60%"), and the HPA controller continuously checks the current state and takes action—adding or removing pods—to move closer to your target. It doesn't predict traffic; it reacts to it.

HOW IT WORKS: The HPA controller periodically queries the Kubernetes Metrics Server for resource metrics (CPU, memory) or custom metrics for the pods it targets. It then calculates the current average value across all pods. If the current average is higher than the target, it increases the replica count. If it's lower, it decreases the count. The calculation is typically: desiredReplicas = ceil[currentReplicas * (currentMetricValue / desiredMetricValue)]. To prevent rapid, destabilizing changes (known as 'flapping'), the HPA uses configurable cooldown periods for scaling up and down.

WHEN TO USE IT: Use HPA for stateless applications that can be easily replicated, such as web servers, microservices, or API backends. It is ideal for workloads with variable or unpredictable traffic patterns, allowing you to build resilient, cost-efficient systems that automatically adapt to demand without manual intervention.

WHEN NOT TO USE IT: Avoid HPA for stateful applications that are difficult to scale horizontally, like a traditional single-node database that can't share load across multiple instances. Also, it's less effective for workloads with very long startup times, as the scaling reaction might be too slow to handle a sudden spike. For these cases, vertical scaling (VPA) or manual scaling may be more appropriate.

ONE CANONICAL EXAMPLE: A web service is deployed with a target CPU utilization of 70%. During a flash sale, traffic surges, and the average CPU across its 3 pods jumps to 95%. The HPA controller detects this deviation from the target. It calculates that more pods are needed and scales the deployment up to 4, then 5 pods. As the new pods come online and take on traffic, the load is distributed, and the average CPU usage across all pods settles back towards the 70% target.

Read the original → kubernetes.io

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.