To improve resource utilization and reduce costs, Container Compute Service (ACS) supports cron-based scaling using the ack-kubernetes-cronhpa-controller component. This feature, called Cron Horizontal Pod Autoscaler (CronHPA), helps reduce resource waste. This topic describes how to implement cron-based container scaling in an ACS cluster and explains how CronHPA is compatible with the Horizontal Pod Autoscaler (HPA).
Prerequisites
You have created an ACS cluster.
Background information
The ack-kubernetes-cronhpa-controller is a Kubernetes HPA controller that scales cluster resources at scheduled times, similar to a crontab. You can use CronHPA for any Kubernetes object that supports the scale subresource, such as a deployment or a StatefulSet. For more information, see kubernetes-cronhpa-controller.
The following example explains the fields in a CronHPA configuration.
apiVersion: autoscaling.alibabacloud.com/v1beta1
kind: CronHorizontalPodAutoscaler
metadata:
labels:
controller-tools.k8s.io: "1.0"
name: cronhpa-sample
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-deployment-basic
excludeDates:
# exclude November 15th
- "* * * 15 11 *"
# exclude every Friday
- "* * * * * 5"
jobs:
- name: "scale-down"
schedule: "30 */1 * * * *"
targetSize: 1
- name: "scale-up"
schedule: "0 */1 * * * *"
targetSize: 3
runOnce: falseField | Description |
scaleTargetRef | The scaleTargetRef field specifies the object to scale. CronHPA supports any object that supports the Scale subresource. |
excludeDates | The excludeDates field is an array of dates. Tasks are skipped on dates that match an entry in excludeDates. Note The minimum unit is a day. The format For example, to prevent a task from running on November 15, specify excludeDates as shown in the following example. |
jobs | You can define multiple CronHPA tasks in a single
|
Install the CronHPA component
You can install the ack-kubernetes-cronhpa-controller component as follows.
Log on to the ACS console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of the target cluster. In the left navigation pane, click Add-ons.
On the Add-ons page, click the Application Management tab. Find ack-kubernetes-cronhpa-controller, and click Install. In the dialog box that appears, click OK.
If you no longer need cron-based container scaling, you can delete the CronHPA component. For more information about how to delete the ack-kubernetes-cronhpa-controller component, see Manage components.
Create a CronHPA scheduled task
Before you create and run a CronHPA for an application, make sure the CronHPA component in the cluster is running correctly and the application has only one HPA object. For more information about the compatibility policy between CronHPA and HPA, see CronHPA compatibility with HPA. You can create a CronHPA task in one of the following two ways.
Method 1: Create a CronHPA task when you create an application
When you create an application, go to the Advanced Configuration page. In the Scaling Configuration section, select Enable next to Cron-based Scaling to create a scheduled scaling task for the application. For more information about how to create an application, see Create a stateless workload (Deployment) or Create a stateful workload (StatefulSet).

The ACS console automatically checks if the CronHPA component is installed. If it is not installed, a Click to install message appears. After you install the CronHPA component, the configuration settings for the scheduled scaling task are displayed. The following table describes the parameters.
Configuration Item | Description |
Scheduled Task Name | Define a name for the scheduled task. The name of each task must be unique. |
Target Replicas | The number of application replicas automatically scales to this value at the scheduled time. |
Scheduling Cycle | Set the scheduling cycle. For more information about how to set the scheduling cycle for a scheduled scaling task, see predefined-schedules. |
Method 2: Create a CronHPA task for an existing application
The following steps show how to create a CronHPA task for an existing stateless application.
Log on to the ACS console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Workloads > Deployments.
On the Stateless page, click Details in the Actions column for the application.
Click the Container Scaling tab and configure the scheduled scaling task.
If the CronHPA component is not installed, a Click to install message appears. Click Click to install and then proceed with the following steps.
If the CronHPA component is already installed, proceed directly with the following steps.
Click Create next to Cron-based Scaling (CronHPA). In the Create dialog box, set the parameters for the scheduled scaling task.

The following table describes the configuration items.
Configuration Item
Description
Task Name
Define a name for the scheduled task. The name of each task must be unique.
Target Replicas
The number of application replicas automatically scales to this value at the scheduled time.
Scheduling Cycle
Set the scheduling cycle. For more information about how to set the scheduling cycle for a scheduled scaling task, see predefined-schedules.
Add or modify a scheduled scaling task
Follow the steps in Create a CronHPA scheduled task to go to the Container Scaling page.
On the Container Scaling tab, in the Cron-based Scaling (CronHPA) section, find the target task and click Add or Edit Task in the Actions column.
In the Edit dialog box, click Add Task to create a task, or click an existing CronHPA task to modify it. Then, click OK.
NoteYou can delete a CronHPA task as shown in the following figure. In the Edit dialog box, click the delete icon in the upper-right corner of the task name, and then click OK.

CronHPA and HPA definition templates
CronHPA
apiVersion: autoscaling.alibabacloud.com/v1beta1
kind: CronHorizontalPodAutoscaler
metadata:
labels:
controller-tools.k8s.io: "1.0"
name: cronhpa-sample
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-deployment-basic
jobs:
- name: "scale-down"
schedule: "30 */1 * * * *"
targetSize: 1
- name: "scale-up"
schedule: "0 */1 * * * *"
targetSize: 11
HPA
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx-deployment-basic-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-deployment-basic
minReplicas: 4
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
CronHPA compatibility with HPA
Both CronHPA and HPA act on workloads, such as deployments or StatefulSets. Configuring both on the same workload can cause conflicts. To solve this problem, CronHPA supports targeting an HPA as its scaling object, which makes it compatible with the original functionality of HPA.
Comparing the CronHPA and HPA definition templates shows the following:
Both CronHPA and HPA use the
spec.scaleTargetReffield to obtain the scaling object.CronHPA uses the crontab rule in
spec.jobsto scale the number of replicas at scheduled times.HPA determines scaling based on resource utilization.
If you set both CronHPA and HPA, they might try to operate on the same scaleTargetRef at the same time. Because CronHPA and HPA are independent and unaware of each other, the two controllers would work separately, and the later operation would overwrite the earlier one.
Solution for CronHPA and HPA compatibility
The conflict can be resolved if CronHPA is aware of the current state of HPA. To achieve this, ACS sets the scaleTargetRef in CronHPA to the HPA object itself, not the final workload object. When CronHPA performs a scaling operation, it first finds the real scaleTargetRef through the HPA object. It then adjusts the workload object based on this state. This prevents overwrites and ensures that scaling operations are consistent and predictable.
The following is a template for a CronHPA definition that is compatible with HPA.
apiVersion: autoscaling.alibabacloud.com/v1beta1
kind: CronHorizontalPodAutoscaler
metadata:
labels:
controller-tools.k8s.io: "1.0"
name: cronhpa-sample
spec:
scaleTargetRef:
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
name: nginx-deployment-basic-hpa
jobs:
- name: "scale-down"
schedule: "30 */1 * * * *"
targetSize: 1
runOnce: false
- name: "scale-up"
schedule: "0 */1 * * * *"
targetSize: 3
runOnce: falseWith the CronHPA definition above, CronHPA can read the values of spec.minReplicas, spec.maxReplicas, and status.desiredReplicas from the HPA. It also knows the current number of replicas for the spec.scaleTargetRef in the HPA. CronHPA interacts with the HPA by adjusting it. CronHPA compares its target number of replicas with the current number of replicas and takes the larger value to decide whether to scale out or modify the HPA's upper limit. CronHPA compares its target number of replicas with the HPA's configuration and takes the smaller value to decide whether to modify the HPA's lower limit.
The following table explains the rules for CronHPA and HPA compatibility in different scenarios.
HPA (min/max) | CronHPA | Deployment | Scaling Result | Compatibility Rule Description |
1/10 | 5 | 5 |
| When the CronHPA target replica count is the same as the current replica count, the HPA's min/max replicas and the application's current replica count do not change. |
1/10 | 4 | 5 |
| When the CronHPA target replica count is lower than the current replica count, the current replica count is maintained. |
1/10 | 6 | 5 |
|
|
5/10 | 4 | 5 |
|
|
5/10 | 11 | 5 |
|
|
The following table describes the parameters.
HPA (min/max): The minimum and maximum number of replicas defined in the HPA.
CronHPA: The number of replicas set by CronHPA.
Deployment: The number of replicas before the application is scaled.
As shown above, CronHPA does not directly adjust the number of replicas in a deployment. Instead, it operates on the deployment through the HPA. This avoids conflicts between HPA and CronHPA.