Horizontal Pod Autoscaling (HPA) automatically adjusts the number of running Pod replicas based on observed CPU, memory, or custom metrics — scaling up under load and back down when demand drops, without manual intervention.
Prerequisites
- A working Kubernetes cluster with the Metrics Server installed
- An existing Deployment to scale
Step 1 — Install the Metrics Server (If Not Already Present)
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
The Metrics Server collects resource usage data that HPA relies on to make scaling decisions.
Step 2 — Verify Metrics Are Available
kubectl top nodes
kubectl top pods
Step 3 — Ensure Your Deployment Has Resource Requests Defined
spec:
containers:
- name: my-app
image: my-app:latest
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
HPA calculates scaling decisions as a percentage of the requests value — without this defined, autoscaling based on CPU/memory won't function correctly.
Step 4 — Create an HPA Resource
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
kubectl apply -f hpa.yaml
This configuration keeps average CPU utilization around 70%, scaling between 2 and 10 replicas as needed.
Step 5 — Create via Command Line (Alternative, Quicker)
kubectl autoscale deployment my-app --cpu-percent=70 --min=2 --max=10
Step 6 — Monitor Autoscaling in Action
kubectl get hpa my-app-hpa --watch
Shows current CPU utilization vs target, and current replica count, updating live.
Testing Autoscaling with a Load Generator
kubectl run load-test --image=busybox --restart=Never -- /bin/sh -c \
"while true; do wget -q -O- http://my-app-service; done"
Generates continuous load against your service to observe the HPA scale up in real time; delete this test Pod once done (kubectl delete pod load-test).
Scaling on Memory Instead of (or Alongside) CPU
metrics:
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
Common Errors
HPA shows "unknown" for current metrics — the Metrics Server isn't installed or isn't functioning correctly; verify with kubectl top pods first.
HPA never scales despite high load — verify resource requests are actually defined on the target Deployment's containers; this is the single most common oversight.
FAQ
Does HPA also scale the underlying VPS/node resources?
No — HPA only scales the number of Pod replicas within existing node capacity; scaling the nodes themselves (Cluster Autoscaler) is a separate, more advanced mechanism not typically applicable to a fixed set of VPS instances.
Continue Reading
- How to Monitor a Kubernetes Cluster with Prometheus and Grafana
- How to Set Up Rolling Updates and Rollbacks in Kubernetes
- Kubernetes Pods, Deployments & Services Explained
Browse more articles in Kubernetes & Container Orchestration.