✦ For everyone, free.

Practical knowledge for real and everyday life

Home

Kubernetes Horizontal Pod Autoscaler Management

Kubernetes Horizontal Pod Autoscaler Management automates scaling based on resource usage, ensuring efficient performance in containerized environments.

Kubernetes Horizontal Pod Autoscaler Management is the operational discipline of deploying, tuning, and maintaining HPA objects across a cluster's workloads, covering metric source configuration, boundary and behavior tuning, and the ongoing verification that each HPA continues to scale its target correctly as workload characteristics and traffic patterns evolve over time.


Configuring Metric Sources

Resource Metrics

The most common HPA configuration targets CPU or memory utilization sourced from the metrics-server, expressed as either a percentage of the pod's resource request or an absolute average value across all target pods.

metrics:
- type: Resource
  resource:
    name: memory
    target:
      type: AverageValue
      averageValue: 500Mi

Custom and External Metrics

Workloads whose load is better represented by an application-specific signal — queue depth, requests per second, active connections — can scale on custom metrics exposed through a metrics adapter implementing the custom or external metrics APIs, decoupling scaling decisions from generic resource consumption when that consumption does not track actual load well.

metrics:
- type: External
  external:
    metric:
      name: queue_messages_ready
      selector:
        matchLabels:
          queue: orders
    target:
      type: AverageValue
      averageValue: "30"

Tuning Boundaries and Behavior

Setting minReplicas and maxReplicas Deliberately

minReplicas should reflect the minimum capacity needed for baseline availability and load distribution even during genuinely idle periods, while maxReplicas should reflect a deliberate ceiling informed by downstream dependency capacity (database connections, rate limits) rather than an arbitrary large number chosen only to avoid ever hitting the cap.

spec:
  minReplicas: 3
  maxReplicas: 25

Configuring Scaling Behavior

The behavior field lets scale-up and scale-down be tuned independently, commonly favoring fast scale-up (to respond quickly to demand) paired with a more conservative, stabilized scale-down (to avoid prematurely removing capacity during a brief lull that could recur).

behavior:
  scaleUp:
    stabilizationWindowSeconds: 0
    policies:
    - type: Percent
      value: 100
      periodSeconds: 30
  scaleDown:
    stabilizationWindowSeconds: 300
    policies:
    - type: Pods
      value: 1
      periodSeconds: 60

Operating HPAs Day to Day

Monitoring Scaling Activity

Reviewing kubectl describe hpa output and the associated Kubernetes events reveals both the current scaling decision and the reasoning behind it, which is the first place to look when a workload appears under-provisioned despite an apparently reasonable HPA configuration.

kubectl describe hpa api-service -n payments

Diagnosing Failure to Scale

An HPA reporting AbleToScale: False or ScalingActive: False typically indicates a problem upstream of the scaling calculation itself — missing metrics-server, an unresolvable metric source, or an incorrectly configured scale target — and should be investigated before assuming the target utilization value is set incorrectly.

kubectl top pods -n payments
kubectl get apiservice v1beta1.metrics.k8s.io

Coordinating With Other Cluster Mechanisms

Pod Disruption Budgets

A PodDisruptionBudget set too restrictively can interfere with scale-down decisions elsewhere in the cluster (voluntary evictions during node drains, for example), though it does not directly block HPA-driven scale-down itself; reviewing both together avoids one mechanism's constraints producing confusing interactions with the other during cluster maintenance.

Cluster Autoscaler Interaction

Because increasing HPA-driven replica counts can exceed existing node capacity, HPA management should account for whether the Cluster Autoscaler is configured with sufficient headroom (maxReplicas values and node group maximums aligned) to actually provide the capacity the HPA might request during a genuine demand spike.


Reviewing and Adjusting Over Time

Revisiting Targets as Workload Characteristics Change

A target utilization value tuned for a workload's traffic pattern at one point in time may become poorly calibrated as the application's resource profile changes (a code change altering per-request CPU cost, for instance); periodically reviewing actual observed utilization against the configured target keeps the HPA's behavior aligned with the workload's current characteristics rather than its characteristics at initial configuration time.

Auditing HPA Coverage Across the Cluster

Enumerating which workloads have an HPA configured, and which do not, surfaces gaps where a workload experiencing variable load is instead running a fixed replica count, a common source of either over-provisioned idle capacity or under-provisioned peak capacity that periodic review should catch.

kubectl get hpa --all-namespaces