Challenge, Medium,  on  Kubernetes

Set Up Horizontal Pod Autoscaler with CPU and Memory Targets

Scenario

A Deployment named sensor-api is already running in the iot-sys namespace with 12 replicas. The Kubernetes Metrics Server has already been installed and configured.


Task

Configure autoscaling for the sensor-api Deployment by creating a Horizontal Pod Autoscaler named sensor-hpa in the iot-sys namespace with the following requirements:

  • Scale between 2 and 8 replicas
  • Use CPU utilization with a target of 80%
  • Use Memory utilization with a target of 80%
  • Configure stabilizationWindowSeconds: 5 for scale down behavior

This configuration ensures that the Deployment gradually scales down from 12 pods to the appropriate number (minimum 2)

After creating the HPA, wait 20–30 seconds for metrics to be collected. If the status still shows Unknown, verify that the HPA is configured correctly.


Hint 1

HPA with multiple metrics and custom behavior requires a YAML manifest — kubectl autoscale only supports CPU. Use spec.metrics for CPU and memory targets and spec.behavior.scaleDown.stabilizationWindowSeconds for scale-down behavior.

See the official docs: Horizontal Pod Autoscaling


Test Cases