Introduction
In real-world cloud applications, user traffic is never constant. Sometimes your application gets very high traffic (like during sales or peak hours), and sometimes it is almost idle. If you keep a fixed number of pods in Kubernetes, you either face performance issues during high traffic or waste resources during low traffic.
This is where Kubernetes autoscaling comes into play. Using Horizontal Pod Autoscaler (HPA), you can automatically increase or decrease the number of pods based on CPU usage, memory usage, or other metrics.
In this step-by-step Kubernetes autoscaling tutorial, you will learn how to configure HPA in simple words with practical examples.
What is Horizontal Pod Autoscaler in Kubernetes?
Horizontal Pod Autoscaler (HPA) is a Kubernetes feature that automatically scales the number of pods in your application based on resource usage like CPU or memory.
Simple explanation:
If load increases → pods increase
If load decreases → pods decrease
Real-life example:
Think of a food delivery app like during dinner time. When many users place orders, more delivery agents (pods) are needed. When orders reduce, fewer agents are required. HPA works exactly like this.
Prerequisites for Kubernetes Autoscaling
Before you configure autoscaling in Kubernetes, make sure the following things are ready:
A working Kubernetes cluster (local or cloud like AWS, Azure, GCP)
kubectl installed and configured
Metrics Server installed (very important for HPA)
A running deployment to scale
Without Metrics Server, Kubernetes cannot track CPU or memory usage, so autoscaling will not work.
Step 1: Install Metrics Server in Kubernetes
Metrics Server is required for Kubernetes HPA because it collects CPU and memory usage data.
Run this command:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
After installing, verify:
kubectl get deployment metrics-server -n kube-system
Simple understanding:
Metrics Server acts like a monitoring tool that tells Kubernetes how much CPU your pods are using.
Step 2: Create or Verify Deployment
Before applying autoscaling, you must have a deployment running.
Example Kubernetes deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: nginx
resources:
requests:
cpu: "100m"
limits:
cpu: "500m"
Important concept:
CPU request = minimum guaranteed CPU
CPU limit = maximum CPU allowed
Why this matters:
HPA uses CPU request as a reference to calculate scaling. If you don’t define it, autoscaling will not work correctly.
Real-world mistake:
Many beginners forget to define CPU requests, and then HPA does not scale properly.
Step 3: Create HPA Using kubectl Command
You can quickly configure autoscaling using a simple command:
kubectl autoscale deployment my-app --cpu-percent=50 --min=2 --max=10
Join the conversation! Your thoughts help the community grow.