All Products
Search
Document Center

Container Service for Kubernetes:Use Horizontal Pod Autoscaler (HPA) in Knative to autoscale workloads based on CPU and memory

Last Updated:Oct 14, 2026

Combining Knative with Horizontal Pod Autoscaler (HPA) gives your applications autoscaling based on resource load. Knative already scales based on request volume, but adding the HPA autoscaling extension lets you further fine-tune scaling behavior based on more metrics, such as CPU utilization and memory utilization.

Prerequisites

Step 1: Deploy a Knative Service

  1. Log on to the ACK console. In the left navigation pane, click Clusters.

  2. On the Clusters page, click the name of your cluster. In the left navigation pane, click Applications > Knative.

  3. On the Knative page, click the Services tab, set Namespace to default, click Create from Template, paste the following YAML sample into the template, and then click Create.

    The following sample configuration creates a Service named helloworld-go-hpa.

    apiVersion: serving.knative.dev/v1
    kind: Service
    metadata:
      name: helloworld-go-hpa # Knative Service name.
    spec:
      template:
        metadata:
          labels:
            app: helloworld-go-hpa
          annotations:
            autoscaling.knative.dev/class: "hpa.autoscaling.knative.dev" # Specifies HPA as the autoscaling extension.
            autoscaling.knative.dev/metric: "cpu" # HPA scaling metric. Valid values: cpu and memory. This example uses cpu.
            autoscaling.knative.dev/target: "30" # The threshold for the HPA CPU metric. Knative HPA adjusts the number of replicas based on this threshold.
            autoscaling.knative.dev/minScale: "1" # The minimum number of instances for the elastic policy.
            autoscaling.knative.dev/maxScale: "4" # The maximum number of instances for the elastic policy.
        spec:
          containers:
            - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/autoscale-go:v1024
              resources:
                requests:
                  cpu: '200m'
  4. Run the following command to check whether the Service is running properly.

    kubectl get ksvc

    Expected output:

    NAME                   URL                                               LATESTCREATED                LATESTREADY                  READY   REASON
    helloworld-go-hpa      http://helloworld-go-hpa.default.example.com      helloworld-go-hpa-00001      helloworld-go-hpa-00001      True        

    READY column shows True, which indicates that the Knative Service is running properly.

Step 2: Enable CPU-based autoscaling

  1. Install the Hey load testing tool.

    For more information about the Hey load testing tool, see Hey.

  2. Run the following command to perform a load test by sending requests to the Service at 100 queries per second (QPS) for 60 seconds.

    Note

    Replace 121.XX.XX.10 with the IP address or domain name of the gateway.

    hey -z 60s -q 100   -host "helloworld-go-hpa.default.example.com"   "http://121.XX.XX.10?prime=40000000" # 121.199.XXX.XXX is the IP address or domain name of the gateway.

    While the load test is running, run the following command to view Pod scaling in real time.

    kubectl get pods --watch

    Expected Pod scaling output:

    NAME                                                     READY   STATUS    RESTARTS   AGE
    # The Pod is running and its containers are ready.
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxfl5      2/2     Running   0          101m
    # Pod count scales out to 4, and the status is 0/2 Pending, which means that the containers are waiting to be scheduled and allocated resources.
    helloworld-go-hpa-00001-deployment-67cc8f979b-kv6rj      0/2     Pending   0          0s
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxq85      0/2     Pending   0          0s
    helloworld-go-hpa-00001-deployment-67cc8f979b-kv6rj      0/2     Pending   0          0s
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxq85      0/2     Pending   0          0s
    # 4 Pods change to a status of 0/2 ContainerCreating, which indicates that the containers are being created.
    helloworld-go-hpa-00001-deployment-67cc8f979b-kv6rj      0/2     ContainerCreating   0          0s
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxq85      0/2     ContainerCreating   0          0s
    helloworld-go-hpa-00001-deployment-67cc8f979b-kv6rj      0/2     ContainerCreating   0          0s
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxq85      0/2     ContainerCreating   0          0s
    # 4 Pods change to statuses of 1/2 Running and 2/2 Running, which indicates that one or both containers are created and running.
    helloworld-go-hpa-00001-deployment-67cc8f979b-kv6rj      1/2     Running             0          1s
    helloworld-go-hpa-00001-deployment-67cc8f979b-kv6rj      2/2     Running             0          1s
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxq85      1/2     Running             0          1s
    helloworld-go-hpa-00001-deployment-67cc8f979b-fxq85      2/2     Running             0          1s

    The output shows that Knative can scale automatically based on requests. When the system load increases and more Pods are required to handle requests, the number of Pods scales out from the initial 1 to 4 to increase the processing capacity and throughput of the system.

(Optional) Step 3: View the Knative monitoring dashboard

Knative provides out-of-the-box observability. On the Knative page, click the Monitoring Dashboards tab to view the monitoring data of the Service. For more information about the monitoring dashboard, see View the Knative service monitoring dashboard.

image.png

References

  • Alibaba Cloud Knative implements Serverless standards by integrating container building (or functions), workload management (dynamic scaling), and an event model. For more information about Alibaba Cloud Knative, see Knative.

  • To learn more about the features of Alibaba Cloud Knative, see Comparison of Alibaba Cloud Knative and open-source Knative.