Configuring HPA
HPA (Horizontal Pod Autoscaler) automatically scales the number of pods up or down based on preset policies and metrics, enabling applications to handle sudden spikes in business load while optimizing resource utilization during low-traffic periods.
TOC
Understanding Horizontal Pod Autoscalers
You can create a horizontal pod autoscaler to specify the minimum and maximum number of pods you want to run, as well as the CPU utilization or memory utilization your pods should target.
After you create a horizontal pod autoscaler, the platform begins to query the CPU and/or memory resource metrics on the pods. When these metrics are available, the horizontal pod autoscaler computes the ratio of the current metric utilization with the desired metric utilization, and scales up or down accordingly. The query and scaling occurs at a regular interval, but can take one to two minutes before metrics become available.
For replication controllers, this scaling corresponds directly to the replicas of the replication controller. For deployment configurations, scaling corresponds directly to the replica count of the deployment configuration. Note that autoscaling applies only to the latest deployment in the Complete phase.
The platform automatically accounts for resources and prevents unnecessary autoscaling during resource spikes, such as during start up. Pods in the unready state have 0 CPU usage when scaling up and the autoscaler ignores the pods when scaling down. Pods without known metrics have 0% CPU usage when scaling up and 100% CPU when scaling down. This allows for more stability during the HPA decision. To use this feature, you must configure readiness checks to determine if a new pod is ready for use.
How Does the HPA Work?
The horizontal pod autoscaler (HPA) extends the concept of pod auto-scaling. The HPA lets you create and manage a group of load-balanced nodes. The HPA automatically increases or decreases the number of pods when a given CPU or memory threshold is crossed.
The HPA works as a control loop with a default of 15 seconds for the sync period. During this period, the controller manager queries the CPU, memory utilization, or both, against what is defined in the configuration for the HPA. The controller manager obtains the utilization metrics from the resource metrics API for per-pod resource metrics like CPU or memory, for each pod that is targeted by the HPA.
If a utilization value target is set, the controller calculates the utilization value as a percentage of the equivalent resource request on the containers in each pod. The controller then takes the average of utilization across all targeted pods and produces a ratio that is used to scale the number of desired replicas.
Supported Metrics
The following metrics are supported by horizontal pod autoscalers:
Important: For memory-based autoscaling, memory usage must increase and decrease proportionally to the replica count. On average:
- An increase in replica count must lead to an overall decrease in memory (working set) usage per-pod.
- A decrease in replica count must lead to an overall increase in per-pod memory usage.
- Use the platform to check the memory behavior of your application and ensure that your application meets these requirements before using memory-based autoscaling.
Prerequisites
Please ensure that the monitoring components are deployed in the current cluster and are functioning properly. You can check the deployment and health status of the monitoring components by clicking on the top right corner of the platform > Platform Health Status..
Creating a Horizontal Pod Autoscaler
Using the CLI
You can create a horizontal pod autoscaler using the command line interface by defining a YAML file and using the kubectl create command. The following example shows autoscaling for a Deployment object. The initial deployment requires 3 pods. The HPA object increases the minimum to 5. If CPU usage on the pods reaches 75%, the pods increase to 7:
-
Create a YAML file named
hpa.yamlwith the following content:- Use the autoscaling/v2 API.
- The name of the HPA resource.
- The name of the deployment to scale.
- The maximum number of replicas to scale up to.
- The minimum number of replicas to maintain.
- Specify the API version of the object to scale.
- Specify the type of object. The object must be a Deployment, ReplicaSet, or StatefulSet.
- The target resource to which the HPA applies.
- The target CPU utilization percentage that triggers scaling.
-
Apply the YAML file to create the HPA:
Example output:
-
After you create the HPA, you can view the new state of the deployment by running the following command:
Example output:
-
You can also check the status of your HPA:
Example output:
Using the Web Console
-
Enter Container Platform.
-
In the left navigation bar, click Workloads > Deployments.
-
Click on Deployment Name.
-
Scroll down to the Elastic Scaling area and click on Update on the right.
-
Select Horizontal Scaling and complete the policy configuration.
-
Click Update.
Using Custom Metrics for HPA
Custom metrics HPA extends the original HorizontalPodAutoscaler by supporting additional metrics beyond CPU and memory utilization.
Requirements
- kube-controller-manager: horizontal-pod-autoscaler-use-rest-clients=true
- Pre-installed metrics-server
- Prometheus
- custom-metrics-api
Traditional (Core Metrics) HPA
Traditional HPA supports CPU utilization and memory metrics to dynamically adjust the number of Pod instances, as shown in the example below:
In this YAML, scaleTargetRef specifies the workload object for scaling, and targetCPUUtilizationPercentage specifies the CPU utilization trigger metric.
Custom Metrics HPA
To use custom metrics, you need to install prometheus-operator and custom-metrics-api. After installation, custom-metrics-api provides a large number of custom metric resources:
These resources are all sub-resources under MetricValueList. You can create rules through Prometheus to create or maintain sub-resources. The HPA YAML format for custom metrics differs from traditional HPA:
In this example, scaleTargetRef specifies the workload.
Trigger Condition Definition
metricsis an array type, supporting multiple metricsmetric typecan be: Object (describing k8s resources), Pods (describing metrics for each Pod), Resources (built-in k8s metrics: CPU, memory), or External (typically metrics external to the cluster)- If the custom metric is not provided by Prometheus, you need to create a new metric through a series of operations such as creating rules in Prometheus
The main structure of a metric is as follows:
This metric data is collected and updated by Prometheus.
Custom Metrics HPA Compatibility
Custom metrics HPA YAML is actually compatible with the original core metrics (CPU). Here's how to write it:
targetAverageValueis the average value obtained for each PodtargetAverageUtilizationis the utilization calculated from the direct value
The algorithm reference is:
Updates in autoscaling/v2beta2
autoscaling/v2beta2 supports memory utilization:
Changes: targetAverageUtilization and targetAverageValue have been changed to target and converted to a combination of xxxValue and type:
xxxValue: AverageValue (average value), AverageUtilization (average utilization), Value (direct value)type: Utilization (utilization), AverageValue (average value)
Notes:
-
For CPU Utilization and Memory Utilization metrics, auto-scaling will only be triggered when the actual value fluctuates outside the range of ±10% of the target threshold.
-
Scale-down may impact ongoing business operations; please proceed with caution.
Calculation Rules
When business metrics change, the platform will automatically calculate the target pod count that matches the business volume according to the following rules and adjust accordingly. If the business metrics continue to fluctuate, the value will be adjusted to the set Minimum Pod Count or Maximum Pod Count.
-
Single Policy Target Pod Count: ceil[(sum(actual metric values)/metric threshold)] . This means that the sum of the actual metric values of all pods divided by the metric threshold, rounded up to the smallest integer that is greater than or equal to the result. For example: If there are currently 3 pods with CPU utilizations of 80%, 80%, and 90%, and the set CPU utilization threshold is 60%. According to the formula, the number of pods will be automatically adjusted to: ceil[(80%+80%+90%)/60%] = ceil 4.1 = 5 pods.
Note:
-
If the calculated target pod count exceeds the set Maximum Pod Count (for example 4), the platform will only scale up to 4 pods. If after changing the maximum pod count the metrics remain persistently high, you may need to use alternate scaling methods, such as increasing the namespace pod quota or adding hardware resources.
-
If the calculated target pod count (in the previous example 5) is less than the pod count adjusted according to the Scale-Up Step (for example 10), the platform will only scale up to 5 pods.
-
-
Multiple Policy Target Pod Count: Take the maximum value among the results of each policy calculation.