liveBook · Manning

HorizontalPodAutoscaler

As explained in Kubernetes for Developers

A pod autoscaler is a Kubernetes controller that adjusts the number of workload replicas according to resource or application metrics.

A traffic spike may need more copies of an application, while a quiet period may need fewer. A HorizontalPodAutoscaler (HPA) adjusts a workload’s Pod replica count in response to a metric. This is horizontal scaling: adding or removing replicas rather than changing the resources available to each one. section 6.2

Listing 6.2 Chapter06/6.2_HPA/hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: timeserver
spec:
  minReplicas: 1                 #1
  maxReplicas: 10                #2
  metrics:
  - resource:
      name: cpu
      target:
        averageUtilization: 20   #3
        type: Utilization
    type: Resource
  scaleTargetRef:                #4
    apiVersion: apps/v1          #4
    kind: Deployment             #4
    name: timeserver             #4

This HPA configuration targets a Deployment, keeps between one and ten replicas, and aims for average CPU utilization of 20% of the Pods’ requested CPU. section 6.2

For a CPU target, the HPA compares average utilization across the Pods with the configured percentage of their CPU requests. It adds replicas above the target and removes them below it, subject to replica limits and cooldown periods. Set resource requests on the Deployment so this comparison works correctly. An HPA scales Pods, not nodes: the cluster still needs capacity to run them. On GKE Autopilot, node capacity is provisioned automatically as Pod demand changes. section 6.2 section 6.1

Test scaling under load

A Deployment starts with one replica and exposes a CPU-intensive URL that calculates pi. After creating the Deployment, Service, and HPA, generate requests with Apache Bench. Use `kubectl top pods` to inspect CPU use and `kubectl get deploy,hpa,pods` to watch the target, replica count, and Pod status change. The same CPU-based approach can scale a Deployment of CPU-intensive background workers. section 6.2 section 10.1.3

CPU is not the only possible signal. Requests per second (RPS) can express traffic in terms of a measured per-replica capacity. An HPA can use an external RPS metric from a monitoring solution; the illustrated HTTP setup uses an Ingress, which can observe requests. Choosing that metric requires the appropriate monitoring metric and, in the Google Cloud example, the Ingress forwarding-rule identifier. section 6.2.1

How Kubernetes for Developers covers it

How deep it goes
Works through CPU-target configuration and load testing, then extends to external metrics without detailing Ingress setup.
Examples
Uses a pi-calculating HTTP endpoint, Apache Bench load, an external RPS metric, and CPU-intensive worker Pods.
What it assumes
Introduces horizontal versus vertical scaling while relying on a Deployment with resource requests for the CPU example.
What you can do after
Operate/configure it with an HPA manifest and observe replica changes using kubectl commands.
How it connects
Connects Deployment replicas, CPU resource requests, external metrics, Ingress, and cluster autoscaling.

Also explained in

Compare how 3 books explain HorizontalPodAutoscaler →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes for Developers. The section links point to the parts of the book it draws on.