To avoid resource wasting in some scenarios, Container Service for Kubernetes (ACK) provides the kubernetes-cronhpa-controller component to automatically scale resources based on predefined schedules. This topic describes how to use Cron Horizontal Pod Autoscaler (CronHPA) to scale your workloads based on a schedule. This topic also describes how to enable CronHPA and Horizontal Pod Autoscaler (HPA) to interact without conflicts.
Prerequisites
- An ACK cluster is created. For more information, see Create an ACK managed cluster.
- Helm 2.11.0 or a later version is installed on your on-premises machine. For more information, see Install Helm.
- A kubectl client is connected to your cluster. For more information, see Use kubectl to connect to an ASK cluster.
Background information
kubernetes-cronhpa-controller is a Kubernetes HPA controller that can scale pods in an ACK cluster based on a schedule that is similar to a crontab. You can use CronHPA with Kubernetes objects whose subresources can be scaled. The objects include Deployments and StatefulSets. In addition, the subresources must be open source projects on GitHub. For more information, see kubernetes-cronhpa-controller.
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: true
Parameter | Description |
---|---|
scaleTargetRef | scaleTargetRef specifies the object that you want to scale. If the subresources of the object can be scaled, you can enable CronHPA for the object. |
excludeDates | The value of excludeDates must be an array of dates. Scaling jobs are not executed on dates that are specified
in excludeDates.
Note The minimum time period is one day.
The value is in the
|
jobs | You can set multiple CronHPA jobs in the spec section. You can set the following parameters
for each CronHPA job:
|
Install the CronHPA controller
You can install the CronHPA controller ack-kubernetes-cronhpa-controller by using one of the following methods.
Method 1: Install the CronHPA controller on the Add-ons page in the ACK console
Method 2: Install the CronHPA controller from App Catalog
- Log on to the ACK console.
- In the left-side navigation pane of the ACK console, choose .
- On the Marketplace page, click the App Catalog tab and then find and click ack-kubernetes-cronhpa-controller.
- On the ack-kubernetes-cronhpa-controller page, click Deploy.
- In the Deploy wizard, select a cluster and a namespace, and then click Next.
- On the Parameters wizard page, configure the required parameters and click OK.
You can uninstall the CronHPA controller if CronHPA is no longer used. For more information about how to uninstall ack-kubernetes-cronhpa-controller, see Manage system components or Delete a release.
Create CronHPA jobs
Before you create and run CronHPA jobs for your application, make sure that the CronHPA controller runs as normal in your cluster and only one HPA task is created for your application. For more information about how to enable CronHPA and HPA to interact without conflicts, see Enable CronHPA and HPA to interact without conflicts. You can create CronHPA jobs in the following scenarios.
Scenario 1: Create CronHPA jobs when you create an application

Parameter | Description |
---|---|
Job Name | Enter a name for the CronHPA job. The name of each CronHPA job must be unique. |
Desired Number of Replicas | Replicated pods are scaled to the desired number at the scheduled time. |
Scaling Schedule | Set the scaling schedule.
For more information about how to set the scaling schedule for a CronHPA job, see AliyunContainerService/kubernetes-cronhpa-controller. |
Scenario 2: Create CronHPA jobs for an existing application
The following example demonstrates how to create CronHPA jobs for an existing application. A stateless application is used in this example.
Create or modify CronHPA jobs
Enable CronHPA and HPA to interact without conflicts
CronHPA triggers horizontal scaling for containers based on schedules. HPA is used to scale pods to ensure the availability of your applications when network traffic spikes. If ACK detects that both CronHPA and HPA are deployed, ACK sets HPA as the scaling object of CronHPA. CronHPA triggers HPA to scale pods at the scheduled time.
The following YAML template shows the configurations of CronHPA and HPA:
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
apiVersion: autoscaling/v2beta2 kind: HorizontalPodAutoscaler metadata: name: php-apache 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
- The scaleTargetRef field is used in the configurations of both CronHPA and HPA to specify the object to be scaled.
- The crontab rules in the jobs section of the CronHPA configuration specify the number to which pods are scaled at the scheduled time.
- HPA triggers scaling activities based on resource usage.
If both CronHPA and HPA are deployed, CronHPA and HPA may scale pods for the same application that is specified by scaleTargetRef. CronHPA and HPA are independent and unaware of each other. As a result, the CronHPA controller and the HPA controller scale pods for the application separately. The later scaling activity overwrites the earlier one.
Solution
The reason for the conflict between CronHPA and HPA is that the CronHPA controller and the HPA controller are unaware of each other. To resolve the conflict, CronHPA needs only to detect the status of HPA. In the HPA configuration, scaleTargetRef specifies the Deployment that is managed by HPA. The Deployment is related to a ReplicaSet. When HPA scales the Deployment, the ReplicaSet scales pods to the desired number. ACK modifies the CronHPA configuration by setting scaleTargetRef to HPA. CronHPA can find the application that is specified by scaleTargetRef in the HPA configuration. This enables CronHPA to detect the status of 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: true
- name: "scale-up"
schedule: "0 */1 * * * *"
targetSize: 3
runOnce: true
After you deploy the preceding YAML template, CronHPA is aware of the values of minReplicas, maxReplicas, and desiredReplicas in the HPA configuration. CronHPA is also aware of the current number of pods provisioned for the application that is specified by scaleTargetRef. CronHPA can detect the status of HPA by modifying the HPA configurations. CronHPA compares the desired number of pods with the current number of pods and then determines whether to trigger scaling activities and change the maximum number of pods in the HPA configuration. CronHPA also compares the desired number of pods with the maximum and minimum numbers of pods that are specified in the HPA configuration and then determines whether to change the minimum numbers of pods in the HPA configuration.
HPA (min/max) | Cronhpa | Deployment | Scaling result | Description |
---|---|---|---|---|
1/10 | 5 | 5 |
|
If the number of pods desired by CronHPA equals the current number of pods, CronHPA does not change the maximum and minimum numbers of pods in the HPA configuration. In addition, no scaling activity is triggered. |
1/10 | 4 | 5 |
|
If the number of pods desired by CronHPA is smaller than the current number of pods, no scaling activity is triggered. |
1/10 | 6 | 5 |
|
|
5/10 | 4 | 5 |
|
|
5/10 | 11 | 5 |
|
|
- HPA (min/max): the minimum and maximum numbers of pods that are specified in the HPA configuration.
- CronHPA: the desired number of pods.
- Deployment: the current number of pods that are provisioned for the application.
CronHPA does not directly change the number of pods for the Deployment. It triggers HPA to scale the pods. This resolves the conflict between CronHPA and HPA.