Use CronHPA for scheduled pod scaling
If your application's resource usage follows a predictable, recurring pattern, you can use CronHPA (Cron Horizontal Pod Autoscaler) to scale pods on a schedule. CronHPA is ideal for scenarios with predictable traffic peaks or for running scheduled tasks.
Before you begin
Container Service for Kubernetes (ACK) provides multiple solutions for workload scaling (at the scheduling layer) and node scaling (at the resource layer). Before you start, read auto scaling to understand the use cases and limitations of each solution.
CronHPA template and parameters
CronHPA is implemented by kubernetes-cronhpa-controller, a time-based pod autoscaler. It uses cron-like syntax to configure scheduled scaling policies, letting you dynamically adjust resources for workloads with periodic traffic fluctuations. CronHPA can be applied to any Kubernetes object that supports the scale subresource, such as Deployments and StatefulSets.
Step 1: Install the CronHPA add-on
-
Log on to the ACK console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of your cluster. In the left navigation pane, click Components and Add-ons .
-
Click the Application Management tab, find ack-kubernetes-cronhpa-controller, and follow the on-screen instructions to install the add-on.
Step 2: Create a CronHPA job
Before you create a CronHPA job, make sure that the CronHPA add-on is running in your cluster and that the target application has only one Horizontal Pod Autoscaler (HPA) object. The following example shows how to create a CronHPA job for a stateless application. The procedure is similar for other types of workloads.
When creating an application
-
Log on to the ACK console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
On the Deployments page, click Create from Image.
-
On the Create page, configure the basic information, container, service, and scheduled scaling settings to create a Deployment that supports CronHPA.
For detailed instructions and parameter descriptions, see Create a stateless Deployment. The key parameters are described below.
-
Basic Information: Configure the application name and replica count.
-
Container: Configure the image and specify the required CPU and memory resources for the container.
-
Advanced:
-
In the Scaling section, select the Enable checkbox for CronHPA. If the add-on is not installed, follow the prompts to install it. Then, configure the scheduled scaling conditions and parameters.
-
Scheduled Task Name: The name of the CronHPA job.
-
Desired Number of Replicas: The target number of replicas for the application at the scheduled time.
-
Scaling Schedule: The schedule for the scaling job. For more information, see Kubernetes CronHPA Controller.
-
-
-
For existing applications
From the Workloads page
-
Log on to the ACK console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
Click the name of the target workload, and then click the Pod Scaling tab. In the CronHPA section, click Create and configure the CronHPA job.
Parameter
Description
Job Name
The name of the CronHPA job.
Desired Number of Replicas
The target number of replicas for the application at the scheduled time.
Scaling Schedule
The schedule for the scaling job. For more information about how to set a schedule for a CronHPA job, see AliyunContainerService/kubernetes-cronhpa-controller.
Workload Scaling page
This page is available only to allowlisted users. To request access, submit a ticket.
-
Log on to the ACK console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of your cluster. In the left navigation pane, click Workload Scaling.
-
In the upper-right corner of the page, click Create Auto Scaling.
-
Click the HPA and CronHPA tab. Select Workload. Then, in the Configure Auto Scaling Policy section, select the CronHPA checkbox.
Optionally, add multiple CronHPA jobs. Configure the CronHPA policy as prompted and click OK.
-
CronHPA Task Name: A custom name for the CronHPA object.
-
Job Name: A custom name for the CronHPA job.
-
Desired Number of Replicas: The target number of replicas for the application at the scheduled time.
-
Scaling Schedule: The schedule for the scaling job. For more information, see Kubernetes CronHPA Controller.
-
Using kubectl
-
Create a file named
cronhpa.yamland copy the following content into it:apiVersion: autoscaling.alibabacloud.com/v1beta1 kind: CronHorizontalPodAutoscaler metadata: name: cronhpa-sample namespace: default spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: nginx-deployment-basic excludeDates: - "* * * 15 11 *" - "* * * * * 5" jobs: - name: "scale-down" schedule: "30 */1 * * * *" targetSize: 1 runOnce: false - name: "scale-up" schedule: "0 */1 * * * *" targetSize: 3 runOnce: true -
Create a CronHPA job for an existing workload:
kubectl apply -f cronhpa.yamlExpected output:
cronhorizontalpodautoscaler.autoscaling.alibabacloud.com/cronhpa-sample created
Related operations
Manage CronHPA jobs
After you create a CronHPA job, you can view its status in the CronHPA list. To add a new job or modify an existing one, use one of the following methods:
-
From the Workload Scaling page
-
On the Clusters page, click the name of your cluster. In the left navigation pane, click Workload Scaling.
-
Click the CronHPA tab. In the Actions column for the target CronHPA job, click Edit.
-
-
From the Workloads page (using a Deployment as an example; the steps are similar for other workload types)
-
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
On the Deployments page, click Details for the target Deployment in the Actions column, and then click the Pod Scaling tab. In the Actions column of the CronHPA section, click Add or Modify Job.
-
Use CronHPA with HPA
CronHPA and HPA cannot detect each other. If you configure both CronHPA and HPA for an application by using YAML, the two configurations work independently, and the operation that runs later overwrites the result of the one that ran earlier. To resolve this issue, ACK provides a compatibility solution for CronHPA and HPA: if both are configured, ACK uses the HPA as the scaling target of CronHPA. This way, the object defined in the HPA, such as a Deployment, is scaled on schedule.
For more information, see Make CronHPA compatible with HPA.