All Products
Search
Document Center

Container Service for Kubernetes:Use CronHPA for scheduled horizontal scaling

Last Updated:Aug 14, 2024

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 applications need to perform tasks at specific times.

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 overview before using this topic to understand the applicable scenarios and usage limitations of different solutions.

Prerequisites

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 scales the cluster based on crontab-like schedules, such as automatically increasing the number of pods during business peak hours on weekdays and decreasing the number of pods during business off-peak hours. CronHPA can be applied to any Kubernetes object that supports scaling subresources, such as Deployment and StatefulSet.

Expand to view CronHPA YAML example 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
   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: false

Parameter

Description

scaleTargetRef

Specifies the object to be scaled. If the object supports Scale subresources, 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

Supports setting multiple CronHPA tasks in one spec. Each CronHPA task has the following parameters:

  • name: It should be unique within a CronHPA, allowing different tasks to be distinguished by name.

  • schedule: The policy is similar to Crontab. The kubernetes-cronhpa-controller uses the Golang library (go-cron) which supports more expression rules. Set it strictly according to the format description below. Otherwise, creation fails.

    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

You can install the CronHPA component ack-kubernetes-cronhpa-controller to achieve scheduled scaling of application workloads.

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

  2. On the Clusters page, find the cluster that you want to manage and click its name. In the left-side navigation pane, choose Operations > Add-ons.

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

Step 2: Create CronHPA Task

Before creating and running CronHPA 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 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 left-side navigation pane, click Clusters.

  2. On the Clusters page, find the cluster that 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 by using a Deployment. The following only introduces the main 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 Create corresponding to Service to configure the service.

      • In the Scaling section, check Enable for CronHPA, 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 task.

        • 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 about setting the scheduling period for CronHPA jobs, see AliyunContainerService/kubernetes-cronhpa-controller.

Create CronHPA jobs for an existing application

Create on the Workloads page

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

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

  3. On the Deployments page, click Details in the Actions column of the target deployment, and then click the Pod Scaling tab. In the CronHPA section, follow the page prompts to install the CronHPA component (ignore if already installed) and 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

The Workload Scaling page is available for only users in whitelists. If you want to use this page, submit a ticket.

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

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

  3. In the upper right corner of the page, click Create Auto Scaling, select the target workload, and then check the CronHPA option under the Horizontal Scaling tab. Follow the page prompts to install the component (ignore if already installed) and configure the CronHPA policy.

    • CronHPA Name: Enter the name of CronHPA.

    • Job Name: Enter 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 about setting the scheduling period for CronHPA tasks, see AliyunContainerService/kubernetes-cronhpa-controller.

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 that you want to manage and click its name. In the left-side pane, choose Auto Scaling > Workload Scaling.

    2. In the CronHPA section under the Horizontal Scaling tab, click Edit in the Actions column.

  • 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 that you want to manage and click its name. In the left-side pane, choose Workloads > Deployments.

    2. In the Deployments page, click Details in the Actions column of the target deployment, and 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

CronHPA and HPA are independent and unaware of each other. As a result, if both CronHPA and HPA are configured in the YAML file of your application, the CronHPA controller and the HPA controller scale for the application separately. 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, it sets the scaling target of CronHPA to HPA, thereby enabling scheduled scaling for the HPA scaling object, such as a Deployment.

For more information, refer to Implement Coordination Between CronHPA and HPA.

FAQ

How do I uninstall ack-kubernetes-cronhpa-controller?

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

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

  3. On the Helm page, check whether the ack-kubernetes-cronhpa-controller component exists.

    • If it exists, click Delete in the Actions column on the right side of the component.

      Important

      Deleting the component via Helm will not delete the CRD and created Task resources. To completely delete CronHPA-related resources, delete them manually.

    • If it does not exist, go to the next step.

  4. In the left-side navigation pane of the details page, choose Operations > Add-ons.

  5. On the Add-ons page, find the ack-kubernetes-cronhpa-controller component and click Uninstall.