During traffic spikes, precise scaling improves response speed and increases cluster resource efficiency. This topic shows how to use Kubernetes External Metrics to integrate key business metrics—such as HTTP request rate and Ingress queries per second (QPS)—and build more automated scaling policies.
This topic walks you through the following steps to configure horizontal pod autoscaling (HPA) using Ingress QPS metrics from Simple Log Service (SLS). The example deploys a Deployment named nginx, a Service, and an Ingress.
Step 1: Deploy the ack-alibaba-cloud-metrics-adapter component
The ack-alibaba-cloud-metrics-adapter component enables Kubernetes to retrieve monitoring data from Alibaba Cloud services—including ECS, SLB, and RDS—through the External Metrics API. This enhances cluster monitoring and autoscaling.
Log on to the Container Service Management Console . In the navigation pane on the left, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, choose .
On the Helm page, click Create. Complete the Basic Information configuration. Select ack-alibaba-cloud-metrics-adapter and click Next.
On the Parameter Settings page, select a Chart Version and click OK.
The ack-alibaba-cloud-metrics-adapter component does not support smooth upgrades. To upgrade, uninstall the current version first and then install the latest version.
Step 2: Create an application and a service
Create a file named nginx-test.yaml.
Run the following command to create the Deployment and its Service.
kubectl apply -f nginx-test.yaml
Step 3: Create a route
In the left navigation pane of the cluster management page, choose . On the Ingress page, click Create Ingress in the upper-left corner.
Fill in the required fields in the Create panel and click OK. After creation, the system automatically redirects you to the Ingress page.
In the Name column, click the name of the Ingress you created to view its routing rules. For more information, see Manage Ingress.
Step 4: Configure HPA
You can configure two SLS Project metrics for scaling in HPA: sls_ingress_qps and sls_ingress_latency_p9999.
sls_ingress_qps: Use AverageValue. The QPS value is divided by the number of pods before comparison.
sls_ingress_latency_p9999: Use Value. The raw value is used without division.
Create a file named ingress-hpa.yaml and paste the following content into it.
The following table describes the parameters used in the HPA configuration.
Parameter name
Required
Description
sls.ingress.route
Yes
Format:
<namespace>-<svc>-<port>.<namespace>is the namespace where the Ingress resides.<svc>is the name of the Service associated with the Ingress.<port>is the port name of that Service. Example: default-nginx-80.sls.logstore
Yes
The name of the Logstore in Simple Log Service. By default,
sls.logstoreisnginx-ingress.sls.project
Yes
The name of the Project in Simple Log Service. By default,
sls.projectisk8s-log-<cluster-id>.sls.internal.endpoint
No
Set whether to access Simple Log Service over the internal network or the public network. Default is true.
true: Access SLS over the internal network.
false: Access SLS over the public network.
Run the following command to create the HPA.
kubectl apply -f ingress-hpa.yaml
Step 5: Verify the result
After configuring HPA, run the following command to perform stress testing.
ab -t 300 -c 10 <domain-name-configured-for-the-ingress> # Use Apache Benchmark to send 10 concurrent requests to the Ingress-exposed service for 300 seconds.Verify the scaling status.
Run the following command to check the status.
kubectl get hpa ingress-hpaExpected output:
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE ingress-hpa Deployment/nginx-deployment-basic 21/10 (avg) 2 10 10 7m49sIf the REPLICAS value equals the MAXPODS value, scaling succeeded.
FAQ
How do I obtain the sls_ingress_qps metric using the command line?
You can run the following command to query data. The sls_ingress_qps request metric is used as an example.
kubectl get --raw /apis/external.metrics.k8s.io/v1beta1/namespaces/*/sls_ingress_qps?labelSelector=sls.project={{SLS_Project}},sls.logstore=nginx-ingress
In the preceding command, {{SLS_Project}} is the name of the Simple Log Service project that corresponds to the ACK cluster. If you do not specify a custom configuration, the default project name is k8s-log-{{ClusterId}}. {{ClusterId}} is the ID of the cluster.
If the following result is returned:
Error from server: {
"httpCode": 400,
"errorCode": "ParameterInvalid",
"errorMessage": "key (slb_pool_name) is not config as key value config,if symbol : is in your log,please wrap : with quotation mark \"",
"requestID": "xxxxxxx"
}
This indicates that no data is available for this metric. This may be because ALB Ingress is not configured, but the sls_alb_ingress_qps metric was used for the data query.
If a result similar to the following is returned:
{
"kind": "ExternalMetricValueList",
"apiVersion": "external.metrics.k8s.io/v1beta1",
"metadata": {},
"items": [
{
"metricName": "sls_ingress_qps",
"timestamp": "2025-02-26T16:45:00Z",
"value": "50", # QPS value
"metricLabels": {
"sls.project": "your-sls-project-name",
"sls.logstore": "nginx-ingress"
}
}
]
}
This indicates that the queries per second (QPS) of the Kubernetes external metric is found. value is the QPS value.
What if the target column is unknown after running kubectl get hpa?
You can perform the following steps to resolve this issue.
-
Run the
kubectl describe hpa <hpa_name>command to identify why HPA is not working.-
If the value of
AbleToScalein theConditionsfield isFalse, confirm that the deployment is normal. -
If the value of
ScalingActivein theConditionsfield isFalse, proceed to the next step.
-
-
Run the
kubectl get --raw "/apis/external.metrics.k8s.io/v1beta1/"command. IfError from server (NotFound): the server could not find the requested resourceis returned, check the startup status of alibaba-cloud-metrics-adapter.If alibaba-cloud-metrics-adapter is running as normal, confirm whether the HPA metrics are related to Ingress. If they are, you must deploy the Simple Log Service component in advance. For more information, see Collect and analyze Nginx Ingress access logs.
-
Confirm that the HPA metrics are entered correctly. The value format for sls.ingress.route is
<namespace>-<svc>-<port>.-
namespace: The namespace where the Ingress is located. -
svc: The name of the Service that corresponds to the Ingress. -
port: The name of the port for the Service that corresponds to the Ingress.
-
How do I find the metric names supported by HPA?
For more information, see Alibaba Cloud HPA Metrics. The following table lists common metrics.
|
Metric name |
Description |
Additional parameters |
|
sls_ingress_qps |
Queries per second (QPS) for the specified IngressRoute |
sls.ingress.route |
|
sls_alb_ingress_qps |
Queries per second (QPS) for ALB data IngressRoute |
sls.ingress.route |
|
sls_ingress_latency_avg |
Latency for all requests |
sls.ingress.route |
|
sls_ingress_latency_p50 |
Latency for 50% of requests |
sls.ingress.route |
|
sls_ingress_latency_p95 |
Latency for 95% of requests |
sls.ingress.route |
|
sls_ingress_latency_p99 |
Latency for 99% of requests |
sls.ingress.route |
|
sls_ingress_latency_p9999 |
Latency for 99.99% of requests |
sls.ingress.route |
|
sls_ingress_inflow |
Inbound bandwidth for Ingress |
sls.ingress.route |
How to Adapt After Customizing the Nginx Ingress Log Format?
For more information about how to use SLS Ingress metrics for horizontal pod autoscaling, see Horizontal pod autoscaling based on Nginx Ingress component metrics. You must enable and correctly configure Nginx Ingress logs to be ingested into Simple Log Service in your cluster.
-
When you create a cluster, Simple Log Service is enabled by default. If you retain the default settings, you can view Nginx Ingress access log analysis reports and monitor the real-time status of Nginx Ingress in the Simple Log Service console after the cluster is created.
-
If you manually disabled Simple Log Service when you created the cluster and you want to use SLS Ingress metrics for horizontal pod autoscaling after the cluster is created, you must re-enable or configure Simple Log Service. For more information, see Collect and analyze Nginx Ingress access logs.
-
To customize the Nginx Ingress log format, the AliyunLogConfig custom resource definition (CRD) that is deployed when Simple Log Service is first enabled in the cluster applies only to the log format in the default ACK Ingress Controller. If you modified the access log format of the Ingress Controller, you must modify the
processor_regexsection in the regular expression of the CRD configuration. For more information, see Collect container logs using a DaemonSet and a CRD.
Failed to pull alibaba-cloud-metrics-adapter image
Symptom
When you upgrade the ack-alibaba-cloud-metrics-adapter component to version 1.3.7, an error occurs while you pull the image. The error message is as follows:
Failed to pull image "registry-<region-id>-vpc.ack.aliyuncs.com/acs/alibaba-cloud-metrics-adapter-amd64:v0.2.9-ba634de-aliyun".
Cause
The ack-alibaba-cloud-metrics-adapter component does not support direct updates.
Solution
You can perform the following steps to upgrade the component.
-
Back up the current component configuration.
-
Uninstall the old version of the component.
-
Install the latest version of the component using the backup configuration.
During the uninstallation and reinstallation of the component, related HPAs pause scaling because monitoring data retrieval stops.