Overview
Kubernetes scales at two levels. Pod scaling changes how many replicas run (Horizontal Pod Autoscaler, HPA) or how much CPU and memory each gets (Vertical Pod Autoscaler, VPA). Node scaling changes how many machines the cluster has (Cluster Autoscaler or Karpenter), adding nodes when pods cannot be scheduled and removing underused ones.
HPA scales on CPU, memory, or custom metrics like requests per second or queue depth; KEDA extends this to event sources (Kafka lag, SQS depth) and can scale to zero. Correct resource requests are essential, because the scheduler and autoscalers make decisions based on them.
When lines grow, the manager opens more lanes (pods). When all lanes are open and there is still a line, the store adds a temporary annex with more lanes (nodes). At night, lanes and annexes close to save money.
When to use it
- Services with daily or seasonal traffic patterns.
- Workers that should scale with queue backlog.
- Reducing cost by removing idle nodes.
- Handling spikes without manual intervention.
Where it shows up in interviews
Recognize it when: requests vary through the day.
- Design an e-commerce API for Black Friday
- Handle viral traffic spikes
Recognize it when: workers should match queue depth.
- Design a video processing fleet
- Design a notification worker pool
Where it is used in real software
AWS's open-source node provisioner launches right-sized nodes in seconds based on pending pods, including spot instances.
Kubernetes Event-driven Autoscaling scales on 60+ event sources and to zero.
Google manages nodes entirely; you pay per pod resources.
Key terms
- HPA
- Adjusts replicas based on metrics.
- VPA
- Adjusts pod CPU and memory requests.
- Cluster Autoscaler / Karpenter
- Adds and removes nodes.
- Resource requests
- Guaranteed CPU/memory used for scheduling.
- PodDisruptionBudget
- Limits voluntary evictions during scale-down.
How it works, step by step
- 1Set accurate requests
Based on observed usage.
- 2Configure HPA
Target 60-70% CPU or a custom metric.
- 3Pods pending?
Node autoscaler adds nodes.
- 4Load drops
HPA reduces replicas after a stabilization window.
- 5Nodes underused
Autoscaler drains and removes them, respecting PDBs.
HPA calculation
desiredReplicas = ceil(currentReplicas x currentMetric / targetMetric)
| Current replicas | Avg CPU | Target | Desired replicas |
|---|---|---|---|
| 4 | 90% | 60% | ceil(4 x 90 / 60) = 6 |
| 6 | 65% | 60% | ceil(6 x 65 / 60) = 7 |
| 7 | 30% | 60% | ceil(7 x 30 / 60) = 4 (after stabilization) |
| 4 | 0% (no traffic) | 60% | minReplicas (e.g. 2) |
NOWCurrent replicas: 4 | Avg CPU: 90% | Target: 60% | Desired replicas: ceil(4 x 90 / 60) = 6
HPA reacts proportionally; the scale-down stabilization window prevents flapping.
Implementation
apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata: { name: api }spec: scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: api } minReplicas: 3 maxReplicas: 60 metrics: - type: Resource resource: { name: cpu, target: { type: Utilization, averageUtilization: 65 } } behavior: scaleDown: { stabilizationWindowSeconds: 300 }---apiVersion: keda.sh/v1alpha1kind: ScaledObjectmetadata: { name: thumbnail-worker }spec: scaleTargetRef: { name: thumbnail-worker } minReplicaCount: 0 # scale to zero when the queue is empty maxReplicaCount: 100 triggers: - type: aws-sqs-queue metadata: { queueURL: "https://sqs.us-east-1.amazonaws.com/123456789012/thumbnails", queueLength: "20", awsRegion: us-east-1 }Complexity and performance
If nodes have room.
Instance launch + image pull.
Trade-offs
Low targets and spare nodes react fast to spikes but waste capacity; overprovisioning with low-priority placeholder pods is a common middle ground.
They conflict when both act on CPU; use VPA for recommendations or different metrics.
Variants and related techniques
Pre-scale before known events (sales, games).
KEDA or Knative for idle services.
Common mistakes
- No resource requests.
Fix: HPA utilization cannot be computed and scheduling is unreliable.
- Scaling on CPU for I/O-bound services.
Fix: Scale on RPS, latency, or queue depth.
- Slow startup.
Fix: Large images and long warm-up delay scaling; optimize startup and readiness.
Interview questions
How does Kubernetes handle a traffic spike?
HPA sees metrics above target and adds replicas; if pods cannot fit, they stay pending and the cluster autoscaler or Karpenter adds nodes; the Service starts routing to new pods once readiness probes pass.
How would you scale queue workers?
Use KEDA or an HPA on an external metric like queue depth or consumer lag, with scale to zero when idle and a max based on downstream capacity.
Practice problems
| Problem | Difficulty | What it trains |
|---|---|---|
| Configure HPA with a custom RPS metric | Medium | Metrics. |
| Plan scaling for a 10x flash sale | Hard | Pre-scaling and node headroom. |