To improve resource utilization, reduce resource costs, and avoid resource waste, Container Compute Service (ACS) provides the ack-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 in an ACS cluster based on a schedule. This topic also describes how to enable CronHPA and Horizontal Pod Autoscaler (HPA) to interact without conflicts.
Prerequisites
-
You have created an ACS cluster.
Background information
The ack-kubernetes-cronhpa-controller is a Kubernetes controller that scales workloads in a cluster based on a cron-like schedule. You can use CronHPA with any Kubernetes object that supports thescale subresource, such as a Deployment or StatefulSet. For more information, visit the kubernetes-cronhpa-controller repository.
The following table describes the parameters in the CronHPA configuration.
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 | 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 the dates that are specified in excludeDates. Note
The smallest time unit you can exclude is aDay. The format is For example, if you do not want to execute scaling jobs on November 15, set excludeDates to the following value:
|
jobs |
You can define multiple CronHPA jobs within a single
|
Install the CronHPA controller
You can install the CronHPA controller ack-kubernetes-cronhpa-controller by using one of the following methods.
-
Log on to the ACS console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster. In the left navigation pane, click Add-ons.
-
On the Add-ons page, click the Manage Applications tab. Find ack-kubernetes-cronhpa-controller, click Install, and then click OK in the dialog box that appears.
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 add-ons.
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 by using one of the following methods:
Method 1: Create a job during application creation
On the Advanced page of the application creation wizard, navigate to the Scaling section. Select Enable next to CronHPA. For detailed steps on creating an application, see Create a stateless workload (Deployment) or Create a stateful workload (StatefulSet).
If you are prompted that the ack-kubernetes-cronhpa-controller component is not installed, click Install to install it.
The ACK console automatically checks if the CronHPA component is installed. If it is not installed, an Install button appears. After installing the CronHPA component, the configuration settings for the scheduled scaling job are displayed. The following table describes the parameters.
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 predefined-schedules. |
Method 2: Create a job for an existing application
The following example demonstrates how to create a CronHPA job for an existing application. A stateless application is used in this example.
-
Log on to the ACS console. In the left navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Workloads > Deployments.
-
On the Deployments page, find the application, and then click Details in the Actions column.
-
Click the Pod Scaling tab and configure the scheduled scaling job.
-
If the CronHPA component is not installed, an Install button appears. Click Install and then proceed to the next step.
If the CronHPA controller is installed, perform the following steps.
-
-
Click Create to the right of CronHPA. In the Create dialog box, set the parameters for the job.
The following table describes the parameters.
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 predefined-schedules.
Create or modify CronHPA jobs
-
Follow the steps in Create a scheduled scaling CronHPA task to go to the Pod Scaling page.
-
On the Pod Scaling tab, in the CronHPA section, find the target job and click Add or Modify Job under the Actions column.
-
In the Edit dialog box, click Add Task to create a new job or click an existing job to modify it. When you are finished, click OK.
The job configuration form includes the following fields: Job Name, Desired Number of Replicas, and Scaling Schedule (which supports five methods: By Hour, By Day, By Week, By Month, or cron expression). If you select cron expression, enter the expression in the input box in the format:
Seconds Minutes Hours Day-of-month Month Day-of-week(for example,1 2 3 4 * *means 03:02:01 on the 4th of every month).NoteYou can delete a CronHPA job. In the Edit dialog box, existing jobs appear as tags. You can remove a job by clicking the delete icon next to its name. To add a new job, enter a name in the Job Name input box and click Add Job. After making your changes, click OK.
Templates of CronHPA and HPA
CronHPA
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
HPA
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: nginx-deployment-basic-hpa
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
Enable CronHPA and HPA to interact without conflicts
CronHPA and HPA take effect on workloads, such as Deployments or StatefulSets. If you use CronHPA and HPA to scale a workload at the same time, conflicts may occur. To resolve the conflicts, CronHPA allows you to use HPA as the object to be scaled, which is compatible with the features of HPA.
The configurations of CronHPA and HPA indicate the following information:
-
Both CronHPA and HPA use the
spec.scaleTargetReffield to identify the scaling target. -
CronHPA usescrontab rules in
spec.jobsto schedule the scaling of replicas. 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 separately scale pods for the application. The later scaling activity overwrites the earlier activity.
Compatibility solution for CronHPA and HPA
As mentioned, the conflict can be resolved if CronHPA is aware of the current state of HPA. To achieve this, ACK configures the scaleTargetRef in the CronHPA to point to the HPA object itself, not the final workload object. When performing a scaling operation, CronHPA first finds the real scaleTargetRef through the HPA object and then adjusts the workload object based on this state. This prevents the controllers from overwriting each other and ensures that scaling is consistent and predictable.
The following YAML template shows the configurations that enable CronHPA and HPA to interact without conflicts:
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: false
- name: "scale-up"
schedule: "0 */1 * * * *"
targetSize: 3
runOnce: falseBased on the CronHPA definition, a CronHPA object can determine the values of spec.minReplicas, spec.maxReplicas, and status.desiredReplicas in the HPA. It also knows the current replica count of the workload specified by spec.scaleTargetRef. A CronHPA interacts with the HPA by adjusting it. The CronHPA uses the greater value between its target replica count and the current replica count to determine whether to scale the workload or increase the HPA's upper limit. It uses the smaller value between its target replica count and the HPA's configuration to determine whether to modify the HPA's lower limit.
The following table describes the rules that enable CronHPA and HPA to interact without conflicts.
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 less than the current number of pods, no scaling activity is triggered. |
1/10 | 6 | 5 |
|
|
5/10 | 4 | 5 |
|
|
5/10 | 11 | 5 |
|
|
The following list describes the parameters in the table:
HPA (min/max): the minimum and maximum numbers of pods that are specified in the HPA configuration.
CronHPA: the desired number of pods that are specified in the CronHPA configuration.
-
Deployment: The number of replicas for the application before scaling.
CronHPA does not directly change the number of pods for the Deployment. Instead, CronHPA triggers HPA to scale the pods. This resolves the conflict between CronHPA and HPA.