Skip to main content

Horizontal Pod Autoscaler

Why HPA Matters

Picture a bustling restaurant. On weekdays, only a few tables are occupied, but on weekends, crowds flood in. The manager doesn’t build a bigger restaurant every time he simply adds more staff to handle demand. Kubernetes workloads face the same challenge: traffic fluctuates, and pods must scale horizontally to keep up.

Horizontal Pod Autoscaler (HPA) is the restaurant manager of Kubernetes, adding or removing pods based on demand.


How HPA Works

  • Metrics Server: Collects resource usage (CPU, memory, custom metrics).
  • Target Utilization: Defines thresholds (e.g. 50% CPU).
  • Scaling Logic:
    • If usage > target → add pods.
    • If usage < target → remove pods.
  • Control Loop: HPA checks metrics every 15 seconds and adjusts replicas.
Analogy: HPA is like adding more waiters when the restaurant gets busy and reducing staff when it’s quiet.

Global Context

  • Enterprises: Use HPA for web apps, APIs, and microservices with unpredictable traffic.
  • Cloud Providers: Managed Kubernetes services integrate HPA with cloud monitoring systems.
  • Community: HPA is one of the most widely adopted Kubernetes features, powering elasticity worldwide.

Hands‑On Exercise

  1. Reflect: How does HPA dynamically add pods when CPU spikes, and scale down when demand drops?

Generate load:

kubectl run -i --tty load-generator --image=busybox /bin/sh
while true; do wget -q -O- http://web; done

Create an HPA:

kubectl autoscale deployment web --cpu-percent=50 --min=2 --max=10
kubectl get hpa

Create a deployment:

kubectl create deployment web --image=nginx
kubectl expose deployment web --port=80

Ensure Metrics Server is running:

kubectl get deployment metrics-server -n kube-system

The Hacker’s Notebook

  • HPA listens to metrics for scaling pods up or down.
  • Target utilization defines thresholds like restaurant occupancy limits.
  • Lesson for engineers: Don’t guess replica counts let HPA decide.
  • Hacker’s mindset: Treat HPA as your auto‑pilot. With it, you can handle unpredictable traffic globally.

Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 30, 2025