Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 248Kubernetes · Workloads

How to autoscale Pods with the HPA

Traffic is ten times higher on Monday morning than on Sunday night, and fixed replica counts either waste money or fall over. The HorizontalPodAutoscaler (HPA) adds Pods when average CPU (or memory, or a custom metric) goes above a target and removes them when load drops. This how-to checks the metrics server, sets requests, creates an HPA, tests it with load, and tunes how fast it scales.

  • 5 Lessons
  • 2 Commands

$ run as a normal user# run as root (or with sudo)

248.1

The quick answer

kubectl top pods -n shop
kubectl -n shop autoscale deployment api --cpu-percent=70 --min=2 --max=10
kubectl -n shop get hpa api --watch

248.2

Step by step

  1. 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.
  2. CPU requests are required — the target is a percentage of the request. Without resources.requests.cpu the HPA shows <unknown> and does nothing.
  3. Create the HPA (line 2), or as YAML with scaling behaviour:
hpa.yaml
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: 60

The scale-down window of 5 minutes stops "flapping" up and down.

  1. **Remove replicas from the Deployment in Git** — otherwise every deploy (or GitOps sync) resets the count and fights the HPA.
  2. 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.

  1. 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).
  2. 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.

248.3

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.

248.4

Common problems

ProblemCauseFix
TARGETS <unknown>No metrics server, or no CPU requests.Install metrics-server; set requests.cpu.
Replicas jump back after deployreplicas in the Deployment manifest.Remove it from Git; let the HPA own it.
Scales up but Pods stay PendingNo room on the nodes.Cluster Autoscaler; check quotas.
Flapping up and downShort windows, spiky load.stabilizationWindowSeconds; lower target.
Never scales on memoryApp keeps memory after load.Scale on CPU or requests per second instead.
Slow to react to sudden spikesMetrics every 15 s + Pod start time.Higher minReplicas for peaks; faster startup; scale-up policies.

248.5

Go further

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.

All commands in this chapter