As the traffic entry point, an MSE gateway provides the most accurate view of your application's traffic volume. Use the queries per second (QPS) metrics from the gateway to enable auto scaling for applications in Container Service for Kubernetes (ACK). This allows your backend services to scale on demand. This topic shows you how to configure auto scaling for ACK applications by using gateway access logs.
Prerequisites
Procedure
-
Deploy the following resources in your ACK cluster.
Log on to the ACK console. In the left navigation pane, click .
-
Search for ack-alibaba-cloud-metrics-adapter and click its application card. On the details page, click Deploy. In the Deploy panel, configure the parameters and click OK.
Set the following parameters: select the target cluster, set the namespace to
kube-system, and set the release name toack-alibaba-cloud-metrics-adapter. -
In the navigation pane on the left, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
On the Deployments page, click Create Resources in YAML. Select Custom from the template list, paste the following YAML, and then click Create.
apiVersion: apps/v1 kind: Deployment metadata: name: httpbin-deploy labels: app: httpbin-deploy spec: replicas: 1 selector: matchLabels: app: httpbin template: metadata: labels: app: httpbin spec: containers: - image: kennethreitz/httpbin imagePullPolicy: IfNotPresent name: httpbin ports: - name: http containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: httpbin-svc namespace: default labels: app: httpbin-svc spec: ports: - port: 8080 name: http protocol: TCP targetPort: 80 selector: app: httpbin type: ClusterIP
-
In the MSE gateway, add a service source and create a service.
-
Log on to the MSE console. In the top navigation bar, select a region.
-
In the left-side navigation pane, choose Cloud-native Gateway > Gateways. On the Gateways page, click the ID of the gateway.
-
In the left-side navigation pane, click Routes. Then, click the Source tab.
-
Click Add Source. For Source Type, select Container Service, choose the ACK cluster where your application is deployed, and then click OK.
-
In the left-side navigation pane, click Routes. Then, click the Services tab.
-
Click Add Service to add the service source to the Services, and then click OK.
-
-
Create a routing rule for the service that you added. For more information, see Create a routing rule.
-
Enable log shipping.
-
In the left-side navigation pane, click Parameter Settings.
-
In the Observability Parameters section, click the settings icon
next to Log Shipping. In the dialog box that appears, enable log shipping and the Compatible with NGINX Ingress option.Leave Simple Log Service (SLS) Project set to Use Default, and then click OK.
-
-
In your ACK cluster, create a HorizontalPodAutoscaler (HPA) and configure its settings.
You need to specify the backend application to scale, the minimum and maximum replica count, and the scaling metrics. The following example configuration scales out the application when the average QPS per pod exceeds 10. In this example,
sls.projectis the log shipping project name, which can be found on the Parameter Settings page in the gateway console. Thesls.ingress.routeis formatted as<namespace>-<service-name>-<service-port>.apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: higress-hpa spec: scaleTargetRef: apiVersion: apps/v1beta2 kind: Deployment name: httpbin-deploy minReplicas: 1 maxReplicas: 10 metrics: - type: External external: metric: name: sls_ingress_qps selector: matchLabels: sls.project: "aliyun-product-data-xxxxxxxxxxxxx-cn-hangzhou" sls.logstore: "nginx-ingress" sls.ingress.route: "default-httpbin-svc-8080" target: type: AverageValue averageValue: 10 -
Run a load test against the application by routing traffic with a controlled QPS through the gateway. Then, observe the HPA events to verify the scaling behavior.
Run the following command:
kubectl describe hpa higress-hpaThe output is similar to the following:
Normal SuccessfulRescale 9m horizontal-pod-autoscaler New size: 3; reason: external metric sls_ingress_qps(&LabelSelector{MatchLabels:map[string]string{sls.ingress.route: default-httpbin-svc-8080,sls.logstore: nginx-ingress,sls.project: aliyun-product-data-xxxxxxxxxxxxxxxx-cn-hangzhou,},MatchExpressions:[]LabelSelectorRequirement{},}) above target Normal SuccessfulRescale 8m45s horizontal-pod-autoscaler New size: 4; reason: external metric sls_ingress_qps(&LabelSelector{MatchLabels:map[string]string{sls.ingress.route: default-httpbin-svc-8080,sls.logstore: nginx-ingress,sls.project: aliyun-product-data-xxxxxxxxxxxxxxxx-cn-hangzhou,},MatchExpressions:[]LabelSelectorRequirement{},}) above target Normal SuccessfulRescale 5m12s horizontal-pod-autoscaler New size: 4; reason: Normal SuccessfulRescale 3m41s horizontal-pod-autoscaler New size: 3; reason: All metrics below target Normal SuccessfulRescale 2m55s horizontal-pod-autoscaler New size: 1; reason: All metrics below target