Traffic is never flat. A shop gets a rush at lunchtime, a batch system wakes up at night, and an AI inference service can go from idle to busy in seconds. If you size a cluster for the peak, you pay for idle machines most of the day. If you size it for the average, users feel the slowdown when the peak arrives. Autoscaling is how Kubernetes closes that gap.
The tricky part is that “autoscaling” in Kubernetes is not one feature. It is a set of separate tools that work at different layers: some add more Pods, some give Pods more CPU and memory, and some add or remove whole nodes. This guide explains each one in plain terms, shows how they fit together, and walks through a hands-on example taken from the official Kubernetes docs.
The layers of Kubernetes autoscaling
It helps to think of autoscaling as three questions the cluster keeps asking:
- How many copies of my app should run? This is horizontal scaling. The Horizontal Pod Autoscaler (HPA) changes the replica count of a Deployment or StatefulSet.
- How big should each copy be? This is vertical scaling, also called rightsizing. The Vertical Pod Autoscaler (VPA) adjusts CPU and memory requests and limits.
- Do I have enough machines? This is node autoscaling. Cluster Autoscaler and Karpenter add nodes when Pods cannot be scheduled and remove nodes that are underused.
On top of these, event-driven tools such as KEDA let you scale on signals outside the cluster, like queue length or a schedule, and even scale a workload down to zero. The layers depend on each other: the HPA creates new Pods, and if there is no room for them, a node autoscaler steps in to add capacity. If you are new to how Pods land on nodes, our guide to Kubernetes Pod scheduling is a useful companion to this one.
Horizontal Pod Autoscaler (HPA)
The HPA is built into Kubernetes and is the autoscaler most teams start with. It runs as a control loop inside the kube-controller-manager. By default the loop runs every 15 seconds, controlled by the --horizontal-pod-autoscaler-sync-period flag. On each pass it reads the current metric, compares it with your target, and works out how many replicas you need.
The core formula from the Kubernetes docs is simple:
desiredReplicas = ceil[currentReplicas * ( currentMetricValue / desiredMetricValue )]
For example, if 4 replicas are running at 100% of the CPU target and the target is 50%, the HPA asks for 8 replicas. To avoid constant small changes, it skips scaling when the ratio is within a tolerance of 10% by default. When scaling down, it also uses a stabilization window of 300 seconds, so a short dip in traffic does not remove Pods you will need again a minute later.
A few practical points matter here:
- Use the autoscaling/v2 API. It is the current stable version and supports memory, custom metrics, multiple metrics and the
behaviorfield for tuning how fast scaling happens. - Install a metrics source. CPU and memory metrics come from the
metrics.k8s.ioAPI, which is usually provided by the Metrics Server add-on. It is not always installed by default. - Set resource requests. CPU utilization targets are measured against the container’s requests, so a Pod with no requests gives the HPA nothing to compare against.
- Know the metric types. Besides Resource and ContainerResource metrics, the HPA can scale on Pods, Object and External metrics through the
custom.metrics.k8s.ioandexternal.metrics.k8s.ioAPIs, which an adapter provides.
Vertical Pod Autoscaler (VPA)
Many workloads do not need more copies. They need the right amount of CPU and memory per copy. Guessing requests by hand is common, and it usually leads to either wasted capacity or Pods that get throttled or killed for running out of memory. The VPA watches real usage and recommends, or applies, better values.
Unlike the HPA, the VPA is not part of the core Kubernetes API. It is defined as a Custom Resource Definition and you install it separately from the kubernetes/autoscaler repository. Its stable API version is autoscaling.k8s.io/v1. It has three parts: a recommender that studies usage, an updater that applies changes to running Pods, and an admission controller webhook that sets values on new or recreated Pods.
You choose how much control to give it with updateMode:
- Off: only produce recommendations in the VPA object’s status. A safe way to start.
- Initial: set requests only when Pods are created.
- Recreate: evict Pods whose requests differ a lot from the recommendation so they come back with new values.
- InPlaceOrRecreate: try to resize Pods without a restart, and fall back to eviction when that is not possible.
The older Auto mode is deprecated since VPA 1.4.0 and is now an alias for Recreate. In-place updates are possible because in-place Pod resize in Kubernetes itself has been stable since v1.35. Here is the example from the Kubernetes docs:
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: my-app-vpa
spec:
targetRef:
apiVersion: "apps/v1"
kind: Deployment
name: my-app
updatePolicy:
updateMode: "Recreate" # Off, Initial, Recreate, InPlaceOrRecreate, InPlace
Node autoscaling: Cluster Autoscaler and Karpenter
Pod autoscalers only help if there is somewhere to run the Pods. Node autoscalers handle two jobs: provisioning, which adds nodes when Pods are stuck in Pending because nothing fits, and consolidation, which removes underused nodes to save money. The Kubernetes docs name two node autoscalers sponsored by SIG Autoscaling.
Cluster Autoscaler
Cluster Autoscaler adds or removes nodes in node groups that you configure in advance. A node group usually maps to a cloud provider resource such as a virtual machine group. It supports many cloud providers, including smaller ones, and its integrations live inside the Kubernetes project. When it shrinks the cluster, it picks specific nodes to remove instead of simply resizing the group.
Karpenter
Karpenter takes a different approach. Instead of fixed node groups, you describe constraints in a NodePool resource, and Karpenter launches individual machines that fit the pending Pods. It also manages more of the node lifecycle, such as replacing nodes after a set lifetime and rolling out new node images. The Kubernetes project publishes Karpenter as a library that cloud providers build on, and there are fewer provider implementations than for Cluster Autoscaler. AWS and Azure are two of them.
A simple rule of thumb: if you are on a provider with a mature Karpenter implementation and you want flexible instance choice, Karpenter is worth a look. If you need wide provider support or already run well-defined node groups, Cluster Autoscaler remains a solid choice.
Event-driven autoscaling with KEDA
CPU is not always the best signal. A worker that reads from a message queue should scale on how many messages are waiting, not on how busy its CPU looks. KEDA, the Kubernetes Event-driven Autoscaler, is a CNCF graduated project built for this. It offers a catalog of more than 70 built-in scalers for clouds, databases, messaging systems and more.
KEDA does not replace the HPA. Its operator handles scaling between zero and one replica, and its metrics server exposes external metrics so the regular HPA can handle scaling above one. You describe the rules in a ScaledObject for Deployments and StatefulSets, or a ScaledJob for batch Jobs. We covered the second case in detail in KEDA ScaledJobs: AutoScale Batch Workloads.
This example from the KEDA cron scaler docs keeps a Deployment at 10 replicas during working hours and lets it drop to zero outside them:
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: cron-scaledobject
namespace: default
spec:
scaleTargetRef:
name: my-deployment
minReplicaCount: 0
cooldownPeriod: 300
triggers:
- type: cron
metadata:
timezone: Asia/Kolkata
start: 0 6 * * *
end: 0 20 * * *
desiredReplicas: "10"
KEDA can be installed with Helm, as shown in its deployment docs:
helm repo add kedacore https://kedacore.github.io/charts
helm repo update
helm install keda kedacore/keda --namespace keda --create-namespace
Hands-on: autoscale a sample app with the HPA
This short walkthrough follows the official Kubernetes HPA tutorial. Prerequisites: a cluster running Kubernetes 1.23 or later, kubectl configured to talk to it, and Metrics Server installed. On minikube you can enable it with minikube addons enable metrics-server.
First, deploy the sample php-apache app, which includes a Deployment and a Service:
kubectl apply -f https://k8s.io/examples/application/php-apache.yaml
Create an HPA that targets 50% CPU and keeps between 1 and 10 replicas:
kubectl autoscale deployment php-apache --cpu=50% --min=1 --max=10
The same autoscaler written declaratively looks like this:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: php-apache
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
In a separate terminal, generate load against the Service:
kubectl run -i --tty load-generator --rm --image=busybox:1.28 --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O- http://php-apache; done"
Then watch the HPA react. After a short while the replica count rises; stop the load generator and, after the stabilization window, it comes back down:
kubectl get hpa php-apache --watch
How to combine autoscalers safely
Most production clusters run more than one autoscaler. That works well as long as each one has a clear job.
- Pair Pod and node autoscaling. An HPA or KEDA without a node autoscaler will eventually leave Pods in Pending when the cluster is full.
- Do not let HPA and VPA fight over the same metric. If the HPA scales on CPU and the VPA also changes CPU requests, each reacts to the other. Microsoft’s AKS docs, for example, advise against using VPA with an HPA that scales on the same CPU and memory metrics. Use the VPA in Off mode for recommendations, or let the HPA scale on custom or external metrics instead.
- Protect availability during scale-down. Node consolidation evicts Pods. Use PodDisruptionBudgets so enough replicas stay up.
- Watch the numbers. Autoscaling is only as good as the metrics behind it. Our guide to the Kubernetes observability stack covers how to collect them.
- Tie it back to cost. Rightsized requests and consolidation are among the biggest levers for spend, as covered in our Kubernetes FinOps guide.
Key takeaways
- Kubernetes autoscaling works in layers: HPA for replica count, VPA for Pod size, and Cluster Autoscaler or Karpenter for nodes.
- The HPA is built in, runs every 15 seconds by default and needs a metrics source such as Metrics Server plus resource requests on your containers.
- The VPA is an add-on with Off, Initial, Recreate and InPlaceOrRecreate modes; start with Off to collect recommendations.
- Cluster Autoscaler scales pre-configured node groups, while Karpenter provisions individual nodes from NodePool constraints and manages node lifecycle.
- KEDA, a CNCF graduated project, adds event-driven scaling and scale to zero on top of the HPA.
- Give each autoscaler one clear job, and avoid HPA and VPA acting on the same CPU or memory metric.
Sources
Kubernetes docs: Autoscaling Workloads; Horizontal Pod Autoscaling; HorizontalPodAutoscaler Walkthrough; Vertical Pod Autoscaling; Resize CPU and Memory Resources assigned to Containers; Node Autoscaling; Cluster Autoscaler on GitHub; Karpenter; KEDA; KEDA concepts; KEDA cron scaler; Deploying KEDA; Microsoft Learn: Vertical Pod Autoscaler in AKS.
