$ run as a normal user# run as root (or with sudo)
The quick answer
Step by step
- Metrics server — the HPA reads CPU/memory from it. If
kubectl top pods(line 1) fails with "Metrics API not available", install metrics-server first. - CPU requests are required — the target is a percentage of the request. Without
resources.requests.cputhe HPA shows<unknown>and does nothing. - Create the HPA (line 2), or as YAML with scaling behaviour:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: api
namespace: shop
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: api
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 50
periodSeconds: 60The scale-down window of 5 minutes stops "flapping" up and down.
- **Remove
replicasfrom the Deployment in Git** — otherwise every deploy (or GitOps sync) resets the count and fights the HPA. - Test with load — for example k6, or a simple loop from a test Pod:
kubectl -n shop run load --rm -it --image=busybox:1.36 --restart=Never -- sh -c 'while true; do wget -q -O- http://api/ >/dev/null; done'Watch (line 3): TARGETS rises above 70 %, REPLICAS grows; after stopping the load it shrinks again after the window.
- Other metrics — memory (careful: many apps do not release memory), or requests per second via a custom metrics adapter (Prometheus Adapter, KEDA for queues and events).
- Node capacity — more Pods need room; combine the HPA with the Cluster Autoscaler (or Karpenter) so nodes are added too, and a PodDisruptionBudget for safe scale-downs.
Check that it worked
kubectl -n shop get hpa api shows a real value in TARGETS (for example 23%/70%), not <unknown>; under load REPLICAS goes up to the needed number (at most 10); kubectl -n shop describe hpa api lists the scaling events with reasons; after the load ends replicas return to 2.
Common problems
| Problem | Cause | Fix |
|---|---|---|
TARGETS <unknown> | No metrics server, or no CPU requests. | Install metrics-server; set requests.cpu. |
| Replicas jump back after deploy | replicas in the Deployment manifest. | Remove it from Git; let the HPA own it. |
Scales up but Pods stay Pending | No room on the nodes. | Cluster Autoscaler; check quotas. |
| Flapping up and down | Short windows, spiky load. | stabilizationWindowSeconds; lower target. |
| Never scales on memory | App keeps memory after load. | Scale on CPU or requests per second instead. |
| Slow to react to sudden spikes | Metrics every 15 s + Pod start time. | Higher minReplicas for peaks; faster startup; scale-up policies. |
Go further
- Command pages: kubectl, k6.
- Next how-tos: Set resource requests and limits, See CPU and memory of Pods and nodes, Load test an API with k6, Scale a deployment up and down.
- Learn it: CKA — Scale with HPA, CKA — Requests and limits.
1Why does an HPA need CPU requests on the Pods?
The CPU target is a percentage of the request; without a request the HPA cannot calculate it.
2Why remove replicas from the Deployment manifest when using an HPA?
Otherwise each deploy or GitOps sync resets the replica count and fights the HPA.