Bird
Raised Fist0
Kubernetesdevops~20 mins

Horizontal Pod Autoscaler in Kubernetes - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Horizontal Pod Autoscaler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of HPA status command
What is the output of the following command after deploying an HPA targeting a deployment named webapp with min replicas 2 and max replicas 5, currently scaling to 3 replicas based on CPU usage?
Kubernetes
kubectl get hpa webapp
A
NAME    REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
webapp  Deployment/webapp   60%/50%   2         5         3          10m
B
NAME    REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
webapp  Deployment/webapp   70%/50%   2         5         4          10m
C
NAME    REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
webapp  Deployment/webapp   60%/60%   2         5         2          10m
D
NAME    REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
webapp  Deployment/webapp   50%/60%   2         5         3          10m
Attempts:
2 left
💡 Hint
Look carefully at the TARGETS column showing current CPU usage versus target CPU utilization.
🧠 Conceptual
intermediate
2:00remaining
Understanding HPA scaling behavior
If a Horizontal Pod Autoscaler is configured with minReplicas=1, maxReplicas=4, and target CPU utilization of 50%, what happens when the CPU usage stays at 30% for a long time?
AThe HPA will keep the pods at the current number, ignoring CPU usage below target.
BThe HPA will scale down the pods to 1, the minimum number of replicas.
CThe HPA will scale up the pods to 4, the maximum number of replicas.
DThe HPA will delete all pods because CPU usage is low.
Attempts:
2 left
💡 Hint
Think about what the minimum replicas setting means for scaling down.
❓ Configuration
advanced
3:00remaining
Correct HPA YAML for CPU autoscaling
Which YAML snippet correctly defines a Horizontal Pod Autoscaler for a deployment named api-server with min 3 replicas, max 10 replicas, targeting 75% CPU utilization?
A
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: api-server
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-server
  minReplicas: 3
  maxReplicas: 10
  targetMemoryUtilizationPercentage: 75
B
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: api-server
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-server
  minReplicas: 3
  maxReplicas: 10
  targetCPUUtilizationPercentage: 75
C
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api-server
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-server
  minReplicas: 3
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 75
D
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api-server
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-server
  minReplicas: 3
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 75
Attempts:
2 left
💡 Hint
The autoscaling/v2 API uses a metrics array with resource type and target details.
❓ Troubleshoot
advanced
2:00remaining
Troubleshooting HPA not scaling up
You created an HPA for a deployment, but the pods never scale beyond 1 even when CPU usage is high. Which is the most likely cause?
AThe maxReplicas is set to 1, limiting scaling to a single pod.
BThe HPA minReplicas is set to 1, so it cannot scale beyond that.
CThe deployment's resource requests for CPU are not set, so HPA cannot calculate usage properly.
DThe deployment has too many replicas already running.
Attempts:
2 left
💡 Hint
Check the maxReplicas setting to see if it limits scaling.
✅ Best Practice
expert
3:00remaining
Best practice for HPA metrics configuration
Which approach is best practice when configuring Horizontal Pod Autoscaler metrics for a web application that experiences variable CPU and memory usage?
AAvoid using metrics and set fixed replica counts to prevent unpredictable scaling.
BConfigure only memory usage metric because CPU usage fluctuates too much to be reliable.
CConfigure multiple metrics including CPU utilization and memory usage with appropriate targets to allow balanced scaling.
DConfigure only CPU utilization metric because memory usage is less important for scaling decisions.
Attempts:
2 left
💡 Hint
Consider how multiple metrics can improve scaling decisions.

Practice

(1/5)
1. What is the main purpose of a Horizontal Pod Autoscaler (HPA) in Kubernetes?
easy
A. Manually increase pod count when needed
B. Automatically adjust the number of pods based on resource usage
C. Scale the number of nodes in the cluster
D. Backup pod data automatically

Solution

  1. Step 1: Understand HPA functionality

    HPA monitors resource usage like CPU or memory and adjusts pod count automatically.
  2. Step 2: Differentiate from other scaling types

    HPA scales pods, not nodes or manual actions, and does not handle backups.
  3. Final Answer:

    Automatically adjust the number of pods based on resource usage -> Option B
  4. Quick Check:

    HPA = automatic pod scaling [OK]
Hint: HPA changes pod count automatically by resource use [OK]
Common Mistakes:
  • Confusing HPA with manual scaling
  • Thinking HPA scales nodes instead of pods
  • Assuming HPA handles backups
2. Which of the following is the correct command to create a Horizontal Pod Autoscaler for a deployment named webapp targeting 50% CPU usage with minimum 2 and maximum 5 pods?
easy
A. kubectl autoscale deployment webapp --cpu=50 --min=2 --max=5
B. kubectl create hpa webapp --cpu=50 --min-pods=2 --max-pods=5
C. kubectl autoscale deployment webapp --cpu-percent=50 --min=2 --max=5
D. kubectl autoscale deployment webapp --cpu-percent=50 --min-pods=2 --max-pods=5

Solution

  1. Step 1: Recall correct kubectl autoscale syntax

    The correct command is kubectl autoscale deployment [name] --cpu-percent=[value] --min=[minPods] --max=[maxPods].
  2. Step 2: Match options with correct syntax

    kubectl autoscale deployment webapp --cpu-percent=50 --min=2 --max=5 matches the correct syntax exactly with proper flags and values.
  3. Final Answer:

    kubectl autoscale deployment webapp --cpu-percent=50 --min=2 --max=5 -> Option C
  4. Quick Check:

    Correct autoscale syntax = kubectl autoscale deployment webapp --cpu-percent=50 --min=2 --max=5 [OK]
Hint: Use kubectl autoscale with --cpu-percent, --min, --max [OK]
Common Mistakes:
  • Using wrong flags like --min-pods or --cpu
  • Misspelling 'autoscale' or 'deployment'
  • Omitting required flags
3. Given this HPA YAML snippet:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: myapp-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: myapp
  minReplicas: 1
  maxReplicas: 4
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 60

What happens if the CPU usage stays at 80% for a while?
medium
A. The number of pods will increase up to 4 to reduce CPU load
B. The number of pods will decrease to 1
C. The number of pods will stay the same regardless of CPU
D. The deployment will be deleted automatically

Solution

  1. Step 1: Understand HPA scaling behavior

    HPA increases pods when CPU usage exceeds target averageUtilization (60%). Here CPU is 80%, which is higher.
  2. Step 2: Check min and max replicas limits

    Pods can scale between 1 and 4. Since CPU is high, pods will increase up to max 4 to reduce load.
  3. Final Answer:

    The number of pods will increase up to 4 to reduce CPU load -> Option A
  4. Quick Check:

    CPU > target => pods increase [OK]
Hint: CPU above target means pods scale up within min-max [OK]
Common Mistakes:
  • Thinking pods scale down when CPU is high
  • Ignoring min/max replica limits
  • Assuming deployment deletion on high CPU
4. You applied this HPA YAML but it does not scale pods as expected:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: test-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: test-app
  minReplicas: 2
  maxReplicas: 5
  metrics:
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 70

What is the most likely reason for no scaling?
medium
A. The pods do not expose memory metrics, so HPA has no data
B. minReplicas is set too low
C. maxReplicas is less than minReplicas
D. The deployment name is incorrect

Solution

  1. Step 1: Check metric type and data availability

    HPA needs metrics from pods. If memory metrics are not exposed or collected, HPA cannot scale.
  2. Step 2: Verify other spec values

    minReplicas and maxReplicas are valid (2 and 5). Deployment name looks correct. So likely missing metrics cause no scaling.
  3. Final Answer:

    The pods do not expose memory metrics, so HPA has no data -> Option A
  4. Quick Check:

    Missing metrics = no scaling [OK]
Hint: HPA needs metrics; no metrics means no scaling [OK]
Common Mistakes:
  • Assuming minReplicas too low stops scaling
  • Confusing minReplicas and maxReplicas values
  • Ignoring metric availability
5. You want to create an HPA that scales a deployment named api-server based on both CPU usage (target 70%) and custom metric requests_per_second (target 100). The minimum pods should be 3 and maximum 10. Which YAML snippet correctly defines this HPA?
hard
A. apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: api-server-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: api-server minReplicas: 3 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70 - type: Resource resource: name: requests_per_second target: type: AverageValue averageValue: "100"
B. apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: api-server-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: api-server minReplicas: 3 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: AverageValue averageValue: "70" - type: Pods pods: metric: name: requests_per_second target: type: AverageValue averageValue: "100"
C. apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata: name: api-server-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: api-server minReplicas: 3 maxReplicas: 10 targetCPUUtilizationPercentage: 70
D. apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: api-server-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: api-server minReplicas: 3 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70 - type: Pods pods: metric: name: requests_per_second target: type: AverageValue averageValue: "100"

Solution

  1. Step 1: Use autoscaling/v2 API for multiple metrics

    autoscaling/v2 supports multiple metrics including resource and custom metrics.
  2. Step 2: Check metric types and targets

    CPU uses type Utilization with averageUtilization; custom metric uses type Pods with metric name and AverageValue target.
  3. Step 3: Verify minReplicas and maxReplicas

    Minimum 3 and maximum 10 pods are correctly set in spec.
  4. Final Answer:

    YAML with cpu resource metric (Utilization) and pods metric for requests_per_second -> Option D
  5. Quick Check:

    Multiple metrics with correct types = apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: api-server-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: api-server minReplicas: 3 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70 - type: Pods pods: metric: name: requests_per_second target: type: AverageValue averageValue: "100" [OK]
Hint: Use autoscaling/v2 with resource and pods metrics for multiple targets [OK]
Common Mistakes:
  • Using autoscaling/v1 which does not support multiple metrics
  • Wrong metric types or target fields
  • Mixing resource and custom metric syntax incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard