CCE CronHPA Controller Description
Component introduction
The CCE-CronHPA-Controller (CronHPA Controller) allows users to define scheduled scaling rules via CRD. For applications with clear periodic workload patterns, it provides pre-scaling capabilities through the Container Cron Horizontal Pod Autoscaler.
Component function
- Scheduled scaling of the specified count of workload replicas
Application scenarios
- Cluster resources are managed based on a predefined schedule. CronHPA automatically initiates scaling-up or scaling-down actions at scheduled time points by configuring a series of tasks.
- Make sure system resources are appropriately configured and adjusted during spikes in user traffic to prevent service disruptions caused by scale-up delays.
Usage restrictions
- Only support Kubernetes clusters of v1.16 and above
Install component
- Sign in to the Baidu AI Cloud official website and enter the management console.
- Select Product Services > Cloud Native > Cloud Container Engine (CCE) to enter the CCE management console.
- Click Cluster Management - Cluster List in the left navigation pane.
- Click on the target cluster name in the Cluster List page to navigate to the cluster management page.
- On the Cluster Management page, click Component Management.
- In the component management list, select the CCE CronHPA Controller component and click Install
- Click the OK button to finalize the component installation.
Deployment status
For installing the CCE CronHPA Controller component, the following objects will be deployed in the Kubernetes cluster:
| Object name | Belonging to namespace | Resource usage | Types |
|---|---|---|---|
| cronhpas.cce.baidubce.com | kube-system | - | CRD |
| cce-cronhpa-controller | kube-system | CPU:100m Memory:100Mi | Deployment |
Use
When deploying CronHPA to the cluster using YAML, the CCE CronHPA Controller monitors the CronHPA creation event and executes the corresponding logic.
Example of CronHPA template
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.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 for common objects
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
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
Version records
| Version No. | Cluster version compatibility | Update time | Update content | Impact |
|---|---|---|---|---|
| 0.1.0 | CCE/v1.16+ | 2024.03.15 | First release | - |
