Why Kubernetes HPA Matters for Production Workloads
Every production cluster eventually faces the same painful moment: a traffic spike arrives, pods saturate their CPU, and response times collapse — until someone manually scales the deployment. Kubernetes HPA, the HorizontalPodAutoscaler, exists to solve exactly this problem. It watches resource metrics and adds or removes pod replicas automatically, so your application scales out under load and scales back down when traffic passes. Done well, a Kubernetes HPA setup saves you both money and sleep.

In this guide, you will install the Metrics Server, create a deployment with proper resource requests, configure a Kubernetes HPA with both the imperative command and a declarative autoscaling/v2 manifest, generate real load to watch it scale, and tune scaling behavior. Everything below is tested against a modern Kubernetes release (1.29+) where autoscaling/v2 is the only stable API for the autoscaler.
Prerequisites
Before you follow along, make sure you have:
A running Kubernetes cluster (kubeadm, k3s, minikube, kind, or a managed cluster) where you have cluster-admin or sufficient RBAC rights. If you are still learning the basics of cluster interaction, review our kubectl commands cheat sheet first. You also need kubectl installed and configured to reach the cluster, and your worker nodes need to be able to pull images from the public registries used below.
Step 1: Install the Metrics Server
A Kubernetes HPA cannot make scaling decisions without resource metrics, and those metrics come from the Metrics Server. It is a lightweight, in-cluster component that scrapes kubelet endpoints every 15 to 60 seconds and exposes CPU and memory usage through the Metrics API.
Install it with a single manifest:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
Wait for the deployment to become ready, then verify metrics are flowing:
kubectl wait --for=condition=available -n kube-system \
deployment/metrics-server --timeout=120s
kubectl top nodes
kubectl top pods -A
If kubectl top nodes returns real numbers instead of errors, the Metrics Server is working. On clusters with self-signed kubelet certificates (minikube and some kubeadm setups), the Metrics Server may report unable to fetch all metrics. The standard fix is to add --kubelet-insecure-tls to the Metrics Server deployment arguments — acceptable for a lab, but use proper certificate trust in production.
Step 2: Create a Deployment with Resource Requests
This is the step most tutorials gloss over, and it is the most common reason a Kubernetes HPA refuses to work: CPU utilization is computed as a percentage of the pod’s CPU request. A deployment without CPU requests gives the autoscaler nothing to divide by, so no scaling happens.
Create a demo deployment that includes realistic requests and limits:
cat <<'EOF' > php-apache.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-apache
spec:
replicas: 1
selector:
matchLabels:
app: php-apache
template:
metadata:
labels:
app: php-apache
spec:
containers:
- name: php-apache
image: registry.k8s.io/hpa-example:latest
ports:
- containerPort: 80
resources:
requests:
cpu: "200m"
limits:
cpu: "500m"
---
apiVersion: v1
kind: Service
metadata:
name: php-apache
spec:
type: ClusterIP
selector:
app: php-apache
ports:
- port: 80
targetPort: 80
EOF
kubectl apply -f php-apache.yaml
The hpa-example image runs a tiny Apache that computes square roots when asked — perfect for burning CPU on demand during load tests. For a deeper look at what Deployments and ReplicaSets actually do under the hood, see our article on Kubernetes Deployments and ReplicaSets explained.
Step 3: Create a Kubernetes HPA Imperatively
For a quick test, the fastest path to a working Kubernetes HPA is the kubectl autoscale command. It creates an autoscaling/v2 HorizontalPodAutoscaler immediately:
kubectl autoscale deployment php-apache --cpu-percent=50 --min=1 --max=5
Inspect what was created:
kubectl get hpa php-apache
Right after creation, you may see <unknown>/50% in the TARGETS column. That is normal — the autoscaler needs one or two scrape cycles from the Metrics Server before it can compute utilization. Within a minute, the output should settle:
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache Deployment/php-apache 0%/50% 1 5 1 45s
The imperative command is handy for experiments, but production configurations belong in version control as YAML.
Step 4: Define a Kubernetes HPA Declaratively with autoscaling/v2
Export the generated object as a starting point, then maintain it as a manifest:
kubectl get hpa php-apache -o yaml > hpa.yaml
A clean, production-style autoscaling/v2 manifest looks like this. Delete the imperative HPA first (kubectl delete hpa php-apache), then apply the file:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: php-apache-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
Key fields to understand:
scaleTargetRef points at the workload to scale — a Deployment, StatefulSet, or any scalable resource. minReplicas is the floor the autoscaler will never go below, and maxReplicas is the hard ceiling that protects your cluster (and your cloud bill) from runaway scaling. The metrics block defines the trigger: here, average CPU utilization of 50 percent across all pods.
Always use autoscaling/v2 in new manifests. The older autoscaling/v1 supports only CPU utilization and is frozen; v2beta2 was removed in Kubernetes 1.26, so any manifest still referencing it will be rejected by modern clusters.
Step 5: Load Test and Watch the Kubernetes HPA Scale
Now generate real CPU load and watch the autoscaler react. Run a temporary busybox pod that hits the demo application in a tight loop:
kubectl run load-generator --rm -i --tty --image=busybox:1.36 \
--restart=Never -- /bin/sh -c \
"while true; do wget -q -O- http://php-apache; done"
In a second terminal, watch the Kubernetes HPA:
kubectl get hpa php-apache-hpa -w
Within a minute or two, you should see TARGETS climb past 50 percent and REPLICAS increase:
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE
php-apache-hpa Deployment/php-apache 12%/50% 2 10 2 3m
php-apache-hpa Deployment/php-apache 148%/50% 2 10 2 4m
php-apache-hpa Deployment/php-apache 96%/50% 2 10 5 5m
Press Ctrl+C in the load generator terminal, and the Kubernetes HPA will scale the deployment back down after the default five-minute stabilization window. This cooldown prevents flapping, where replicas oscillate rapidly around the target.
Step 6: Fine-Tune Kubernetes HPA Scaling Behavior
The defaults are sensible, but real workloads often need tighter control. The behavior field lets you configure stabilization windows and scaling policies for both directions:
behavior:
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 100
periodSeconds: 60
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Pods
value: 1
periodSeconds: 60
This says: scale up aggressively (double the pods per minute if needed, with no stabilization delay), but scale down conservatively — at most one pod per minute, and only after metrics have stayed low for five minutes. Asymmetric behavior like this is a classic production pattern: fast reactions to spikes, cautious reactions to quiet periods.
Scaling a Kubernetes HPA on Memory Too
CPU is not always the right signal. You can add a memory metric to the same manifest — the autoscaler computes a desired replica count from each metric and takes the maximum:
- type: Resource
resource:
name: memory
target:
type: AverageValue
averageValue: 300Mi
Note the different target type: memory uses an absolute AverageValue (per pod) rather than a percentage of request, because memory utilization percentages are misleading when limits differ from requests.
Troubleshooting Kubernetes HPA Issues
TARGETS stuck at <unknown>. The Metrics Server is not serving metrics yet. Check kubectl top nodes — if it errors, fix the Metrics Server deployment (the insecure-TLS flag on lab clusters, network policies blocking kubelet port 10250, or a Metrics Server pod stuck in CrashLoopBackOff).
Replicas never change despite high CPU. Verify the deployment sets CPU requests (kubectl describe deployment php-apache). Utilization without requests cannot be computed. Also confirm the target CPU threshold is actually being exceeded — kubectl top pods shows current usage in millicores.
Scaling thrashes up and down. Your thresholds are too tight or your pods have slow startup times. Increase the scale-down stabilization window and make sure readiness probes are configured so new pods receive traffic only when truly ready.
HPA ignores maxReplicas ceiling. It does not — double-check you are looking at the right HPA object and that another controller (Cluster Autoscaler settings, VPA in auto mode) is not fighting it.
Events explain everything. When in doubt, kubectl describe hpa php-apache-hpa and read the Events section — the autoscaler logs every scaling decision and every reason it could not scale.
Conclusion
A Kubernetes HPA is one of the highest-leverage objects in a cluster: a single manifest that turns static replica counts into elastic capacity. The recipe is straightforward — install the Metrics Server, set CPU requests on every workload, define a Kubernetes HPA with autoscaling/v2, validate with a load test, and tune scaling behavior to match your application’s rhythm. For full API details and advanced scenarios like custom and external metrics, the official documentation at kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/ is the authoritative reference. Start with CPU, add memory when it makes sense, and let the autoscaler do the night-shift scaling for you.