22 - Autoscaling (HPA, VPA, Cluster Autoscaler, KEDA)
Why this matters
Static replica counts and static node pools waste money or fall over under load. Real prod clusters scale both pods and nodes dynamically based on demand.
Read this first — Definitions & Explanations
HPA (Horizontal Pod Autoscaler)
Scales replica count based on metrics (CPU, memory, or custom metrics). Needs metrics pipeline (often metrics-server for resource metrics).
VPA (Vertical Pod Autoscaler)
Recommends or applies better requests/limits for Pods. Different problem than HPA.
Cluster Autoscaler
Adds/removes nodes when Pods are unschedulable or nodes are underused. Works with cloud node groups / autoscaling groups.
KEDA
Event-driven autoscaling — scale on queue depth, Kafka lag, etc., including scale to zero patterns.
Autoscaling hygiene
Garbage in, garbage out: if requests are wrong, HPA/Cluster Autoscaler behave badly. Set realistic requests first.
Official docs (read for detail)
- Horizontal Pod Autoscaling
- HorizontalPodAutoscaler Walkthrough
- Vertical Pod Autoscaler
- Cluster Autoscaler
- KEDA Documentation
Key Concepts
- HPA (Horizontal Pod Autoscaler): scales replica count based on metrics (CPU/memory by default, custom metrics via adapters)
- VPA (Vertical Pod Autoscaler): adjusts requests/limits automatically (careful: restarts pods to resize)
- Cluster Autoscaler: adds/removes nodes based on unschedulable pods / underutilized nodes
- KEDA: event-driven autoscaling — scale on queue depth, Kafka lag, cron schedule, etc. (not just CPU)
- HPA needs metrics-server (or custom/external metrics API) to function at all
- Scaling behavior tuning: stabilization windows, scale-up/down policies (avoid flapping)
YouTube search terms
- "Kubernetes HPA horizontal pod autoscaler tutorial"
- "Kubernetes VPA vertical pod autoscaler explained"
- "Cluster Autoscaler Kubernetes explained"
- "KEDA event driven autoscaling tutorial"
Hands-on lab (on prod-sim)
# HPA — you already have metrics-server enabled
kubectl create deployment php-apache --image=k8s.gcr.io/hpa-example --requests='cpu=200m'
kubectl expose deployment php-apache --port=80
kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=5
kubectl get hpa -w &
# Generate load and watch it scale up
kubectl run load-generator --image=busybox --restart=Never -- \
sh -c "while true; do wget -q -O- http://php-apache; done"
sleep 60
kubectl get hpa php-apache
kubectl get pods -l app=php-apache
kubectl delete pod load-generator # stop load, watch it scale back down after stabilization window
kill %1
# VPA (install the components first: https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler)
cat <<EOF | kubectl apply -f -
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: php-apache-vpa
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
updatePolicy:
updateMode: "Auto"
EOF
kubectl describe vpa php-apache-vpa # see recommended requests
# KEDA
helm repo add kedacore https://kedacore.github.io/charts
helm install keda kedacore/keda -n keda --create-namespace
# scale a deployment based on a cron schedule as the simplest possible example
cat <<EOF | kubectl apply -f -
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: cron-scaledobject
spec:
scaleTargetRef:
name: php-apache
minReplicaCount: 1
maxReplicaCount: 5
triggers:
- type: cron
metadata:
timezone: UTC
start: "0 * * * *"
end: "5 * * * *"
desiredReplicas: "3"
EOF
Common mistakes and how to avoid them
- Keeping
minReplicas: 1on a latency-sensitive service. A spike hits a single Pod while metrics are collected and new Pods start. Keep a floor that can absorb a spike for the minute or two scale-up takes. - Leaving
replicasin the manifest while an HPA manages the Deployment. Re-applying the manifest resets the replica count and fights the HPA. Removespec.replicasonce an HPA owns scaling. - Expecting the Cluster Autoscaler to react to CPU usage. It adds nodes when Pods are unschedulable, not when nodes are busy. Pods without resource requests, or with a nodeSelector no node group satisfies, will not trigger it.
- Running VPA in an updating mode on a single-replica workload. VPA applies new requests by evicting Pods, which means restarts. Start in recommendation-only mode (
updateMode: "Off") and read the suggestions first. - Expecting HPA to scale to zero. The standard HPA keeps at least one replica unless an alpha feature gate is enabled. Use KEDA when you need scale to zero.
Quick answers
What is the difference between HPA, VPA and Cluster Autoscaler?
HPA changes the number of Pod replicas, VPA changes the CPU and memory requests of each Pod, and the Cluster Autoscaler adds or removes nodes when Pods cannot be scheduled or nodes sit mostly empty. They act on different layers, so pair HPA with the Cluster Autoscaler and be careful combining HPA with VPA.
How does the HPA calculate the desired replica count?
It scales the current replica count by the ratio of the current metric to the target: desired = ceil(current replicas × current value ÷ target value). With 4 replicas at 80% CPU against a 50% target it asks for ceil(4 × 80 ÷ 50) = 7 replicas.
When would you use KEDA?
Use KEDA when scaling should follow an external event source such as queue length, Kafka lag or a cron schedule, not only CPU or memory. It can also scale a workload down to zero and back up, which the standard HPA does not do by default.
Notes
(fill in your own words after watching + labbing)