Horizontal Pod Autoscaler (HPA) & Metrics Server
Scale replicas from CPU/memory metrics via Metrics Server and HPA.
In plain words
HPA is a restaurant manager opening duplicate counters when each reserved counter stays too busy, rather than merely measuring the queue. The simple Kubernetes rule is: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
Think of it like…
HPA is a restaurant manager opening duplicate counters when each reserved counter stays too busy, rather than merely measuring the queue.
Where you’ve seen it
During a release or scale event, the workload owner runs kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa api. Expected evidence: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies. If the run instead shows this symptom, stop and restore the last known state: No CPU request makes utilization scaling undefined, stale metrics can freeze the controller, and scaling only adds throughput when the bottleneck is inside stateless replicas.
Words you’ll hear
- HPA
- For this lesson, HPA is the controlling idea: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
- Metrics Server
- Metrics Server is the checkpoint; run
kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa apiand inspect the owner chain. No CPU request makes utilization scaling undefined, stale metrics can freeze the controller, and scaling only adds throughput when the bottleneck is inside stateless replicas. - Autoscale
- Autoscale is the outcome to preserve; the cost is explicit: HPA handles demand quickly but duplicates application startup costs and cannot solve a dependency, database, node capacity, or memory limit problem.
- Verification evidence
- The evidence command is
kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa api. It connects the declaration to this behavior: Scale replicas from CPU/memory metrics via Metrics Server and HPA.
Visual diagram
Theory & deep dive
How HPA reaches the intended state
Scale replicas from CPU/memory metrics via Metrics Server and HPA. HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
The concrete checkpoint is kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa api; compare the HPA owner, Metrics Server state, and Autoscale outcome instead of treating YAML acceptance as proof.
Run kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa api after the change and correlate the result with Events and the owning object. HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
Metrics Server: failure boundary and production cost
No CPU request makes utilization scaling undefined, stale metrics can freeze the controller, and scaling only adds throughput when the bottleneck is inside stateless replicas. The prerequisite contract is concrete: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
HPA handles demand quickly but duplicates application startup costs and cannot solve a dependency, database, node capacity, or memory limit problem. A safe design limits blast radius, records the last good configuration, and uses kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa api as a deterministic rollback checkpoint.
Capture the current state with kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa api, then correct the controlling manifest rather than patching a child object; the relevant divergence signal is specific: No CPU request makes utilization scaling undefined, stale metrics can freeze the controller, and scaling only adds throughput when the bottleneck is inside stateless replicas.
How it works, step by step
Step 1 of 41. Declare workload intent
Write the controller-owned Pod or scaling intent and preserve the owner chain: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
- Goal
- Scale replicas from CPU/memory metrics via Metrics Server and HPA.
- Owner
- HPA
When to use it & trade-offs
What it is
Scale replicas from CPU/memory metrics via Metrics Server and HPA. HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
Why it matters: No CPU request makes utilization scaling undefined, stale metrics can freeze the controller, and scaling only adds throughput when the bottleneck is inside stateless replicas. The invariant at risk is specific: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies. The quickest fact check before changing the design is kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa api.
When to reach for it
- Use HPA to enforce this invariant: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
- Use Metrics Server evidence from
kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa apiin reviews and incident timelines. Scale replicas from CPU/memory metrics via Metrics Server and HPA. - Choose the progressive-delivery layer route only when this cost is justified by governance or scale: HPA handles demand quickly but duplicates application startup costs and cannot solve a dependency, database, node capacity, or memory limit problem.
When to avoid or be careful
- Avoid assuming HPA is complete after YAML submission; the required behavior is concrete: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
- Avoid treating the next symptom as random; check the owner and state with
kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa apifirst: No CPU request makes utilization scaling undefined, stale metrics can freeze the controller, and scaling only adds throughput when the bottleneck is inside stateless replicas. - Avoid adopting a heavier platform wrapper before measuring its cost: HPA handles demand quickly but duplicates application startup costs and cannot solve a dependency, database, node capacity, or memory limit problem.
Native, manual, and managed paths for HPA
kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa api.Strengths: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
Watch out: HPA handles demand quickly but duplicates application startup costs and cannot solve a dependency, database, node capacity, or memory limit problem. Recovery must also recognize this signal: No CPU request makes utilization scaling undefined, stale metrics can freeze the controller, and scaling only adds throughput when the bottleneck is inside stateless replicas.
kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa api; the expected invariant is: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.Strengths: kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa api gives immediate feedback without first building a platform layer.
Watch out: A manual change can hide drift, and unlike HPA, it does not make this symptom self-documenting: No CPU request makes utilization scaling undefined, stale metrics can freeze the controller, and scaling only adds throughput when the bottleneck is inside stateless replicas.
kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa api.Strengths: Platform controls can wrap this exact behavior in policy or dashboards: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
Watch out: The added platform ownership buys value at this cost: HPA handles demand quickly but duplicates application startup costs and cannot solve a dependency, database, node capacity, or memory limit problem.
Interview signals & numbers to know
When this topic comes up
- Interview anchor: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
- Trace HPA to Autoscale through Metrics Server; the invariant is: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
- Recognize the symptom: No CPU request makes utilization scaling undefined, stale metrics can freeze the controller, and scaling only adds throughput when the bottleneck is inside stateless replicas.
- Justify the cost: HPA handles demand quickly but duplicates application startup costs and cannot solve a dependency, database, node capacity, or memory limit problem.
- Verify with kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa api instead of only inspecting YAML.
Benchmarks & rules of thumb
- 15s
- default HPA synchronization period
- 10%
- default utilization tolerance before HPA ignores a small ratio difference
- 2
- resource dimensions commonly used are CPU and memory
Common mistakes & follow-ups
Common interview mistakes
- Debugging only the child object misses the HPA owner contract: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies.
- Ignoring this telltale condition leads to unrelated changes: No CPU request makes utilization scaling undefined, stale metrics can freeze the controller, and scaling only adds throughput when the bottleneck is inside stateless replicas.
- Repeating
kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa apiwithout an owner for remediation wastes retry cost. HPA handles demand quickly but duplicates application startup costs and cannot solve a dependency, database, node capacity, or memory limit problem.
Likely interviewer follow-ups
- How would API output and runtime evidence prove this behavior: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies?
- Which owner object, logs, and Events would you inspect for this symptom-No CPU request makes utilization scaling undefined, stale metrics can freeze the controller, and scaling only adds throughput when the bottleneck is inside stateless replicas-?
- At what threshold would this cost become unacceptable: HPA handles demand quickly but duplicates application startup costs and cannot solve a dependency, database, node capacity, or memory limit problem?
kubectl get hpa api; kubectl top pods -l app=api; kubectl describe hpa api, name the failure, then justify the cost. Invariant: HPA queries Metrics Server resource metrics, computes a desired replica count from current usage against requested resources, and is stabilized by scaling policies. Failure: No CPU request makes utilization scaling undefined, stale metrics can freeze the controller, and scaling only adds throughput when the bottleneck is inside stateless replicas. Cost: HPA handles demand quickly but duplicates application startup costs and cannot solve a dependency, database, node capacity, or memory limit problem.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Horizontal Pod Autoscaler (HPA) & Metrics Server with generative AI interview Q&A and hands-on practice.