Skip to main content

How to autoscale pods with the Kubernetes HorizontalPodAutoscaler

The HorizontalPodAutoscaler scales a Deployment to match load using metrics-server and CPU requests. Define a target utilization, generate load to test, and tune behavior policies to avoid flapping.

Difficulty
Intermediate
Duration
35 minutes
Steps
6

Autoscaling pods with the HorizontalPodAutoscaler

The HorizontalPodAutoscaler (HPA) adjusts the replica count of a Deployment to match observed load. It reads metrics, compares them to a target, and adds or removes pods. CPU-based scaling is the most common starting point and requires metrics-server.

Prerequisites

  • A Deployment whose containers define CPU requests (the HPA needs them to compute utilization).
  • Cluster admin access to install metrics-server.

Steps

1. Install metrics-server

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
kubectl get deployment metrics-server -n kube-system

Confirm metrics flow with kubectl top pods.

2. Set resource requests

The HPA computes utilization as usage divided by request:

resources:
  requests:
    cpu: 200m
  limits:
    cpu: 500m

3. Create the HPA

kubectl autoscale deployment myapp --cpu-percent=70 --min=2 --max=10

Or declaratively with an autoscaling/v2 manifest targeting average CPU utilization at 70%.

4. Generate load

Drive CPU up to trigger scaling:

kubectl run -it --rm load --image=busybox -- /bin/sh -c "while true; do wget -q -O- http://myapp; done"

5. Watch scaling decisions

kubectl get hpa myapp --watch
kubectl describe hpa myapp

The events show the desired replica calculation.

6. Tune behavior policies

Use the behavior field to control how fast pods are added or removed, preventing flapping:

behavior:
  scaleDown:
    stabilizationWindowSeconds: 300

Verification

With load applied, confirm replica count rises toward max via kubectl get hpa. Stop the load and confirm the HPA scales back down after the stabilization window. kubectl top pods should show CPU returning below the target.

Next Steps

Scale on memory or custom application metrics through the custom metrics API, combine the HPA with a cluster autoscaler so nodes grow with pods, and add a PodDisruptionBudget to protect availability during scale-down.

Prerequisites

  • A Deployment with resource requests set
  • metrics server installed

Steps

  • 1
    Install metrics-server
  • 2
    Set resource requests
  • 3
    Create the HPA
  • 4
    Generate load
  • 5
    Watch scaling decisions
  • 6
    Tune behavior policies

Category

Containers