Kubernetes · Chapter 23 of 28
Autoscaling
By the end of this chapter you can read what each pod uses with kubectl top and the metrics API, make a HorizontalPodAutoscaler that adds and removes replicas to keep CPU near a target, work out what it will do from its formula, watch it under load, read its events, tune how fast it scales, and say why an HPA without CPU requests shows <unknown>. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: More workloads and traffic
- 4 tasks
- Kubernetes
- In the all labs plan
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.
Why you need this
The rider app asks rider-tracker where every one of Jhatpat's 1,900 riders is, every few seconds. On a normal afternoon that is about 30 requests a second; between 7 and 10 pm on Dhanteras, about 300. Each request costs rider-tracker's pod about 20m of CPU, so the evening needs ten times the pods the afternoon does, and nobody knows the exact minute the rush starts.
What you know so far is a fixed number. kubectl scale (chapter 5) sets replicas by hand: set 10 and you pay for 10 pods all day; set 2 and the riders' maps freeze at 7:15 pm while someone looks for a laptop. Requests and limits (chapter 12) say what one pod may use, not how many pods there should be. Ritika wants the cluster to add pods when the rush arrives and remove them when it is over, without anyone watching.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
hpa.yaml: make an HPA for rider-tracker
Make an autoscaler, so rider-tracker gets more pods when it is busy.
1. Open hpa.yaml in the chapter folder. Complete the HorizontalPodAutoscaler rider-tracker.
2. It scales the Deployment rider-tracker from 2 to 10 replicas.
3. It aims for 60% average CPU utilisation, as a percentage of each pod's CPU request.
4. Apply hpa.yaml. You can also make the HPA with kubectl autoscale instead.
5. Wait about 20 seconds. Then list the HPA. TARGETS should show a number, not unknown.
To pass: in ch23, the HPA rider-tracker scales the Deployment rider-tracker, minReplicas is 2, maxReplicas is 10, and the CPU target is Utilization at 60. It also has a CPU reading already, so TARGETS shows a number.
- Task 2
Send 300 requests a second to rider-tracker
Send load to rider-tracker, and watch the HPA add pods.
1. Do task 1 first. The HPA must be in place.
2. Use lab load to send 300 requests a second to the Service rider-tracker.
3. Watch the HPA and the pods. The HPA adds pods every few seconds.
4. Leave the load running.
To pass: the load on rider-tracker is exactly 300 requests a second and is still running. The HPA wants 10 replicas, the Deployment has 10, and all 10 pods are ready.
- Task 3
events.txt: write down what the HPA did
The HPA writes an event each time it changes the size. Copy the sizes into a file.
1. Keep the load from task 2 running.
2. Run kubectl describe hpa rider-tracker. Read the events at the end.
3. Find every SuccessfulRescale event. Each one has a number after New size:.
4. Write those numbers in events.txt, one on each line, oldest first.
To pass: events.txt has every new size from the SuccessfulRescale events, in the same order, and nothing else. Lines that start with # are notes and do not count. If the load stops, the HPA scales down and the list changes.
- Task 4 · interview question
Interview: an HPA that shows unknown
Interview question: Tushar's HPA notifier shows cpu: <unknown>/70% and never scales. Why?
1. Describe the HPA notifier. Read its conditions and events.
2. Compare the notifier Deployment's container with rider-tracker's container.
3. Ammar will ask: "The HPA says unknown. What is it missing, and what do you change?"
4. Write your answer in one or two sentences of your own, in answer.txt, below the notes.
To pass: answer.txt names what the notifier container is missing, and says what you change to fix it. Lines that start with # do not count.
What you will learn
- metrics-server and the metrics API: what every pod uses, behind kubectl top.
- The HorizontalPodAutoscaler: replicas between a minimum and a maximum, aiming at a CPU target.
- The formula: how the HPA computes the number of replicas, with real numbers.
- kubectl autoscale and autoscaling/v2 YAML, and behavior: how fast it scales up and down.
- lab load: the Dhanteras rush, on demand.
- Why requests are required, and vertical and cluster autoscaling.
More in More workloads and traffic
Build "Autoscaling" yourself
Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.