Autoscaling Kubernetes: HPA, VPA, KEDA and Cluster Autoscaler or Karpenter

Certified Kubernetes Administrator 28 min min read Updated: Nov 17, 2026 Intermediate
Autoscaling Kubernetes: HPA, VPA, KEDA and Cluster Autoscaler or Karpenter
Intermediate Topic 2 of 2

Three Levels of Scaling

What scalesToolDirection
Number of podsHPA (Horizontal Pod Autoscaler), KEDAOut and in (more or fewer replicas)
Size of a podVPA (Vertical Pod Autoscaler)Up and down (requests/limits)
Number of nodesCluster Autoscaler, KarpenterAdd or remove machines

Horizontal Pod Autoscaler (HPA)

The HPA controller checks metrics about every 15 seconds and changes the replicas of a Deployment, StatefulSet or other scalable resource.

text desiredReplicas = ceil( currentReplicas * ( currentMetricValue / targetMetricValue ) ) Example: 4 pods, average CPU 90% of request, target 60% -> ceil(4 * 90/60) = 6 pods
HPA percentages use the request: CPU utilization is relative to the pod's CPU request. If pods have no CPU request, the HPA cannot compute utilization and shows <unknown>. It also needs metrics-server.
yaml apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: web spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: web minReplicas: 3 maxReplicas: 20 metrics: - type: Resource resource: name: cpu target: {type: Utilization, averageUtilization: 60} - type: Resource resource: name: memory target: {type: AverageValue, averageValue: 400Mi} behavior: scaleUp: stabilizationWindowSeconds: 0 policies: - {type: Percent, value: 100, periodSeconds: 60} # at most double per minute - {type: Pods, value: 4, periodSeconds: 60} selectPolicy: Max scaleDown: stabilizationWindowSeconds: 300 # wait 5 minutes before shrinking policies: - {type: Percent, value: 20, periodSeconds: 60}
bash kubectl autoscale deployment web --min=3 --max=20 --cpu-percent=60 # quick way kubectl get hpa web -w # NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS # web Deployment/web 85%/60% 3 20 6 kubectl describe hpa web # Events and Conditions: AbleToScale, ScalingActive, ScalingLimited # Load test from inside the cluster kubectl run load --rm -it --image=busybox:1.36 --restart=Never -- sh -c 'while true; do wget -q -O- http://web.shop; done' # or: hey / k6 / fortio
Metric typeExampleSource
ResourceCPU, memorymetrics-server
ContainerResourceCPU of one container (not sidecars)metrics-server
Podshttp_requests_per_second averaged per podCustom Metrics API (Prometheus Adapter)
ObjectRequests per second on an IngressCustom Metrics API
ExternalQueue length in SQS or Kafka lagExternal Metrics API (KEDA, adapters)
Do not scale the same Deployment's replicas by hand and with GitOps: If your manifest has replicas: 3 and the HPA changes it to 10, every kubectl apply resets it. Remove replicas from the Deployment YAML when an HPA manages it.

Common HPA Problems

SymptomCause
TARGETS <unknown>/60%metrics-server missing or pods have no CPU requests
Never scales upTarget too high, wrong metric, maxReplicas reached, pods not Ready
Flapping up and downToo short stabilization, spiky load. Increase scaleDown window
Scales up but pods are PendingNo node capacity. Need the Cluster Autoscaler
Scaling does not helpBottleneck is a database or a lock, not the pod count

Vertical Pod Autoscaler (VPA)

The VPA watches real usage and recommends or sets CPU and memory requests. It is installed as an add-on (not in core Kubernetes).

yaml apiVersion: autoscaling.k8s.io/v1 kind: VerticalPodAutoscaler metadata: {name: api} spec: targetRef: {apiVersion: apps/v1, kind: Deployment, name: api} updatePolicy: updateMode: "Off" # Off = only recommendations. Also: Initial, Recreate, Auto resourcePolicy: containerPolicies: - containerName: "*" minAllowed: {cpu: 50m, memory: 64Mi} maxAllowed: {cpu: "2", memory: 2Gi}
bash kubectl describe vpa api | sed -n '/Recommendation/,$p' # Container Name: app Lower Bound: cpu 80m mem 120Mi Target: cpu 150m mem 220Mi Upper Bound: cpu 400m mem 600Mi
  • Start with updateMode: Off and use the recommendations to set requests in your manifests
  • In Auto/Recreate mode the VPA evicts pods to apply new values (newer Kubernetes can resize in place when supported)
  • Do not use HPA and VPA on the same metric (CPU) for the same workload, they fight each other

KEDA: Event-Driven Autoscaling

KEDA scales workloads from events such as queue length, Kafka lag, cron schedules and database rows. It can scale to zero and back. Under the hood it creates and manages an HPA.

yaml apiVersion: keda.sh/v1alpha1 kind: ScaledObject metadata: {name: worker} spec: scaleTargetRef: {name: worker} minReplicaCount: 0 maxReplicaCount: 50 cooldownPeriod: 120 triggers: - type: aws-sqs-queue metadata: queueURL: https://sqs.eu-west-1.amazonaws.com/123456789012/jobs queueLength: "20" # one pod per 20 messages awsRegion: eu-west-1 authenticationRef: {name: keda-aws-auth}

Cluster Autoscaler and Karpenter

More pods need more nodes. Node autoscalers watch for Pending pods that cannot be scheduled and add nodes, and remove nodes that are empty or under-used.

Cluster AutoscalerKarpenter
ModelScales node groups (ASG, VMSS, MIG) up and downCreates individual nodes that fit pending pods, chooses instance types freely
SpeedMinutesFaster, bin-packs and consolidates
ConfigMin/max per node groupNodePool and NodeClass objects
WhereMany clouds and Cluster APIStarted on AWS, spreading to other clouds
text Load grows -> HPA adds pods -> pods Pending (no room) -> Cluster Autoscaler/Karpenter adds a node -> pods scheduled Load drops -> HPA removes pods -> nodes under-used -> autoscaler drains and removes a node (respects PDBs)
  • Requests must be right: autoscalers decide using requests, not real usage
  • Use PodDisruptionBudgets so that scale-down does not break availability
  • Pods with local storage, no controller, or the annotation cluster-autoscaler.kubernetes.io/safe-to-evict: false block scale-down
  • Keep a little headroom (overprovisioning pods with low priority) for fast bursts
  • Set limits on maximum nodes to protect your budget

Putting It Together

GoalCombination
Web app with varying trafficHPA on CPU or requests per second + Cluster Autoscaler/Karpenter
Queue workersKEDA on queue length, scale to zero
Right-size stable servicesVPA in recommendation mode, then update requests
Batch jobs on cheap nodesNode pools with taints, spot instances, Karpenter

Practice Tasks

  1. Create a Deployment with CPU requests, add an HPA and generate load until it scales
  2. Add a scaleDown stabilization window and watch the effect
  3. Install VPA in Off mode and read the recommendations
  4. Explain what happens when the HPA wants 20 pods but only 12 fit on nodes
  5. Design autoscaling for a queue-driven image processing service

Conclusion

HPA adds and removes pods using metrics and CPU requests, VPA right-sizes requests, KEDA scales from events, and Cluster Autoscaler or Karpenter adds and removes nodes for Pending pods. Set correct requests, PDBs and limits so scaling is safe and affordable.

Quick Summary: HPA formula ceil(cur*metric/target); needs metrics-server + requests; behavior windows; VPA Off mode; no HPA+VPA on CPU; KEDA scale to zero; node autoscalers react to Pending pods.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators