Container Timing Horizontal Scaling (CronHPA)
In predictable service scenarios, you can predefine scheduled scaling tasks to increase the number of working containers before service peaks, thereby minimizing the workload for operations and maintenance teams. Currently, CCE supports setting scheduled scaling tasks for Deployment and StatefulSet, with an option to combine scheduled scaling with HPA, significantly broadening the scope of auto-scaling use cases.
Prerequisites
- A K8S Cluster CCE has been created. For specific operations, refer to Create a K8S Cluster CCE.
- The CCE-CronHPA-Controller component has been installed. For specific operations, please refer to CCE CronHPA Controller Description.
Operation steps
Use CronHPA through the console
Method I: Enable CronHPA for an existing workload
- Sign in to the Baidu AI Cloud management console. Navigate to Product Services > Cloud Native > Cloud Container Engine (CCE). Click Cluster Management > Cluster List, then select the target cluster to enter the Cluster Details page. In the sidebar, click Autoscaler > CronHPA.
- Click the Create Cron Horizontal Pod Autoscaler operation on the Cron Horizontal Pod Autoscaler page.
- Set up the scaling configurations on the Create Cron Horizontal Pod Autoscaler page.
| ConfigMap | Required/Optional | Configuration |
|---|---|---|
| Name | Required | Provide a name for the Cron Horizontal Pod Autoscaler. The name must be 1-63 characters long, consist of only numbers, lowercase letters, and hyphens ("-"), start with a letter, and end with a letter or number. |
| Namespace | Required | Choose the namespace where the Cron Horizontal Pod Autoscaler will be created. |
| Associated object type | Required | Support is available for configuring Cron Horizontal Pod Autoscaler for Deployment, StatefulSet, and Horizontal Pod Autoscaler (HPA). |
| Associated objects | Required | Select the desired Deployment, StatefulSet, and Horizontal Pod Autoscaler (HPA) resources for enabling the feature. |
| CornJob | Required | Configure the strategy for scheduled execution of scaling rules, and support adding multiple CornJobs. Name: Specify the name of the scheduled task. It must be 1-63 characters long, contain only numbers, lowercase letters, and "-", and start with a letter and end with a number or a letter. Scheduled rules: Enter the scheduled rule in the format: second minute hour day month week (for example: 1 2 3 4 * * means that the scaling task is executed once at 03:02:01 on the 4th day of each month). For more information, refer to go-cron. One-time execution: Configure whether to enable the CornJob only once. If checked, the task will be executed only once. Target replica count: Specify the number of Pod instances for scaling. |
| Task skipping time | Optional | Define the time rules during which the task will not execute. When the specified time is matched, CronJob will not be triggered. The format is second minute hour day month week, with a minimum unit of day. |
- Click OK to complete creation.
Method II: Enable CronHPA during the creation of a workload
Taking the creation of a stateless deployment as an example:
- Sign in to the Baidu AI Cloud management console. Navigate to Product Services - Cloud Native - Cloud Container Engine (CCE). Click Cluster Management - Cluster List, then select the target cluster to enter the Cluster Details page. In the sidebar, click Workloads > Stateless Deployment.
- Click the Create Stateless Deployment operation on the Stateless Deployment page.
- On the Create Stateless Deployment - Advanced Settings page, check Cron Horizontal Pod Autoscaler to enable Cron Horizontal Pod Autoscaler (CronHPA) for the workload.
| ConfigMap | Required/Optional | Configuration |
|---|---|---|
| Name | Required | Provide a name for the Cron Horizontal Pod Autoscaler. The name must be 1-63 characters long, consist of only numbers, lowercase letters, and hyphens ("-"), start with a letter, and end with a letter or number. |
| Namespace | Required | Choose the namespace where the Cron Horizontal Pod Autoscaler will be created. |
| Associated object type | Required | Support is available for configuring Cron Horizontal Pod Autoscaler for Deployment, StatefulSet, and Horizontal Pod Autoscaler (HPA). |
| Associated objects | Required | Select the desired Deployment, StatefulSet, and Horizontal Pod Autoscaler (HPA) resources for enabling the feature. |
| CornJob | Required | Configure the strategy for scheduled execution of scaling rules, and support adding multiple CornJobs. Name: Specify the name of the scheduled task. It must be 1-63 characters long, contain only numbers, lowercase letters, and "-", and start with a letter and end with a number or a letter. Scheduled rules: Enter the scheduled rule in the format: second minute hour day month week (for example: 1 2 3 4 * * means that the scaling task is executed once at 03:02:01 on the 4th day of each month). For more information, refer to go-cron. One-time execution: Configure whether to enable the CornJob only once. If checked, the task will be executed only once. Target replica count: Specify the number of Pod instances for scaling. |
| Task skipping time | Optional | Define the time rules during which the task will not execute. When the specified time is matched, CronJob will not be triggered. The format is second minute hour day month week, with a minimum unit of day. |
- Click Finish, and then a stateless deployment supporting CronHPA has been configured.
Use CronHPA through kubectl commands
The following is a specific CronHPA example:
1apiVersion: cce.baidubce.com/v1
2kind: CronHPA
3metadata:
4 name: cronhpa-sample
5spec:
6 scaleTargetRef:
7 apiVersion: apps/v1
8 kind: Deployment
9 name: nginx-deployment-basic
10 excludeDates:
11 - "* * * 15 11 *"
12 - "* * * 1 11 *"
13 crons:
14 - name: "scale-down"
15 schedule: "30 */1 * * * *"
16 targetSize: 1
17 runOnce: true
18 - name: "scale-up"
19 schedule: "01 */1 * * * *"
20 targetSize: 3
Introduction to deployment parameters
| Name | Function | Required or not |
|---|---|---|
| spec.scaleTargetRef | Metadata of the expected scaling object | Required |
| crons.name | Task name | Required |
| crons.schedule | Time of task triggering | Required |
| crons.runOnce | One-time execution of the task | Optional |
| crons.targetSize | Expected replica count | Required |
| spec.excludeDates | Time of task not triggering | Optional |
Among them, schedule and excludeDates need to be configured in the following format:
| Field name | Required or not | 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 | */,-? |
Special symbol description
- An asterisk (*) indicates that the field can match all possible values. For example, if an asterisk is used in the 5th field (month), it signifies every month.
- Slash (/) indicates an increment interval. For example, if the value of the first field (minutes) is 3-59/15, it means execution starts at the 3rd minute of each hour and repeats once every 15 minutes thereafter (i.e., executed at 3, 18, 33, 48 timepoints). This can also be expressed as: 3/15
- A comma (,) is used to specify enumerated values. For instance, if the value in the 6th field is MON,WED,FRI, it denotes execution on Monday, Wednesday, and Friday.
- Hyphen (-) indicates a range. For example, value of 9-17 in the third field means every hour from 9 am to 5 pm (including 9 and 17)
- Question mark (?) is only used for day of month and day of week. \ indicates no specified value and can be used to replace *
Example of cron
- Execute every 5 seconds: /5 * * * ?
- Execute every 1 minute: 0 /1 * * ?
- Execute once daily at 23:00: 0 0 23 * * ?
- Execute once daily at 1:00 AM: 0 0 1 * * ?
- Execute once at 1:00 AM on the 1st day of each month: 0 0 1 1 * ?
- Execute once at the 26th, 29th and 33rd minutes of each hour: 0 26,29,33 * * * ?
- Execute daily at 0:00, 13:00, 18:00, and 21:00: 0 0 0,13,18,21 * * ?
Example of CronHPA scaling deployment
To apply CronHPA to a normal workload, you only need to configure the spec.scaleTargetRef field to the information of the workload. The following is a specific example of CronHPA scaling deployment:
1---
2apiVersion: apps/v1
3kind: Deployment
4metadata:
5 name: nginx-deployment-basic
6 labels:
7 app: nginx
8spec:
9 replicas: 2
10 selector:
11 matchLabels:
12 app: nginx
13 template:
14 metadata:
15 labels:
16 app: nginx
17 spec:
18 containers:
19 - name: nginx
20 image: nginx
21 ports:
22 - containerPort: 80
23---
24apiVersion: cce.baidubce.com/v1
25kind: CronHPA
26metadata:
27 name: cronhpa-sample
28spec:
29 scaleTargetRef: //Configure to the information of the above deployment
30 apiVersion: apps/v1
31 kind: Deployment
32 name: nginx-deployment-basic
33 crons:
34 - name: "scale-down"
35 schedule: "30 */1 * * * *"
36 targetSize: 1
37 - name: "scale-up"
38 schedule: "01 */1 * * * *"
39 targetSize: 3
Example of CronHPA compatibility with HPA
It can be seen from the template definitions of CronHPA and HPA:
- Both CronHPA and HPA access the scaling object through the scaleTargetRef field.
- CronHPA adjusts the replica count periodically based on crontab rules.
- HPA determines scaling requirements based on resource utilization.
If both CronHPA and HPA are set, there will be a scenario where CronHPA and HPA operate on the same scaleTargetRef at the same time. CronHPA and HPA are independent of each other and cannot perceive each other. The later operation will overwrite the earlier one. For example, after a scheduled scale-up is completed, it may be scaled down due to low resource utilization.
Therefore, when the object to be scaled is referenced by HPA, to avoid conflicts between HPA and CronHPA that cause the result to be overwritten, you need to configure spec.scaleTargetRef to the corresponding HPA object. In this way, CronHPA can perceive the status of HPA and adjust the replica count by adjusting the upper and lower limits of HPA.
Here are some specific scenario examples that are compatible with CronHPA and HPA to achieve scheduled scaling:
| HPA(min/max) | CronHPA | deployment | result | Description |
|---|---|---|---|---|
| 1/10 | 5 | 5 | HPA(1/10) deployment 5 | The expected replica count of CronHPA is fixed and consistent, without change |
| 1/10 | 4 | 5 | HPA(1/10) deployment 5 | The current replica count is 5, which is higher than the expected replica count (4) of CronHPA; the expected replica count is within the range [1,10], without change |
| 1/10 | 6 | 5 | HPA(6/10) deployment 6 | The expected replica count of CronHPA is 6, which is higher than the current replica count (5). Adjust the HPA lower limit to automatically scale up the replica count to 6 |
| 5/10 | 4 | 5 | HPA(4/10) deployment 5 | The expected replica count of CronHPA is lower than the current replica count (5). Keep the current replica count. The expected replica count of CronHPA is lower than the HPA lower limit. Modify the HPA lower limit (ensure at least 4 replicas) |
| 5/10 | 11 | 5 | HPA(11/11) deployment 11 | The expected replica count of CronHPA is higher than the HPA upper limit. Modify the HPA lower limit to the expected replica count of CronHPA. The upper limit is the same as the lower limit |
The following is a specific yaml example of CronHPA compatible with HPA:
1---
2apiVersion: autoscaling/v1
3kind: HorizontalPodAutoscaler
4metadata:
5 name: nginx-deployment-basic-hpa
6 namespace: default
7spec:
8 scaleTargetRef:
9 apiVersion: apps/v1
10 kind: Deployment
11 name: nginx-deployment-basic
12 minReplicas: 1
13 maxReplicas: 10
14 targetCPUUtilizationPercentage: 50
15---
16apiVersion: cce.baidubce.com/v1
17kind: CronHPA
18metadata:
19 name: cronhpa-sample
20spec:
21 scaleTargetRef:// Configure to the HPA object
22 apiVersion: autoscaling/v1
23 kind: HorizontalPodAutoscaler
24 name: nginx-deployment-basic-hpa
25 crons:
26 - name: "scale-down"
27 schedule: "30 */1 * * * *"
28 targetSize: 5
29 runOnce: false
30 - name: "scale-up"
31 schedule: "01 */1 * * * *"
32 targetSize: 10
33 runOnce: false
