Ravindra BagaleCourses & study guides मराठी Track your progress

Guides

Horizontal Pod Autoscaler: Scale Under Load

Install metrics-server, give the Deployment CPU requests, apply an HPA with min/max and a CPU target, generate load, and watch replicas rise then cool down.

Friends, HPA only works when the pod asks for CPU and metrics-server is alive. Chala mitrano — set maxReplicas so a load test cannot empty the wallet.

Quick answer

Hold this vertical picture:

  1. Confirm metrics API.
  2. Add CPU requests.
  3. Write HPA YAML.
  4. apply and get hpa.
  5. Generate load; watch; clean up.

Language: English · मराठी · हिंदी

metrics-serverCPU samples HPA objectmin/max + target Load risesUtilization high Replicas upMore pods Load fallsCool down
HPA under load

Why HPA

Traffic spikes need more pods without a human waking up.

Why: Fixed replica counts waste money at night and break during sales. What: HPA watches metrics and changes replicas. How: Resource metric on CPU is the beginner path.

Example 1: Confirm metrics-server idea

Type this:

printf '%s\n' 'kubectl get apiservice v1beta1.metrics.k8s.io -o jsonpath="{.status.conditions[0].status}" 2>/dev/null || echo NEED_METRICS_SERVER'

You will see:

NEED_METRICS_SERVER

What it means: HPA needs metrics-server (or equivalent) before CPU targets work.

Example 2: Deployment with resources

Type this:

mkdir -p /tmp/hpa && printf '%s\n' 'apiVersion: apps/v1' 'kind: Deployment' 'metadata: {name: web}' 'spec:' '  replicas: 1' '  selector: {matchLabels: {app: web}}' '  template:' '    metadata: {labels: {app: web}}' '    spec:' '      containers:' '      - name: web' '        image: public.ecr.aws/docker/library/nginx:1.27-alpine' '        resources:' '          requests: {cpu: "100m"}' '          limits: {cpu: "200m"}' > /tmp/hpa/deploy.yaml && grep -A2 requests /tmp/hpa/deploy.yaml

You will see:

requests: {cpu: "100m"}

What it means: Requests give HPA a baseline; without them CPU % is meaningless.

Example 3: HPA YAML

Type this:

printf '%s\n' 'apiVersion: autoscaling/v2' 'kind: HorizontalPodAutoscaler' 'metadata: {name: web}' 'spec:' '  scaleTargetRef:' '    apiVersion: apps/v1' '    kind: Deployment' '    name: web' '  minReplicas: 1' '  maxReplicas: 5' '  metrics:' '  - type: Resource' '    resource: {name: cpu, target: {type: Utilization, averageUtilization: 50}}' > /tmp/hpa/hpa.yaml && head -4 /tmp/hpa/hpa.yaml

You will see:

apiVersion: autoscaling/v2

What it means: min/max and CPU target — Flipkart-style web tier pattern.

Concept 1: Apply and watch

Objects must exist before load.

Why: Load without HPA only burns one pod. What: apply Deployment+HPA; get hpa -w. How: TARGETS fills when metrics arrive.

Example 4: Apply commands

Type this:

printf '%s\n' 'kubectl apply -f deploy.yaml -f hpa.yaml && kubectl get hpa'

You will see:

kubectl apply -f deploy.yaml -f hpa.yaml && kubectl get hpa

What it means: TARGETS column fills once metrics arrive.

Example 5: Watch HPA

Type this:

printf '%s\n' 'kubectl get hpa web -w'

You will see:

kubectl get hpa web -w

What it means: REPLICAS climbs when utilization stays above the target.

Example 6: Generate load sketch

Type this:

printf '%s\n' 'kubectl run load --rm -it --image=public.ecr.aws/docker/library/busybox:1.36 -- /bin/sh -c "while true; do wget -q -O- http://web/; done"'

You will see:

kubectl run load --rm -it --image=public.ecr.aws/docker/library/busybox:1.36 -- /bin/sh -c "while true; do wget -q -O- http://web/; done"

What it means: Steady traffic — Infosys labs use a loop, not one curl.

Concept 2: Load and events

Prove with traffic and describe.

Why: Guessing without events wastes hours. What: busybox loop + describe hpa. How: Read scale-up reasons in Events.

Example 7: Describe HPA events

Type this:

printf '%s\n' 'kubectl describe hpa web | sed -n "/Events/,+12p"'

You will see:

kubectl describe hpa web | sed -n "/Events/,+12p"

What it means: Events explain why it scaled or stayed put.

Example 8: Scale bounds

Type this:

printf '%s\n' 'minReplicas: 1' 'maxReplicas: 5  # cost ceiling for labs and startups'

You will see:

minReplicas: 1
maxReplicas: 5  # cost ceiling for labs and startups

What it means: Max is a bill guardrail as much as a capacity knob.

Example 9: Cool-down note

Type this:

printf '%s\n' 'After load stops, wait for the stabilization window before replicas drop'

You will see:

After load stops, wait for the stabilization window before replicas drop

What it means: Do not panic if scale-down is slower than scale-up.

Concept 3: Bounds and cleanup

maxReplicas caps cost; delete labs.

Why: Forgotten HPA labs leave expensive replica floors. What: Set max; delete -f when done. How: Know HPA vs VPA for interviews.

Example 10: Custom metric hint

Type this:

printf '%s\n' 'CPU first; custom metrics (queue depth) come after metrics-server works'

You will see:

CPU first; custom metrics (queue depth) come after metrics-server works

What it means: Beginners should prove Resource metrics before custom adapters.

Example 11: Delete lab

Type this:

printf '%s\n' 'kubectl delete -f hpa.yaml -f deploy.yaml'

You will see:

kubectl delete -f hpa.yaml -f deploy.yaml

What it means: Clean labs keep shared clusters tidy.

Example 12: Interview one-liner

Type this:

printf '%s\n' 'HPA changes replica count; VPA changes pod size — different knobs'

You will see:

HPA changes replica count; VPA changes pod size — different knobs

What it means: Amazon/TCS interviews love this contrast.

HPA card

1 metrics-server 2 requests on container 3 HPA YAML 4 apply 5 generate load 6 watch replicas

Suppose: unknown / targets empty

Install or fix metrics-server; wait a minute.

Suppose: never scales

Check requests exist and load actually hits the Service.

Ravindra Bagale's Tip

At Flipkart and Amazon interviews, explain: requests give the baseline, target is a percent of that baseline, maxReplicas is the cost ceiling.

Check yourself (quiz)

Q1. What does HPA need first?

Working metrics (metrics-server)

Q2. Why CPU requests?

Baseline for utilization %

Q3. What does maxReplicas protect?

Cost and cluster capacity

Q4. Empty TARGETS?

Metrics not ready

Q5. HPA vs VPA?

Replicas vs pod size

Q6. Scale-down slow?

Stabilization window — wait

Reading tracking

Mark this guide when you finish. Your browser remembers it on this device.

metrics → HPA → load → scale → cool down. Did you understand? Let us try it once more.

Frequently asked questions

Why unknown on HPA?

Metrics not flowing yet.

Do I need limits?

Requests matter most for HPA %; limits still help fairness.

Custom metrics?

After Resource CPU works.

Will it scale to zero?

Not with default minReplicas 1.

HPA vs Cluster Autoscaler?

Pods vs nodes.

Slow scale-down?

Stabilization — by design.