This topic describes how to configure external metrics supported by Kubernetes, such as the HTTP request rate and the Ingress queries per second (QPS), to implement horizontal auto scaling for applications in an environment on Alibaba Cloud.
Prerequisites
A Container Service for Kubernetes (ACK) cluster is created. For more information, see Create an ACK managed cluster, Create an ACK dedicated cluster, and Create an ACK Serverless cluster.
Deploy alibaba-cloud-metrics-adapter
Log on to the ACK console. In the left-side navigation pane, click Clusters.
On the Clusters page, click the name of the cluster that you want to manage and choose in the left-side navigation pane.
On the Helm page, click Deploy. In the Basic Information step, configure the parameters based on the following table.
Parameter
Example
Application Name
ack-alibaba-cloud-metrics-adapter
Namespace
kube-system
Source
Default value: Marketplace.
Chart
Select All for the Use Scenarios parameter.
Select amd64 for the Supported Architecture parameter.
Search box: Enter ack-alibaba-cloud-metrics-adapter.
Select ack-alibaba-cloud-metrics-adapter and click Next.
In the Parameters step, configure the Chart Version parameter and click OK.
HPA example
The following example shows how to configure HPA by creating a Deployment and a Service that are both named Nginx.
Log on to the ACK console. In the left-side navigation pane, click Clusters.
On the Clusters page, find the cluster that you want to manage and click its name. In the left-side pane, choose
.On the Deployments page, click Create from YAML in the upper-right corner.
Set Sample Template to Custom. Use the following YAML template to create a Deployment and a ClusterIP Service. Then, click Create.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment-basic labels: app: nginx spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: nginx namespace: default spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: app: nginx type: ClusterIP
In the left-side navigation pane, choose
. On the Ingresses page, click Create Ingress in the upper-right corner.In the Create Ingress dialog box, set the parameters and click OK. After the Ingress is created, the Ingresses page appears.
In the Name column, find and click the name of the newly created Ingress to view information about the Ingress.
Configure HPA.
Click the icon in the upper-left corner to return to the Clusters page.
In the left-side navigation pane of the ACK console, choose .
- On the Templates page, click Create in the upper-right corner.
In the Create dialog box, configure the template and click Save.
Name: Enter a name for the template. In this example, HPA is used.
Description: Enter a description for the template. This parameter is optional.
Template: Configure an orchestration template that complies with the YAML syntax of Kubernetes. For more information, see the following template.
apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: ingress-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: nginx-deployment-basic minReplicas: 2 maxReplicas: 10 metrics: - type: External external: metric: name: sls_ingress_qps selector: matchLabels: sls.project: "***" # Specify the Simple Log Service project that you want to use. sls.logstore: "nginx-ingress" sls.ingress.route: "default-nginx-80" target: type: AverageValue averageValue: 10 - type: External external: metric: name: sls_ingress_latency_p9999 selector: matchLabels: # default ingress log project is k8s-log-clusterId sls.project: "***" # default ingress logstre is nginx-ingress sls.logstore: "nginx-ingress" # namespace-svc-port sls.ingress.route: "default-nginx-80" # sls vpc endpoint, default true # sls.internal.endpoint:true target: type: Value # sls_ingress_latency_p9999 > 10ms value: 10
The following table describes the parameters that are used to configure HPA.
Parameter
Description
sls.ingress.route
Set the value in the
<namespace>-<svc>-<port>
format. Example: default-nginx-80. This parameter is required.Note<namespace>
specifies the namespace to which the Ingress belongs.<svc>
specifies the name of the Service that you selected when you created the Ingress.<port>
specifies the port of the Service.sls.logstore
The name of the Logstore in Simple Log Service. This parameter is required. The default value of
sls.logstore
isnginx-ingress
.sls.project
The name of the Simple Log Service project. This parameter is required. The default value of
sls.project
isk8s-log-cluster ID
.sls.internal.endpoint
Specifies whether to access Simple Log Service over the internal network. Default value: true. If you set the value to true, you can access Simple Log Service over the internal network. If you set the value to false, you can access Simple Log Service over the Internet.
NoteIn this example, HPA triggers scaling activities based on the sls_ingress_qps and sls_ingress_latency_p9999 metrics. In the target sections, each metric has a different type value:
The type value of the sls_ingress_qps metric is set to AverageValue. This indicates that the metric value is the result of dividing the total QPS by the number of pods.
The type value of the sls_ingress_latency_p9999 metric is set to Value. This indicates that the latency is not divided by the number of pods.
The two type values are commonly used in HPA configurations.
On the Templates page, select HPA and click Create Application.
On the HPA page, click Create.
After HPA is configured, run the following script to perform a stress test:
#!/bin/bash ##Use Apache Benchmark to send requests to the Service exposed by the Ingress. The test lasts 300 seconds and 10 concurrent requests are sent per second. ab -t 300 -c 10 <The domain name of the Ingress>
Check whether HPA works as expected.
In the left-side navigation pane of the ACK console, click Clusters. On the Clusters page, find the cluster that you want to manage and choose
in the Actions column.Run the following command to check the status of HPA:
kubectl get hpa ingress-hpa
Expected results:
NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAPS AGE ingress-hpa Depolyment/nginx-deployment-basic 21/10 (avg) 2 10 10 7m49s
If the value of REPLICAS is the same as the value of MAXPODS, it indicates that HPA scaled out the application as expected.
Use the CLI to query data
Run the following command to query data. In this example, the sls_ingress_qps metric is queried.
kubectl get --raw /apis/external.metrics.k8s.io/v1beta1/namespaces/*/sls_ingress_qps?labelSelector=sls.project={{SLS_Project}},sls.logstore=nginx-ingress
{{SLS_Project}} is the name of the Simple Log Service project used by the ACK cluster. The default name of the Simple Log Service project used by an ACK cluster is k8s-log-{{ClusterId}}. {{ClusterId}} is the ID of the cluster.
Expected output:
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"
}
If the preceding output is returned, the metric is invalid. No data is returned for the sls_alb_ingress_qps metric because no ALB Ingress is created.
FAQ
What do I do if the TARGETS column shows <unknow> after I run the
kubectl get hpa
command?Perform the following operations to troubleshoot the issue:
Run the
kubectl describe hpa <hpa_name>
command to check why HPA does not function as normal.If the value of AbleToScale is False in the Conditions field, check whether the Deployment is created as normal.
If the value of ScalingActive is False in the Conditions field, proceed to the next step.
Run the
kubectl get --raw "/apis/external.metrics.k8s.io/v1beta1/"
command. If Error from server (NotFound): the server could not find the requested resource is returned, verify the status of alibaba-cloud-metrics-adapter.If the status of alibaba-cloud-metrics-adapter is normal, check whether the HPA metrics are related to the Ingress. If the metrics are related to the Ingress, make sure that you deploy the Simple Log Service component before ack-alibaba-cloud-metrics-adapter is deployed. For more information, see Analyze and monitor the access log of nginx-ingress.
Make sure that the values of the HPA metrics are valid. The value of sls.ingress.route must be in the
<namespace>-<svc>-<port>
format.namespace specifies the namespace to which the Ingress belongs.
svc specifies the name of the Service that you selected when you created the Ingress.
port specifies the port of the Service.
Where can I find the metrics that are supported by HPA?
For more information about the metrics that are supported by HPA, see Alibaba Cloud metrics adapter. The following table describes the commonly used metrics.
Metric name
Description
Additional parameter
sls_ingress_qps
The number of requests that the Ingress can process per second based on a specific routing rule.
sls.ingress.route
sls_alb_ingress_qps
The number of requests that the ALB Ingress can process per second based on a specific routing rule.
sls.ingress.route
sls_ingress_latency_avg
The average latency of all requests.
sls.ingress.route
sls_ingress_latency_p50
The maximum latency for the fastest 50% of all requests.
sls.ingress.route
sls_ingress_latency_p95
The maximum latency for the fastest 95% of all requests.
sls.ingress.route
sls_ingress_latency_p99
The maximum latency for the fastest 99% of all requests.
sls.ingress.route
sls_ingress_latency_p9999
The maximum latency for the fastest 99.99% of all requests.
sls.ingress.route
sls_ingress_inflow
The inbound bandwidth of the Ingress.
sls.ingress.route
How do I collect NGINX Ingress logs in a custom format?
In this topic, horizontal pod autoscaling is performed based on the Ingress metrics that are collected by Simple Log Service. You must configure Simple Log Service to collect NGINX Ingress logs.
When you create an ACK cluster, Simple Log Service is enabled for the cluster by default. If you use the default log collection settings, you can view the log analysis reports and real-time status of NGINX Ingresses in the Simple Log Service console after you create the cluster.
If you disable Simple Log Service when you create an ACK cluster, you cannot perform horizontal pod autoscaling based on the Ingress metrics that are collected by Simple Log Service. You must enable Simple Log Service for the cluster before you can use this feature. For more information, see Analyze and monitor the access log of nginx-ingress-controller.
The AliyunLogConfig that is generated the first time you enable Simple Log Service applies only to the default log format that ACK defines for the Ingress controller. If you have changed the log format, you must modify the
processor_regex
settings in the AliyunLogConfig. For more information, see Use the Simple Log Service console to collect container text logs in DaemonSet mode.