liveBook · Manning

HorizontalPodAutoscaler

As explained in Server-Side WebAssembly

The Horizontal Pod Autoscaler is a Kubernetes controller that adjusts a workload's replica count according to observed resource utilization or other metrics.

HorizontalPodAutoscaler (HPA) automatically adjusts a Kubernetes workload’s replica count as resource demand changes: scaling up under pressure and scaling down when activity quiets. section 8.2.3

A practical HPA workflow for a Spin application is:

  • Create a Spin application with a route that generates sustained CPU activity. section 8.2.3
  • Build the application, push its OCI image to a registry, and expose it through an Ingress. section 8.2.3
  • Scaffold SpinKube resources with one replica, a ten-replica maximum, CPU and memory requests, and HPA support. section 8.2.3
  • Set CPU and memory targets to 60 percent utilization, then inspect the HPA and deployment while traffic increases. section 8.2.3

Scaling a Spin application

A generated manifest enables autoscaling for scaling-spin, targets its Deployment, permits 1–10 replicas, and monitors CPU and memory at 60 percent average utilization. Sending concurrent requests to /load can drive the deployment from one replica toward the configured maximum. section 8.2.3

Listing 8.8 scaling-spin.yaml
apiVersion: core.spinkube.dev/v1alpha1
kind: SpinApp
metadata:
  name: scaling-spin
spec:
  image: "ghcr.io/danbugs/serverside-wasm-book-code/scaling-spin:latest"
  executor: containerd-shim-spin
  enableAutoscaling: true
  resources:
    limits:
      cpu: 100m
      memory: 128Mi
    requests:
      cpu: 50m
      memory: 64Mi
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: scaling-spin-autoscaler
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: scaling-spin
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 60
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 60

The manifest shows the connection between a SpinApp, its resource requests and limits, and the autoscaling/v2 HPA resource. section 8.2.3

How Server-Side WebAssembly covers it

How deep it goes
Implements application creation, load generation, registry publishing, manifest scaffolding, monitoring, and observed replica changes.
Examples
Uses a recursive Fibonacci /load route, concurrent oha traffic, kubectl get hpa, and deployment events showing replicas scaling to ten.
What it assumes
Assumes familiarity with creating a basic Spin application and deploying its image to a container registry.
What you can do after
Operate/configure HPA for a Spin application using Spin kube scaffolding and kubectl.
How it connects
Connects Kubernetes, SpinKube, Spin applications, Deployments, OCI images, Ingress, and CPU or memory utilization metrics.

Also explained in

Compare how 3 books explain HorizontalPodAutoscaler →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Server-Side WebAssembly. The section links point to the parts of the book it draws on.