Typorithm · medium

HorizontalPodAutoscaler

hpakubernetesautoscalingmetricsreplicas
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn HorizontalPodAutoscaler

0%

Config & Scaling

A HorizontalPodAutoscaler scales a Deployment between minReplicas and maxReplicas to hold a target metric, like averageUtilization 70% CPU, using the autoscaling/v2 API.

An HPA automatically adjusts the replica count of a workload to meet demand, scaling out under load and back in when idle.

scaleTargetRef names the Deployment to scale; minReplicas and maxReplicas bound the range it may use.

metrics with target averageUtilization tell the HPA the goal — it computes desired replicas to keep the observed metric near that value.

01

autoscaling/v2 supports multiple metrics.

02

scaleTargetRef names the Deployment.

03

averageUtilization sets the target.

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70

Read the implementation

Connect each part of the code to the concept before you type.

1. v2 API

autoscaling/v2 supports multiple and custom metrics, unlike the older v1 CPU-only API.

apiVersion: autoscaling/v2

2. Scale target

scaleTargetRef points the HPA at the Deployment whose replica count it adjusts.

scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web

3. Replica bounds

The HPA keeps the replica count between minReplicas and maxReplicas.

minReplicas: 2
  maxReplicas: 10

4. Target metric

averageUtilization: 70 tells the HPA to add or remove pods to keep average CPU near 70%.

target:
          type: Utilization
          averageUtilization: 70

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

HorizontalPodAutoscaler: Lesson & Practice · Typorithm | ApplyDjinn