All Products
Search
Document Center

Container Service for Kubernetes:Use CronHPA for scheduled horizontal scaling

Last Updated:Jul 15, 2025

If your application resource utilization exhibits periodic changes and you need to scale pods based on Crontab-like schedules, you can use Cron Horizontal Pod Autoscaler (CronHPA) to achieve scheduled scaling. CronHPA is suitable for scenarios where business traffic has significant peak periods or scheduled tasks.

Pre-reading tips

ACK clusters offer various workload scaling (scheduling layer elasticity) and node scaling (resource layer elasticity) solutions. We recommend that you read Auto scaling before using this topic to understand the applicable scenarios and limits of different solutions.

CronHPA template and parameters

CronHPA is implemented based on kubernetes-cronhpa-controller. The kubernetes-cronhpa-controller is a time-based pod horizontal scaling controller that uses Crontab-like syntax to configure scheduled scaling policies, enabling dynamic resource adjustment in scenarios with cyclical traffic patterns. CronHPA can be applied to any Kubernetes object that supports scaling subresources (Scale Subresource), such as Deployment and StatefulSet.

Expand to view examples and parameters

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  # Associated workload name
  excludeDates:                 # Task exclusion dates (Cron format), scaling will not be performed on these dates
  - "* * * 15 11 *"             # Exclude November 15th
  - "* * * * * 5"               # Exclude Friday
  jobs:
  - name: "scale-down"
    schedule: "30 */1 * * * *"  # Triggered at the 30th second of every minute (e.g., 08:00:30, 08:01:30)
    targetSize: 1               # Target number of replicas
    runOnce: false              # Task execution mode (whether to run only once), default is false
  - name: "scale-up"
    schedule: "0 */1 * * * *"  # Triggered at the 0th second of every minute (e.g., 08:00:00, 08:01:00)
    targetSize: 3              # Target number of replicas
    runOnce: false             

The following table describes the parameters in CronHPA configuration.

Parameter

Description

scaleTargetRef

Specifies the object to be scaled. If the object supports scaling subresources (Scale Subresource), CronHPA can support it.

excludeDates

Date array. Tasks are skipped on dates that match the excludeDates description. Unit: days.

"* * * * * *" represents "<Seconds> <Minutes> <Hours> <Day of month> <Month> <Day of week>".

If you want to exclude tasks on November 15th, specify excludeDates as shown in the following example.

excludeDates:
  - "* * * 15 11 *"

jobs

You can set multiple CronHPA jobs in the spec section. Each CronHPA job can be configured with the following fields:

  • name: The name of the CronHPA job.

  • schedule: The kubernetes-cronhpa-controller (based on the go-cron library) syntax is similar to standard Crontab but supports more flexible expressions. Set it strictly according to the format description below.

    The format of the Cron expression is as follows:

    Field name   | Mandatory? | Allowed values  | Allowed special characters
      ----------   | ---------- | --------------  | --------------------------
      Seconds      | Yes        | 0-59            | * / , -
      Minutes      | Yes        | 0-59            | * / , -
      Hours        | Yes        | 0-23            | * / , -
      Day of month | Yes        | 1-31            | * / , - ?
      Month        | Yes        | 1-12 or JAN-DEC | * / , -
      Day of week  | Yes        | 0-6 or SUN-SAT  | * / , - ?
  • targetSize: Sets the number of pods to scale during scheduling.

  • runOnce: When set to true, the task executes only once and exits after the first execution.

Step 1: Install the CronHPA component

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

  2. On the Clusters page, find the one you want to manage and click its name. In the navigation pane on the left, click Add-ons.

  3. Click the Manage Applications tab, find ack-kubernetes-cronhpa-controller, and follow the page prompts to complete the component installation.

Step 2: Create CronHPA jobs

Before creating CronHPA jobs for an application, make sure that the CronHPA component in the cluster is running normally and that there is only one HPA task object for the current application. The following example shows how to enable CronHPA jobs for a stateless application. The steps for other types of workloads are similar.

Create CronHPA jobs when creating an application

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

  2. On the Clusters page, find the cluster you want to manage and click its name. In the left-side pane, choose Workloads > Deployments.

  3. On the Deployments page, click Create From Image.

  4. On the Create page, follow the page prompts to complete the basic application information configuration, container configuration, service configuration, and scheduled scaling configuration to create a Deployment that supports CronHPA.

    For more information about steps and parameters, see Create a stateless application using a Deployment. The following list describes the key parameters.

    • Basic Information: Configure the basic information of the application, such as the name and the number of replicas.

    • Container: Configure the image, CPU, and memory resources required by the container.

    • Advanced:

      • In the Access Control section, click Service corresponding to Create to configure the service.

      • In the Scaling section, check CronHPA for Enable, follow the page prompts to install the component (ignore if already installed), and then configure the conditions and parameters for scheduled scaling.

        • Job Name: The name of the CronHPA job.

        • Desired Number Of Replicas: When the scheduled time is reached, the number of application replicas automatically scales to this value.

        • Scaling Schedule: Set the scheduling period. For more information, see Kubernetes CronHPA Controller.

Create CronHPA jobs for an existing application

Create on the Workloads page

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

  2. On the Clusters page, find the cluster you want to manage and click its name. In the left-side pane, choose Workloads > Deployments.

  3. Click the target workload, and then click the Pod Scaling tab. In the CronHPA section, click Create to configure CronHPA.

    create

    Parameter

    Description

    Job Name

    The name of the CronHPA job.

    Desired Number Of Replicas

    When the scheduled time is reached, the number of application replicas automatically scales to this value.

    Scaling Schedule

    The set scheduling period. For more information about setting the scheduling period for CronHPA jobs, see AliyunContainerService/kubernetes-cronhpa-controller.

Create on the Workload Scaling page

Note

This page is currently available only for users in whitelists. To use this feature, submit a ticket.

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

  2. On the Clusters page, find the cluster you want and click its name. In the left-side pane, click Workload Scaling.

  3. In the upper-right corner of the page, click Create Auto Scaling.

  4. Click the HPA And CronHPA tab, then Select The Target Workload. In the Configure Scaling Policy section, check CronHPA.

    You can add multiple CronHPA jobs. Follow the page prompts to configure the CronHPA policy information, and then click OK.

    • CronHPA Name: Enter the name of CronHPA.

    • Job Name: Enter the name of the CronHPA job.

    • Desired Number Of Replicas: Set the target number of replicas when the scheduled time is reached.

    • Scaling Schedule: Set the scheduling period. For more information, see Kubernetes CronHPA Controller.

Use kubectl

  1. Create and copy the following content to the cronhpa.yaml file.

    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         
  2. Run the following command to create a CronHPA job for an existing workload.

    kubectl apply -f cronhpa.yaml

    Expected results:

    cronhorizontalpodautoscaler.autoscaling.alibabacloud.com/cronhpa-sample created

Related operations

View, add, or modify CronHPA jobs

After creating CronHPA jobs, you can view the status and list of CronHPA jobs in the CronHPA list. You can add a CronHPA job or modify the existing CronHPA configuration through the following entry points.

  • Workload Scaling entry point

    1. On the Clusters page, find the cluster you want and click its name. In the left-side pane, click Workload Scaling.

    2. Click the CronHPA tab, and then click Edit in the Actions column of the target CronHPA job.

  • Workloads entry point (using a stateless application as an example, the steps for other types of workloads are similar)

    1. On the Clusters page, find the cluster you want to manage and click its name. In the left-side pane, choose Workloads > Deployments.

    2. On the Deployments page, click Details in the Actions column of the target Deployment. Then click the Pod Scaling tab. In the CronHPA section, click Add Or Edit Job in the Actions column.

Implement coordination between CronHPA and HPA

Cron Horizontal Pod Autoscaler (HPA) and Horizontal Pod Autoscaler (HPA) are unaware of each other. If both CronHPA and HPA are configured for your application, CronHPA and HPA scale your application separately. In this case, the later scaling activity overwrites the earlier one. To resolve this issue, Container Service for Kubernetes (ACK) provides a solution for CronHPA to interact with HPA without conflicts. If ACK detects that both CronHPA and HPA are deployed, ACK sets HPA as the scaling object of CronHPA. CronHPA triggers HPA to scale the object scaled by HPA, such as a Deployment, at the scheduled time.

For more information, see Implement Coordination Between CronHPA and HPA.