Version note: examples use the Kubernetes v1.35 documentation snapshot, not necessarily the newest release. Check current support and component compatibility. These commands were not tested on a live cluster.
What this guide covers
The Horizontal Pod Autoscaler, or HPA, changes the number of application Pods based on metrics such as CPU use. It does not add worker machines, so extra Pods can still run out of space.
Before you start
Create autoscale-lab. Install a compatible Metrics Server with working certificate trust and confirm kubectl top nodes works. kubeadm does not install it automatically. Avoid disabling kubelet TLS checks to make metrics work.
Step 1: Set requests and scaling limits
Save this as autoscale.yaml and apply it:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: autoscale-lab
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.28.0
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
path: /
port: http
resources:
requests:
cpu: 100m
memory: 64Mi
limits:
cpu: 500m
memory: 128Mi
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
namespace: autoscale-lab
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Pods
value: 1
periodSeconds: 60
Run kubectl apply -f autoscale.yaml. The 60 percent CPU target is relative to the CPU request. With a 100m request, it corresponds to about 60m average usage per relevant Pod, not 60 percent of the whole machine.
Step 2: Check metrics and HPA conditions
Run:
kubectl top nodes
kubectl -n autoscale-lab top pods
kubectl -n autoscale-lab get hpa web
kubectl -n autoscale-lab describe hpa web
kubectl get apiservice v1beta1.metrics.k8s.io
If metrics are unknown, fix the metrics path and missing resource requests before changing the target. Check HPA conditions for the reason it is or is not scaling.
Step 3: Apply controlled load
Use a load tool against your own test service with a bounded rate and duration. Static Nginx may use too little CPU to demonstrate scaling clearly. Use a representative application when measuring scaling behavior. Never load-test an unrelated public service.
Step 4: Observe scale-up and scale-down
Watch replicas, readiness, errors and response time. If new Pods are Pending, inspect node capacity. Avoid repeatedly applying a fixed replica count from another tool while HPA manages it. After load ends, allow time for the configured stabilization window.
Check the result
Confirm valid metrics, observed scaling and acceptable application behavior. More Pods do not fix every bottleneck, and multiple replicas on one worker do not survive loss of that worker.
Conversation
0 commentsNo comments yet. Start the conversation below.