VPA

Difficulty: Advanced

Question

What is the Vertical Pod Autoscaler (VPA), how does it work, and when would you use it instead of or alongside the HPA?

Answer

The Vertical Pod Autoscaler automatically sets CPU and memory requests (and proportionally limits) for containers based on their observed usage. Instead of adding more Pods like the Horizontal Pod Autoscaler, it makes each Pod the right size, which fixes over-provisioned services that waste money and under-provisioned ones that get throttled or OOMKilled.

The three components

VPA is not part of core Kubernetes; you install it from the kubernetes/autoscaler project (GKE offers it as a built-in option). It has three parts:

  1. Recommender: reads usage from the metrics API (metrics-server, optionally Prometheus history), builds decaying histograms per container, and writes recommendations into the VPA object's status: target, lowerBound, upperBound and uncappedTarget.
  2. Updater: when a running Pod's requests are far from the recommendation, it evicts the Pod (respecting PodDisruptionBudgets) so it can be recreated with new values.
  3. Admission controller: a mutating webhook that rewrites the requests of new Pods as they are created.

updateMode decides how aggressive it is

ModeBehaviour
OffOnly computes recommendations; nothing changes
InitialSets requests at Pod creation, never evicts
RecreateSets at creation and evicts Pods to apply new values
AutoCurrently behaves like Recreate
InPlaceOrRecreateNewer mode that resizes running Pods in place where possible

Kubernetes added in-place Pod resize (changing a running container's CPU and memory without restarting the Pod), which became beta and enabled by default in 1.33 and later graduated to stable. VPA's InPlaceOrRecreate mode builds on it, falling back to eviction when an in-place resize is not possible. Check your cluster and VPA versions before relying on it.

VPA vs HPA

  • HPA changes the number of replicas and suits stateless services with spiky traffic, such as a checkout API during a flash sale.
  • VPA changes the size of each replica and suits workloads that cannot scale out easily (single-instance services, some stateful systems, batch jobs) or where nobody knows the right requests.
  • Do not let both act on the same CPU or memory metric. HPA scales on utilisation as a percentage of requests; if VPA keeps changing requests, the two fight. Safe combinations: HPA on custom or external metrics (queue length, RPS) with VPA on resources, or VPA in Off mode purely for recommendations.

Practical advice and gotchas

  • Start with updateMode: "Off" and read the recommendations with kubectl describe vpa. Many teams use VPA only as a right-sizing report.
  • Set resourcePolicy with minAllowed and maxAllowed so a bad week of data cannot shrink a container to nothing or grow it beyond node size.
  • Evictions restart Pods; services with one replica will see downtime in Recreate mode. By default the updater will not evict when fewer than two live replicas exist.
  • controlledValues: RequestsOnly leaves limits alone; the default RequestsAndLimits scales limits to keep the original ratio.
  • Java and other runtimes with fixed heap sizes may not use extra memory unless the heap is configured relative to the container limit.
How VPA right-sizes a Pod. Usage history becomes a recommendation, which is applied at Pod creation or by eviction or in-place resize.
  1. metrics-server usage data: CPU and memory samples per container.
  2. Recommender: Builds histograms and writes target requests to the VPA status.
  3. Updater: Evicts or resizes Pods whose requests drift too far.
  4. Admission controller: Mutates new Pods to use the recommended requests.
  5. Right-sized Pod: Runs with requests close to real usage.

Code examples

VPA object with safe bounds

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: invoice-worker
  namespace: billing
spec:
  targetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: invoice-worker
  updatePolicy:
    updateMode: "Off"        # start with recommendations only
  resourcePolicy:
    containerPolicies:
      - containerName: "*"
        minAllowed:
          cpu: 100m
          memory: 128Mi
        maxAllowed:
          cpu: "2"
          memory: 2Gi
        controlledValues: RequestsAndLimits

Off mode is a safe first step; switch to Initial or Recreate once the recommendations look sensible.

Reading the recommendation

kubectl describe vpa invoice-worker -n billing | sed -n '/Recommendation/,$p'

Target is what VPA would set; lower and upper bounds show how confident it is.

Key points

  • VPA adjusts requests (and proportionally limits) per container; HPA adjusts replica count
  • Components: Recommender, Updater, Admission controller; installed separately
  • updateMode Off is a free right-sizing report and the safest start
  • Never run HPA and VPA on the same CPU or memory signal
  • In-place Pod resize lets newer VPA versions resize without always recreating Pods

Common follow-up questions

  • Why do HPA and VPA conflict on CPU, and how would you combine them safely?
  • What happens to a single-replica Deployment when VPA applies a new recommendation?
  • How does in-place Pod resize change how VPA works?
  • How would you right-size 200 microservices without VPA evicting them?

Interview tips

  • Contrast vertical vs horizontal scaling in one sentence before going deeper
  • Mention the HPA conflict; it is the most common follow-up
  • Recommend Off mode first, which shows production caution

Concepts covered

Vertical Pod Autoscaler, Resource Requests, Recommender, updateMode, In-place Pod Resize

More Kubernetes & Orchestration questions