# Cron Horizontal Pod Autoscaler (CronHPA)

HPA (Horizontal Pod Autoscaling) refers to the horizontal auto-scaling of Kubernetes Pods, where the Kubernetes cluster uses monitoring metrics (such as CPU and memory) to automatically scale in or out the number of Pods in services. Different from timed scaling, which scales the number of Pods through timers for known high concurrency, allowing businesses to pre-scale before high concurrency arrives.

## 1. Using Scheduled Autoscaling in UK8S

### 1.1 Enable scheduled autoscaling

Click on the **Cluster Scaling** tab in the UK8S Cluster Management page, select **Scheduled Autoscaling CronHPA**, and click immediately to install the CronHPA control plugin to enable the scheduled scaling function.

### 1.2 Add scheduled scaling conditions

Users can click Add to enter the new timed task page, where they need to input the timer name, select the object to be scaled, set the execution schedule time, and specify the target number of Pods. If the Single Execution option is checked, it indicates that the timed scaling task will only run once (non-periodic).

### 1.3 Explanation of crontab syntax

The syntax used for the crontab schedule is consistent with CronTab. Below are a few commonly used syntax.

Crontab format (the first 5 bits are time options, and here we only use the first 5 bits)

```
<Minute> <Hour> <Day> <Month> <Weekday> <Command>
```

Once a day, execute at 0:0

```
0 0 * * *
```

Once a week, execute at 0:0

```
0 0 * * 0
```

Once a month, execute at 0:0

```
0 0 1 * *
```

> ⚠️ CronTab command time is in UTC. For the actual execution time, users can calculate it as +8 hours.

### 1.4 Example yaml

We set the `up5` and `down2` execution plans for the nginx-deployment application, which are set to `40 8 * * *` and `50 8 * * *`, respectively, meaning that the application will expand to 5 at 16:40 Beijing time and shrink to 2 at 16:50, and it will execute every day.

```yaml
apiVersion: autoscaling.ucloud.cn/v1
kind: CronHorizontalPodAutoscaler
metadata:
  name: "nginx-cronhpa"
  namespace: default
spec:
  jobs: # Execution plan, and you can add multiple execution plans in the same CronHPA task
  - name: "up5"
    schedule: "40 8 * * * "
    targetSize: 5
    runOnce: false
  - name: "down2"
    schedule: "50 8 * * * "
    targetSize: 2
    runOnce: false 
  scaleTargetRef: # Target execution object, supporting Deployment, StatefulSet and HPA resource objects
    apiVersion: apps/v1
    kind: Deployment
    name: nginx-deployment
```

## 2. CronHPA Scheduled Autoscaling supports HPA objects

CronHPA plugin supports selecting existing HPA objects when creating, and the compatibility rules are as follows:

|   HPA Configuration<br>min/max    | CronHPA<br>Target Pod Number | Deployment<br>Current Pod Number | Scaling result                                                   | Description                                                                                         |
| :-------------------------------: | :--------------------------: | :-----------------------------: | ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| <div style="width:55pt">1/10</div> | <div style="width:60pt">5</div> |              5              | <div style="width:90pt">HPA：5/10<br>Deployment：5</div> | If the CronHPA target replica number > HPA minimum replica number, modify the minimum replicas in HPA                                                   |
|               5/10                |              4               |              5               | HPA：4/10<br>Deployment：5                               | If CronHPA target replica number < HPA minimum replica number, modify the minimum replicas in HPA<br>When the service drops below the set HPA threshold range, HPA will adjust the replica number in Deployment to 4 |
|               1/10                |              11              |              5               | HPA：11/11<br>Deployment：11                             | If CronHPA target replica number > HPA maximum replica number, modify both the maximum and minimum replicas in HPA                                             |
