Sobes.tech
Senior

What is HPA (Horizontal Pod Autoscaler)? How does it work?

sobes.tech AI

Answer from AI

Horizontal Pod Autoscaler (HPA) is a Kubernetes component that automatically scales the number of pods in a deployment, replica set, or stateful set based on current load. Typically, HPA uses metrics such as CPU utilization or custom metrics to determine whether to increase or decrease the number of replicas.

Working principle:

  • HPA periodically queries metrics (e.g., CPU usage) from pods.
  • Compares current values with set targets.
  • If the load exceeds the target, it increases the number of pods; if lower, it decreases.

Example of an HPA manifest that scales a deployment based on CPU:

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: example-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: example-deployment
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50

Thus, HPA helps automatically adapt the number of pods to current load, ensuring a balance between performance and resources.