Horizontal Pod Autoscaling (HPA)
Objectives
Use the Horizontal Pod Autoscaler (HPA) to automatically scale the number of pod replicas based on CPU usage, so the workload matches demand instead of paying for idle capacity.
Prerequisites
Understanding HPA
The Horizontal Pod Autoscaler scales a workload out (more replicas) or in (fewer replicas) by watching a metric such as CPU utilization. It is one of the core FinOps levers on Kubernetes: capacity follows demand.
Q1: What does the HPA need to work?
Q2: When should you use HPA vs VPA?
Setup
The cluster is shared by the trainees: metrics-server are installed once for the whole cluster by the trainer, with install.sh. Do not run it yourself.
Info
Trainer only, once per cluster, before the lab:
The script targets the kind-kind cluster, and creates it if it does not exist (CLUSTER_NAME=<name> for another one). It can be run again safely: a running VPA is never reinstalled.
Switch to the shared kind cluster:
Verify metrics are available:
Deploy a CPU-bound workload
We use the classic php-apache image, which burns CPU on every HTTP request.
Shared cluster: run the lab in a namespace suffixed by your user name so several students can work on the same cluster without collisions.
Note: The Deployment defines
cpu: 200mas a request andcpu: 500mas a limit. The request is what the HPA uses as the 100% reference for utilization.
Create the HorizontalPodAutoscaler
Q3: Create an HPA targeting 50% average CPU, between 1 and 3 replicas.
Generate load and watch it scale OUT
Open two terminals (or use byobu).
Terminal 1 — watch the autoscaler and the deployment:
Terminal 2 — generate load:
After 1–2 minutes the CPU target is exceeded and the HPA increases the replica count:
Expected: TARGETS climbs well above 50% and REPLICAS grows to the maximum. The maximum is kept low (3, each pod can use up to 500m CPU) because all the trainees share the same cluster.
Stop the load and watch it scale IN
After the stabilization window (default ~5 minutes for scale-down) the replica count drops back to 1:
FinOps takeaway: the scale-down stabilization window prevents flapping, but it also means you keep paying for extra replicas a few minutes after the spike ends. Tune
behavior.scaleDown.stabilizationWindowSecondsto balance cost vs responsiveness.
Automated run
The solution of this lab is validated by ex1-hpa.sh.