Lesson 3 of 19 · 18m
Autoscaling: HPA, KEDA, and the Layer Below
Utilization is a fraction of requests, not of the node — and HPA can't place pods on nodes that don't exist.
Traffic doubles at 9am, pods melt, and someone asks why the cluster "didn't autoscale." Autoscaling in Kubernetes is three different systems solving three different timescales, and the usual failure is deploying one of them and expecting the other two. HPA changes pod count in seconds; the cluster autoscaler changes node count in minutes; VPA changes pod size over restarts. Confuse them and you get the classic shape: HPA scaling replicas into a cluster that has nowhere to put them.
HPA: utilization is a fraction of requests
The HorizontalPodAutoscaler (autoscaling/v2) adjusts replica count against a metric:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api
minReplicas: 3
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
behavior:
scaleDown:
stabilizationWindowSeconds: 300Here's the mental model to kill: averageUtilization: 70 is not "70% of the node's CPU." It's usage / requests.cpu, averaged across pods. No requests → no meaningful percentage → the HPA can't compute the metric at all (this is also why kubectl top numbers that look fine can accompany an HPA doing nothing). Set requests at 500m and run at 400m and you're at 80% of request — nowhere near the node's capacity, but the HPA scales anyway. Which is correct: requests were your contract.
Metric sources: Resource metrics (cpu/memory) come from metrics-server — the same pipeline as kubectl top. Custom metrics (requests-per-pod from Prometheus) need the Prometheus adapter. External metrics (queue depth, consumer lag) are where KEDA lives — it runs its own metric server, scales on 60+ scalers from SQS to Kafka lag, and can scale to zero, which HPA alone cannot do: minReplicas is at least 1 in practice for HPA-managed objects.
The failure modes
Flapping. Load wobbles around the target, replicas go 5→8→5→8, each churn costs you cold starts. behavior.scaleDown.stabilizationWindowSeconds (default 300) makes the HPA pick the highest recommendation inside the window before scaling down — scale up fast, scale down slow.
Scaling into a wall. HPA adds replicas; the cluster has no headroom; they go Pending with Insufficient cpu. The fix isn't a bigger HPA — it's the layer below.
The layer below: cluster autoscaler and Karpenter
The cluster autoscaler watches for Pending pods that can't be placed and adds nodes to a node group; it removes underutilized nodes when their pods can move elsewhere. Karpenter does the same job with a different philosophy — provision nodes directly against pod requirements instead of sizing fixed node groups. Either way: minutes, not seconds. PodsPending → node provisioned → scheduled → image pulled → warm. That's why HPA targets belong on a metric that leads the pain (queue lag beats CPU for workers) — CPU is a lagging indicator by the time it trips.
The interplay that bites: autoscaler scale-down respects PDBs (voluntary disruption — the drain lesson) and refuses to evict pods with local storage, pods not managed by a controller, and pods that can't fit anywhere else. Your cluster won't shrink because three namespaces nobody owns hold it open.
The third axis is pod size: VPA recommends or sets requests, and in-place pod resize — adjusting requests/limits on a running container without restart — went beta in Kubernetes 1.33, which closes a long-standing annoyance for JVM services that can't be bounced casually.
Practice
Order matters: honest requests first, then HPA on a real metric, then node autoscaling underneath. Prove it under load, not in review: kubectl run -i load --image=busybox -- /bin/sh -c 'while true; do wget -q -O- http://api; done' against a staging deployment and watch the full pipeline — HPA scales pods, autoscaler catches the Pending, nodes arrive. If any link is missing you'll see it in kubectl describe hpa and kubectl get events in under five minutes.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
What is Platform as a Service? The Sweet Spot Between "Too Easy" and "Too Much Work"
Platform as a Service sits between raw servers and fully managed software. You push code, the platform runs it. No Nginx config, no OS patches, no 3 AM server alerts. Here is what PaaS actually gives you, what it costs you in control, and how to decide if it is the right layer for your application.
The World now is Serverless and Container based
A new report from Datadog reveals that serverless computing could be entering the mainstream. This has already been the case in my world for the past few years. Results show that more than half of all organizations are using serverless on one of the three major public cloud providers: Amazon,
PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.
PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...
Self-Hosting Prometheus and Grafana: Infrastructure Monitoring That Actually Works
Build a production-grade monitoring stack with Prometheus and Grafana from scratch. Covers architecture, Docker Compose deployment, Node Exporter, alerting with Alertmanager, PromQL from basics to advanced, pre-built dashboards, and everything SudoSecurity uses to monitor its own infrastructure.