Horizontal Pod Autoscaler

The HPA automatically adds or removes Pod replicas based on observed CPU, memory, or custom metrics.

Traffic rises and falls. Manually editing replicas is slow and wasteful. The Horizontal Pod Autoscaler (HPA) scales a Deployment automatically to match load.

How it decides

You give the HPA a target, such as "keep average CPU at 50%". It watches the metric and:

  • Adds Pods when the metric is above target.
  • Removes Pods when it is comfortably below.

You also set a minReplicas and maxReplicas so it never scales to zero or to infinity.

Prerequisites

The HPA needs a metrics source (the Metrics Server for CPU/memory), and your Pods must declare resource requests β€” the percentage is measured against them.

Example

Example Β· yaml
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: 50

When to use it

  • An e-commerce site uses HPA so its product-search API automatically scales from 3 to 20 replicas during flash sale traffic spikes.
  • A data processing service uses HPA with custom metrics (queue depth) to add workers when the message queue grows beyond 100 items.
  • A team reduces cloud costs by setting HPA minReplicas=2 so the cluster scales down idle deployments overnight without manual intervention.

More examples

HPA based on CPU utilization

Creates an HPA that keeps average CPU utilization at 60%, scaling the web deployment between 2 and 20 replicas.

Example Β· yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web
  minReplicas: 2
  maxReplicas: 20
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 60

Inspect HPA scaling events

Shows current CPU utilization vs. target and the scaling events that Kubernetes has taken to maintain the target.

Example Β· bash
kubectl get hpa web-hpa
# NAME     REFERENCE         TARGETS   MINPODS  MAXPODS  REPLICAS
# web-hpa  Deployment/web    85%/60%   2        20       5

kubectl describe hpa web-hpa | grep -A5 'Events:'

Scale down behaviour tuning

Configures a 5-minute stabilization window and limits scale-down to 25% of pods per minute to prevent thrashing.

Example Β· yaml
spec:
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
        - type: Percent
          value: 25
          periodSeconds: 60

Discussion

  • Be the first to comment on this lesson.
Horizontal Pod Autoscaler β€” Kubernetes | SoundsCode