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 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 workload (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 replica count to which the application scales 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, and then select the checkbox for CronHPA in the Configure Auto Scaling Policy section.
You can 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 replica count 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 -
Run the following command to 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)
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 operate independently. If you configure both for the same application, their actions can conflict, causing a later scaling action to overwrite an earlier one. To resolve this, ACK provides a compatibility solution. When both are detected for a workload, ACK treats the HPA object as the scaling target for CronHPA. This allows CronHPA to perform scheduled scaling on the object defined by the HPA, such as a Deployment.
For more information, see Make CronHPA compatible with HPA.