Horizontal Pod Autoscaler

Scale Deployments automatically based on CPU or memory metrics and understand how HPA works with metrics-server.

What Is HPA?

The Horizontal Pod Autoscaler (HPA) changes the number of pod replicas based on observed metrics such as CPU or memory utilization.

Quick Imperative Example

kubectl autoscale deployment web --cpu-percent=70 --min=2 --max=10

YAML Example

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: 70
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: 75

Inspect HPA

kubectl get hpa

How HPA Gets Metrics

HPA commonly reads metrics from metrics-server. Without metrics-server, kubectl top and resource-based autoscaling often do not work.

Cooldown and Stability

Autoscaling should not flap up and down wildly. HPA behavior settings and controller logic help smooth changes over time.

Real-World Example

An API normally runs 2 pods. During peak traffic, average CPU climbs above 70 percent. HPA increases replicas to 6. When load drops and metrics stabilize, it scales back down within the configured bounds.

Why HPA Is Useful

  • improves responsiveness during load spikes
  • reduces manual scaling work
  • can lower cost compared to overprovisioning constantly

HPA is like adding checkout counters in a store when lines get long, then closing extra counters after the rush ends.

Exercise

Scale Direction

What does the Horizontal Pod Autoscaler change?

Exercise

Dependency

Which component is commonly needed for `kubectl top` and basic CPU based HPA metrics?

Continue Learning

Explore Related Topics

Try the Tool

Related Resources