Bird
Raised Fist0
Microservicessystem_design~10 mins

Horizontal Pod Autoscaler in Microservices - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the metric type for CPU utilization in a Horizontal Pod Autoscaler YAML.

Microservices
metrics:
  - type: [1]
Drag options to blanks, or click blank then click option'
AResource
BCustom
CMemory
DObject
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'Memory' instead of 'Resource' as the metric type.
Confusing 'Custom' metrics with built-in resource metrics.
2fill in blank
medium

Complete the code to set the target average CPU utilization percentage in the Horizontal Pod Autoscaler spec.

Microservices
spec:
  targetCPUUtilizationPercentage: [1]
Drag options to blanks, or click blank then click option'
A100
B20
C80
D50
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Setting the target too low causing frequent scaling.
Setting the target too high causing resource exhaustion.
3fill in blank
hard

Fix the error in the YAML snippet by completing the missing field for specifying the resource name in the metric.

Microservices
metrics:
  - type: Resource
    resource:
      name: [1]
Drag options to blanks, or click blank then click option'
Anetwork
Bcpu
Cmemory
Ddisk
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'disk' or 'network' which are not standard resource names for HPA.
Using uppercase letters in resource names.
4fill in blank
hard

Fill both blanks to complete the YAML snippet for setting minimum and maximum replicas in Horizontal Pod Autoscaler.

Microservices
spec:
  minReplicas: [1]
  maxReplicas: [2]
Drag options to blanks, or click blank then click option'
A2
B10
C5
D20
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Setting minReplicas higher than maxReplicas.
Setting maxReplicas too low causing insufficient scaling.
5fill in blank
hard

Fill all three blanks to complete the YAML snippet for a Horizontal Pod Autoscaler targeting a deployment named 'web-app' in the 'default' namespace.

Microservices
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web-app-hpa
  namespace: [1]
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: [2]
  minReplicas: 1
  maxReplicas: 5
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: [3]
Drag options to blanks, or click blank then click option'
Adefault
Bweb-app
C75
Dproduction
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using incorrect namespace or deployment name.
Setting unrealistic average utilization values.

Practice

(1/5)
1. What is the primary purpose of a Horizontal Pod Autoscaler in a Kubernetes microservices environment?
easy
A. Store persistent data for pods
B. Manually restart pods when they fail
C. Balance network traffic between pods
D. Automatically adjust the number of pods based on CPU or custom metrics

Solution

  1. Step 1: Understand the role of Horizontal Pod Autoscaler

    It is designed to monitor resource usage like CPU or custom metrics and adjust pod count automatically.
  2. Step 2: Compare options with this role

    Only Automatically adjust the number of pods based on CPU or custom metrics describes automatic scaling based on load, which matches the autoscaler's purpose.
  3. Final Answer:

    Automatically adjust the number of pods based on CPU or custom metrics -> Option D
  4. Quick Check:

    Autoscaler adjusts pods automatically = A [OK]
Hint: Autoscaler changes pod count automatically based on load [OK]
Common Mistakes:
  • Confusing autoscaler with manual pod management
  • Thinking it balances network traffic
  • Assuming it stores data persistently
2. Which of the following is the correct YAML snippet to define a Horizontal Pod Autoscaler targeting CPU utilization at 50% for a deployment named web-app?
easy
A. apiVersion: autoscaling/v2\nkind: HorizontalPodAutoscaler\nmetadata:\n name: web-app-hpa\nspec:\n scaleTargetRef:\n apiVersion: apps/v1\n kind: Deployment\n name: web-app\n minReplicas: 1\n maxReplicas: 5\n metrics:\n - type: Resource\n resource:\n name: cpu\n target:\n type: Utilization\n averageUtilization: 70
B. apiVersion: v1\nkind: Pod\nmetadata:\n name: web-app\nspec:\n containers:\n - name: web-app\n image: web-app:latest
C. apiVersion: autoscaling/v1\nkind: HorizontalPodAutoscaler\nmetadata:\n name: web-app-hpa\nspec:\n scaleTargetRef:\n apiVersion: apps/v1\n kind: Deployment\n name: web-app\n minReplicas: 2\n maxReplicas: 10\n targetCPUUtilizationPercentage: 50
D. apiVersion: autoscaling/v2beta2\nkind: HorizontalPodAutoscaler\nmetadata:\n name: web-app-hpa\nspec:\n scaleTargetRef:\n apiVersion: apps/v1\n kind: Deployment\n name: web-app\n minReplicas: 1\n maxReplicas: 5\n metrics:\n - type: Resource\n resource:\n name: memory\n target:\n type: Utilization\n averageUtilization: 50

Solution

  1. Step 1: Identify correct API version and fields for CPU target

    autoscaling/v1 supports targetCPUUtilizationPercentage directly; v2 requires metrics array.
  2. Step 2: Check min/max replicas and target CPU utilization

    apiVersion: autoscaling/v1\nkind: HorizontalPodAutoscaler\nmetadata:\n name: web-app-hpa\nspec:\n scaleTargetRef:\n apiVersion: apps/v1\n kind: Deployment\n name: web-app\n minReplicas: 2\n maxReplicas: 10\n targetCPUUtilizationPercentage: 50 uses autoscaling/v1 with minReplicas 2, maxReplicas 10, and targetCPUUtilizationPercentage 50, which is valid syntax.
  3. Final Answer:

    YAML with autoscaling/v1 and targetCPUUtilizationPercentage 50% -> Option C
  4. Quick Check:

    autoscaling/v1 + targetCPUUtilizationPercentage = B [OK]
Hint: autoscaling/v1 uses targetCPUUtilizationPercentage field [OK]
Common Mistakes:
  • Using wrong apiVersion for the fields
  • Confusing CPU with memory metrics
  • Setting minReplicas higher than maxReplicas
3. Given this Horizontal Pod Autoscaler configuration:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-server
  minReplicas: 2
  maxReplicas: 6
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 60

If the current CPU usage is 90% and there are 3 pods running, how many pods will the autoscaler try to set?
medium
A. 5 pods
B. 3 pods
C. 6 pods
D. 4 pods

Solution

  1. Step 1: Understand scaling formula based on CPU utilization

    Desired replicas = current replicas * (current CPU / target CPU) = 3 * (90/60) = 4.5
  2. Step 2: Round up and check min/max limits

    4.5 rounds up to 5, which is between minReplicas 2 and maxReplicas 6, so 5 pods will be set.
  3. Final Answer:

    5 pods -> Option A
  4. Quick Check:

    3 * (90/60) = 4.5 -> 5 pods [OK]
Hint: Multiply current pods by (current CPU รท target CPU) [OK]
Common Mistakes:
  • Rounding down instead of up
  • Ignoring min/max replica limits
  • Using target CPU as current CPU
4. You configured a Horizontal Pod Autoscaler but notice it never scales pods beyond the minimum replicas even under high load. What is the most likely cause?
medium
A. The maxReplicas is set lower than minReplicas
B. The metrics server is not running or not providing metrics
C. The deployment has too many replicas already
D. The pods are using too little CPU

Solution

  1. Step 1: Check autoscaler dependency on metrics

    Horizontal Pod Autoscaler requires metrics server to get CPU or custom metrics to decide scaling.
  2. Step 2: Understand effect of missing metrics

    If metrics server is missing or not providing data, autoscaler cannot detect load and keeps pods at minReplicas.
  3. Final Answer:

    The metrics server is not running or not providing metrics -> Option B
  4. Quick Check:

    Missing metrics = no scaling beyond minReplicas [OK]
Hint: Autoscaler needs metrics server to scale pods [OK]
Common Mistakes:
  • Assuming maxReplicas lower than minReplicas causes this
  • Thinking high load always triggers scaling
  • Ignoring metrics server setup
5. You want to design a microservices system that scales pods horizontally based on both CPU usage and custom queue length metrics. Which approach best uses Horizontal Pod Autoscaler to achieve this?
hard
A. Configure HPA with multiple metrics: CPU utilization and custom queue length, setting thresholds for both
B. Use two separate HPAs, one for CPU and one for queue length, targeting the same deployment
C. Scale pods manually based on CPU and queue length metrics collected externally
D. Configure HPA to scale only on CPU and ignore queue length metrics

Solution

  1. Step 1: Understand HPA multi-metric support

    Horizontal Pod Autoscaler supports multiple metrics in a single configuration to scale pods based on combined criteria.
  2. Step 2: Evaluate options for best practice

    Configure HPA with multiple metrics: CPU utilization and custom queue length, setting thresholds for both uses multiple metrics in one HPA, which is efficient and avoids conflicts from multiple HPAs targeting the same deployment.
  3. Final Answer:

    Configure HPA with multiple metrics: CPU utilization and custom queue length, setting thresholds for both -> Option A
  4. Quick Check:

    Single HPA with multiple metrics = A [OK]
Hint: Use one HPA with multiple metrics for combined scaling [OK]
Common Mistakes:
  • Using multiple HPAs on same deployment causing conflicts
  • Ignoring custom metrics support
  • Relying only on CPU metrics