Skip to content

Autoscaling

HPA (Horizontal Pod Autoscaler)

metrics-server is installed, so you can scale on CPU utilisation directly. Use autoscaling/v2.

yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: myapp
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: myapp
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70

averageUtilization is relative to the container's CPU request. Without a request set, the HPA has no baseline and will not scale.

KEDA

For event-driven scaling, wherever CPU utilisation is the wrong signal (queue depth, schedules, external metrics). KEDA can also scale down to zero.

yaml
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: worker
spec:
  scaleTargetRef:
    name: worker
  minReplicaCount: 0
  maxReplicaCount: 10
  triggers:
    - type: cron
      metadata:
        timezone: Europe/Berlin
        start: 0 8 * * *
        end: 0 20 * * *
        desiredReplicas: "3"

The trigger type depends on your event source, the surrounding structure stays the same.

Scale-to-zero

An HTTP service can drop to zero replicas when nothing calls it and wake on the next request, which is worth doing for anything idle most of the day. It has its own page, including what it does and does not remove from your invoice: see Scale-to-zero.

It cannot be combined with an HPA or a ScaledObject on the same workload.

VPA (Vertical Pod Autoscaler)

The VPA runs in recommendation mode: updateMode: "Off". It computes suggested requests and limits but changes nothing on its own. Read the recommendations from the resource status and apply them to your deployment yourself.

yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: myapp
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: myapp
  updatePolicy:
    updateMode: "Off"

InPlaceOrRecreate is also available. With it the VPA adjusts requests on the running pod where possible and only recreates the pod when an in-place change is not possible.

What's next