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
Install the lab environment (kind cluster + metrics-server) with the dedicated script:
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 10 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 toward the maximum.
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 whole exercise is automated in: