Use CronHPA for scheduled pod scaling

更新时间:
复制 MD 格式

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.

View sample YAML and parameter descriptions

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  # Name of the target workload.
  excludeDates:                 # Dates to exclude scaling jobs (in Cron format).
  - "* * * 15 11 *"             # Excludes November 15.
  - "* * * * * 5"               # Excludes every Friday.
  jobs:
  - name: "scale-down"
    schedule: "30 */1 * * * *"  # Triggers at 30 seconds past every minute (for example, 08:00:30, 08:01:30).
    targetSize: 1               # The target replica count.
    runOnce: false              # Specifies whether the job runs only once. Default: false.
  - name: "scale-up"
    schedule: "0 */1 * * * *"  # Triggers at 0 seconds past every minute (for example, 08:00:00, 08:01:00).
    targetSize: 3              # The target replica count.
    runOnce: false             

The following table describes the parameters in a CronHPA configuration.

Parameter

Description

scaleTargetRef

Specifies the target object to scale. CronHPA can scale any Kubernetes resource that supports the scale subresource.

excludeDates

An array of dates. The task is skipped on the dates specified in excludeDates. The minimum unit is day.

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

If you do not want to run a task on November 15, you can specify excludeDates as shown in the following example.

excludeDates:
  - "* * * 15 11 *"

jobs

A list of CronHPA jobs that you can define in a single spec. Each job can be configured with the following fields:

  • name: The name of the CronHPA job.

  • schedule: A cron expression that defines when the job runs. Based on the go-cron library, the syntax is similar to the standard crontab but supports more flexible expressions. When you create a job, strictly follow the format described below.

    The format of a 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: The target replica count for the workload when the job runs.

  • runOnce: If you set this parameter to true, the task is executed only once and then exits.

Step 1: Install the CronHPA add-on

  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 Components and Add-ons.

  3. 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

  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 Workloads > Deployments.

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

  4. 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

  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 Workloads > Deployments.

  3. 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

Note

This page is available only to allowlisted users. To request access, submit a ticket.

  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 Workload Scaling.

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

  4. 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

  1. Create a file named cronhpa.yaml and 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         
  2. Run the following command to create a CronHPA job for an existing workload:

    kubectl apply -f cronhpa.yaml

    Expected 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

    1. On the Clusters page, click the name of your cluster. In the left navigation pane, click Workload Scaling.

    2. 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)

    1. On the Clusters page, click the name of your cluster. In the left navigation pane, click Workloads > Deployments.

    2. 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.