Static replica counts cannot keep up with real traffic: too few pods and users wait, too many and you pay for idle capacity. Kubernetes HPA, the HorizontalPodAutoscaler, solves this by adding or removing pod replicas automatically based on observed metrics such as CPU and memory utilization. This guide shows how to configure Kubernetes HPA for a real workload, choose sane thresholds, and avoid the flapping and thrashing that make autoscaling feel unreliable.
You will deploy a sample application, set resource requests correctly, because HPA cannot work without them, create an autoscaler for CPU and memory, and test it under load. The examples use the metrics server and the stable autoscaling/v2 API, which is available in all supported Kubernetes versions.

Prerequisites for Kubernetes HPA
You need a running Kubernetes cluster with the metrics server installed, kubectl configured, and permission to create Deployments and HorizontalPodAutoscalers. Check the metrics server first, because without it CPU and memory metrics are simply unavailable:
kubectl top nodes
kubectl top pods -A
If those commands fail, install the metrics server for your cluster before continuing. You also need a workload with resource requests set. HPA calculates utilization as a percentage of the requested resources, not of the node capacity, so a pod without requests has no meaningful utilization value.
Step 1: Deploy a Workload with Resource Requests
Deploy a simple web application with explicit CPU and memory requests and limits. Requests are what HPA and the scheduler use; limits are the ceiling that triggers throttling or termination. A common starting point is a request of 250 millicores of CPU and 256 MiB of memory, with limits roughly double the requests:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
resources:
requests:
cpu: 250m
memory: 256Mi
limits:
cpu: 500m
memory: 512Mi
Apply it and confirm the pods are running and reporting metrics:
kubectl apply -f web.yaml
kubectl get pods -l app=web
kubectl top pods -l app=web
If your application is memory heavy or CPU heavy in a different ratio, adjust the requests to match its real profile. Guessing here is the most common cause of poor autoscaling, so measure with kubectl top under normal load before choosing thresholds.
Step 2: Create a Kubernetes HPA for CPU and Memory
Create a HorizontalPodAutoscaler that targets the Deployment, scales between 2 and 10 replicas, and aims for 60 percent average CPU utilization and 70 percent average memory utilization. Keeping the target below 100 percent leaves headroom for traffic spikes while new pods start:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 70
kubectl apply -f web-hpa.yaml
kubectl get hpa web-hpa
kubectl describe hpa web-hpa
When multiple metrics are configured, HPA scales to satisfy the metric that demands the most replicas. That is the safe behavior: a workload that is memory bound should not be held back by a low CPU reading.
Step 3: Test Kubernetes HPA Under Load
Generate load against the service and watch the replica count respond. A simple load generator pod running a loop of requests is enough for a test; in production, use your real traffic patterns or a proper load testing tool:
kubectl run -i --tty load-generator --rm \
--image=busybox --restart=Never -- \
/bin/sh -c "while true; do wget -q -O- http://web; done"
kubectl get hpa web-hpa -w
Expect a delay before scaling: the metrics server scrapes on an interval, and HPA evaluates on its own sync period, so allow a minute or two. When load stops, replicas scale back down more slowly by design, to avoid flapping when traffic dips briefly.
Step 4: Tune Scaling Behavior to Prevent Flapping
The default HPA behavior is reasonable, but production workloads often need explicit stabilization. The behavior field controls how fast scaling happens in each direction. For example, limit scale-down to at most 50 percent of pods per minute, and require a five-minute stabilization window before scaling down:
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 50
periodSeconds: 60
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 100
periodSeconds: 60
Add this block under spec in the HPA manifest. Fast scale-up with slow, stabilized scale-down is the right default for user-facing services: you would rather over-provision briefly than drop requests during a spike. Batch workloads can use the opposite tradeoff.
Troubleshooting Kubernetes HPA
If HPA shows <unknown> targets, the metrics server is missing or cannot scrape the pods, or the pods have no resource requests. Run kubectl describe hpa and read the events and conditions; they usually name the exact failure, such as “failed to get cpu utilization” or “missing metrics”.
If replicas never increase under load, verify that the load actually reaches the pods and raises utilization above the target. A load generator hitting a cached response, or a service selector pointing at the wrong pods, produces no pressure at all. Check kubectl top pods during the test to confirm utilization moves.
If replicas flap up and down, widen the stabilization window, raise minReplicas, or raise the target utilization. Also check whether a memory leak is driving memory-based scaling: a pod whose memory grows without bound will scale forever, and the fix is in the application, not in the autoscaler.
Conclusion
Kubernetes HPA turns replica management from guesswork into a feedback loop, but it only works when the inputs are honest: real resource requests, a working metrics server, and thresholds with headroom for spikes. Start with CPU and memory, tune the scale-down behavior for your traffic shape, and test under load before trusting it in production. For related workload protection, see our guides on Kubernetes resource limits and requests and PodDisruptionBudgets. The official HorizontalPodAutoscaler documentation covers custom and external metrics when you outgrow CPU and memory.