CD5: Kubernetes essentials

Lesson 5 of 5 · reading 15–25 minutes; practice and project time is on the module page

Scaling, and running Kubernetes in practice

🎯You'll be able to

Explain how the Horizontal Pod Autoscaler decides the number of pods, how nodes are added, and what running a cluster involves day to day.

🏠The everyday picture

A bank opens more counters when the queue grows and closes them only after the queue has stayed short for a while. If all counters are open and the queue still grows, the branch manager calls in staff from another branch.

📘What it really is

📝A worked example

Input (not verified): an HPA for RouteRani:

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata: { name: routerani-api }
spec:
  scaleTargetRef: { apiVersion: apps/v1, kind: Deployment, name: routerani-api }
  minReplicas: 2
  maxReplicas: 6
  metrics:
    - type: Resource
      resource: { name: cpu, target: { type: Utilization, averageUtilization: 60 } }

Expected output: under load, kubectl get hpa routerani-api --watch shows the CPU percentage rising and REPLICAS growing towards 6; after the load stops, replicas return to 2 about five minutes later, because of the stabilisation window.

🌍Where it's used

📝Case scenario

A ticketing platform's HPA scaled pods up during a concert sale, but new pods stayed Pending for six minutes while new nodes started, and the site slowed badly. They kept a small buffer of spare capacity (low-priority placeholder pods that real pods can push aside) and started scaling earlier, at a lower target.

🖱️Try it

Work out the HPA formula by hand: 4 pods at 30% CPU, target 60%, minimum 2; then 2 pods at 150%, target 60%, maximum 6. Success: you get 2 (ceil(4 × 30 ÷ 60) = 2) and 5 (ceil(2 × 150 ÷ 60) = 5), and you can explain why scaling down waits.

⚠️Common mistakes

🛠️For your project

Project 3's autoscaler uses the same formula, bounds and stabilisation window; tests 6 and 7 check it scales up to its maximum and back down to its minimum.

✅Quick check

With 2 pods at 120% CPU and a 60% target, how many pods does the HPA want?

Show the answer

ceil(2 × 120 ÷ 60) = 4.

Why does scaling down wait?

Show the answer

So a short dip doesn't remove pods that are needed again a moment later.

← PreviousConfiguration, secrets and traffic from outside
ProLearners Assist