Labs · DevOps
Lab: Install metrics-server, Create a HorizontalPodAutoscaler at 50% CPU, Generate Load and Watch Pods Scale Out and In
Course: DevOps · Chapter 18: Kubernetes Workloads · Lesson: 18.13 Autoscaling: HPA and two names to recognise
Chapter 18 explains the HPA; this lab makes it scale with real load on your laptop.
Chala mitrano! Sale chya divashi traffic dahaa pat. Pod haatane vaadhavnar ka? Nahi, HPA karel.
Suppose we are…
Suppose we are on an Instagram-style team before a big festival campaign. Traffic can jump ten times in minutes and fall back at night. Fixed replicas either waste money or fall over.
In this lab a HorizontalPodAutoscaler watches CPU and adds Pods when average CPU goes above 50% of the request, then removes them when load stops.
Goal of this lab
By the end you will have:
- metrics-server running, so
kubectl top podsworks. - An HPA with min 1 and max 5 replicas at 50% CPU.
- Replicas that scale out under load and back in after it stops.
What you need
- A kind cluster (
kind create cluster --name lab). - Internet access to pull images.
- 45 minutes (scale-in waits about 5 minutes by design).
Part 1: metrics-server
Install metrics-server and allow kind's self-signed kubelet certificates (lab only):
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml kubectl -n kube-system patch deploy metrics-server --type=json -p '[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--kubelet-insecure-tls"}]' kubectl -n kube-system rollout status deploy/metrics-server--kubelet-insecure-tlsis only for local kind clusters; managed clusters such as EKS do not need it.Wait one minute, then:
kubectl top nodesWhat you should see: CPU and memory numbers for
lab-control-plane.
Part 2: App and HPA
Deploy the CPU demo app (it burns CPU on every request) with a CPU request, and a Service:
kubectl create deployment php-apache --image=registry.k8s.io/hpa-example --port=80 kubectl set resources deploy/php-apache --requests=cpu=200m --limits=cpu=500m kubectl expose deploy php-apache --port=80The HPA percentage is measured against the request. Without a CPU request, the HPA shows
<unknown>.Create the HPA as YAML:
hpa.yaml
apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: php-apache spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: php-apache minReplicas: 1 maxReplicas: 5 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 50Apply and check:
kubectl apply -f hpa.yaml kubectl get hpa php-apacheWhat you should see:
TARGETS cpu: 0%/50%(after a minute),REPLICAS 1.
Part 3: Load, scale out, scale in
In a second terminal start a load generator Pod:
kubectl run load --rm -it --image=busybox:1.36 --restart=Never -- /bin/sh -c "while true; do wget -q -O- http://php-apache; done"In the first terminal watch the HPA:
kubectl get hpa php-apache -wWhat you should see: CPU rising above 50% and REPLICAS going 1 → 2 → 4 or 5 within a few minutes.
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS php-apache Deployment/php-apache cpu: 0%/50% 1 5 1 php-apache Deployment/php-apache cpu: 248%/50% 1 5 1 php-apache Deployment/php-apache cpu: 248%/50% 1 5 5 php-apache Deployment/php-apache cpu: 58%/50% 1 5 5Stop the load generator with Ctrl+C, then keep watching for about 5 minutes.
What you should see: CPU drops to 0% and replicas go back to 1 after the scale-down stabilisation window.
Clean up
Delete the lab objects, or the whole cluster:
kubectl delete hpa php-apache kubectl delete svc,deploy php-apache # kind delete cluster --name lab
Ravindra Bagale's Tip
Set a sensible maxReplicas. Autoscaling without a ceiling can turn a bug or an attack into a very large bill.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| No CPU request on the container | HPA target shows <unknown> | Set resources.requests.cpu |
| metrics-server not ready | kubectl top errors | Wait a minute; on kind add --kubelet-insecure-tls |
| Expecting instant scale-in | Replicas stay high for minutes | Normal: scale-down waits ~5 minutes to avoid flapping |
| Scaling manually while an HPA exists | HPA overrides you | Change minReplicas/maxReplicas instead |
Self-check checklist
0 of 4 done
Try-at-home challenge
Add a behavior.scaleDown.stabilizationWindowSeconds: 60 block to the HPA and compare how fast it scales in. Write one reason a team would want a long window.
Samajla ka? Requests set, ceiling set, HPA does the night shift. Aata pudhe jaauya Services ani volumes.