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.
Step 1: Install the CronHPA component
Log on to the ACK console. In the navigation pane on the left, click Clusters.
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.
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
Log on to the ACK console. In the navigation pane on the left, click Clusters.
On the Clusters page, find the cluster you want to manage and click its name. In the left-side pane, choose .
On the Deployments page, click Create From Image.
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
Log on to the ACK console. In the navigation pane on the left, click Clusters.
On the Clusters page, find the cluster you want to manage and click its name. In the left-side pane, choose .
Click the target workload, and then click the Pod Scaling tab. In the CronHPA section, click Create to configure CronHPA.
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
This page is currently available only for users in whitelists. To use this feature, submit a ticket.
Log on to the ACK console. In the navigation pane on the left, click Clusters.
On the Clusters page, find the cluster you want and click its name. In the left-side pane, click Workload Scaling.
In the upper-right corner of the page, click Create Auto Scaling.
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
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
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
On the Clusters page, find the cluster you want and click its name. In the left-side pane, click Workload Scaling.
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)
On the Clusters page, find the cluster you want to manage and click its name. In the left-side pane, choose .
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.