Kubernetes · Orchestration under pressure
How does the Horizontal Pod Autoscaler work?
Short answer
HPA adjusts replica counts based on observed metrics versus targets. It needs a metrics source, works best with well-defined requests, and reacts on a delay rather than instantly.
The HPA controller periodically reads metrics and computes a desired replica count. For CPU, the target is a percentage of requests, so missing requests make CPU-based scaling meaningless. HPA can also use memory, custom metrics or external metrics through adapters.
- metrics-server is enough for CPU and memory, but not for custom metrics
- Scale-up is usually fast; scale-down is stabilized to avoid flapping
- HPA scales pods, not nodes; cluster autoscaler or Karpenter handles nodes
- Metrics must be available for all targeted pods or the calculation is incomplete
- HPA and a Deployment with a fixed replica count conflict over the same field
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api
minReplicas: 3
maxReplicas: 30
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 65Autoscaling is only useful if the application scales horizontally and starts fast enough. Cold starts, connection pools and shared caches can make extra replicas ineffective. Tune the stabilization window and scaling policies to match traffic patterns, and test scale-up under realistic load.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why does CPU-based HPA require resource requests?
- 2What is the difference between HPA and cluster autoscaler?
- 3How do you stop HPA from flapping?
- 4When would custom metrics be better than CPU?
Say it in the interview
HPA changes replica counts from observed metrics against targets. CPU scaling is based on requests, so missing requests or missing metrics break it. I usually start with CPU and add custom metrics when the real bottleneck is queue depth or latency. I also remember that HPA scales pods only, so node capacity has to come from a cluster autoscaler or Karpenter.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Kubernetes mock interview and get scored feedback on depth, clarity and confidence.