The Horizontal Pod Autoscaler (HPA) adds and removes pod replicas to keep a metric near a target. Enabling it takes a few lines of YAML. Making it keep up with real traffic, without flapping, takes a little more thought.

How the HPA decides

Roughly: desired replicas = current replicas × (current metric value ÷ target value), rounded up, within minReplicas and maxReplicas. For CPU, the metric is average utilization as a percentage of the CPU request across pods. That is why requests must be realistic before autoscaling can work.

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: { name: api }
spec:
  scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: api }
  minReplicas: 3
  maxReplicas: 30
  metrics:
    - type: Resource
      resource: { name: cpu, target: { type: Utilization, averageUtilization: 60 } }
  behavior:
    scaleUp:
      stabilizationWindowSeconds: 0
      policies: [{ type: Percent, value: 100, periodSeconds: 60 }]
    scaleDown:
      stabilizationWindowSeconds: 300
      policies: [{ type: Percent, value: 20, periodSeconds: 60 }]

Choosing the metric

MetricGood forWatch out for
CPU utilizationCPU-bound servicesI/O-bound services barely move CPU
MemoryRarely a good scaling signalMemory does not drop when load drops
Requests per second per pod (custom)Web APIsNeeds a metrics adapter
Queue length or age (external)WorkersNeeds a metrics adapter; scale on age for latency

For web services, CPU works surprisingly well if requests are correct. For queue consumers, scaling on backlog age keeps processing latency steady.

Choosing the target

A target of 60 to 70% CPU leaves headroom for spikes while new pods start. If pods take a minute to become ready and traffic can double in that minute, a lower target buys time. Measure your startup time; it is part of the scaling budget.

Behaviour: fast up, slow down

Scale up quickly when traffic rises, and scale down slowly to avoid flapping when traffic dips briefly. The stabilization window on scale-down (300 seconds above) uses the highest recommendation over that period, which prevents removing pods that will be needed again a minute later.

Min and max replicas

  • minReplicas: at least 2 or 3 for availability across nodes and zones, and enough to absorb a spike while scaling happens.
  • maxReplicas: a safety cap that protects downstream systems (databases, rate-limited APIs) and your bill. Alert when you hit it.

Things the HPA cannot fix

  • Slow startup: if a pod takes 3 minutes to be ready, autoscaling reacts 3 minutes late. Optimise startup and use readiness properly.
  • Downstream limits: more pods mean more database connections. Check connection pooling and max_connections before raising maxReplicas.
  • Node capacity: new pods stay Pending if the cluster has no room; the cluster autoscaler must keep up too.

Monitoring autoscaling

Chart, for each autoscaled workload: replicas over time, the scaling metric against its target, p95 latency and error rate. Good autoscaling shows replicas rising ahead of latency; bad autoscaling shows latency spiking first. Alert when replicas sit at max, and when pods are Pending. VitalRix infrastructure monitoring shows workloads and pods with CPU and memory against limits, and APM shows the latency that tells you whether scaling kept up. See also Kubernetes monitoring.

A worked example

A checkout service with 6 replicas runs at about 40% CPU of its requests. A marketing email goes out, and traffic triples in three minutes. With a 60% target and fast scale-up allowed (100% more pods per minute), the HPA computes desired replicas = 6 × (120 ÷ 60) = 12 at the first evaluation, and new pods are ready in 40 seconds. Latency rises briefly and recovers. With the old configuration (target 80%, scale-up limited to 4 pods per minute, 2-minute startup), the same spike would have taken several minutes to absorb, with p95 latency far above target throughout.

Combining HPA with the cluster autoscaler

The HPA adds pods; the cluster autoscaler adds nodes when pods cannot be scheduled. Node provisioning takes minutes, so keep some headroom: slightly lower HPA targets, or a small amount of spare capacity, so the first wave of new pods fits on existing nodes.

Scheduled and event-driven scaling

For predictable peaks (a daily batch, a known launch), raise minReplicas ahead of time with a scheduled job. For queue workers, event-driven autoscaling based on queue length or age reacts better than CPU. Monitor both the queue age and replica count to confirm scaling keeps processing latency within your target.

Why is my HPA not scaling?

Common causes: no CPU request set (utilization cannot be computed), metrics-server not running, or the metric target already met.

Why does my HPA flap?

Scale-down is too aggressive for a spiky metric. Increase the scale-down stabilization window.

Can I scale on custom metrics?

Yes, with a metrics adapter that exposes them to the HPA, for example requests per second or queue length.

Try VitalRix free

Uptime monitoring, APM, logs, real user monitoring and status pages in one place. No credit card needed.