Cron Horizontal Pod Autoscaler (CronHPA)
HPA(Horizontal Pod Autoscaling) refers to the horizontal auto-scaling of Kubernetes Pods, which utilizes monitoring metrics to automatically scale up or down the number of Pods in a service in a Kubernetes cluster, including monitoring metrics using CPU memory, etc. The difference with Scheduled Autoscaling is that it scales the number of Pods through a timer, for known high concurrency, and pre-scales the business to cope 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
User clicks to add to enter the new scheduling task page, in the page need to input the name of the timer, select the object to be scaled, the time of the execution plan, and the target Pod quantity. If you check the "Single Execution" option, then it means that this scheduled scaling task needs to be executed only once and is not periodically executed.
1.2 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, 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.3 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.
apiVersion: autoscaling.ucloud.cn/v1
kind: CronHorizontalPodAutoscaler
metadata:
name: "nginx-cronhpa"
namespace: default
spec:
jobs: # Execution plan, 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, supports 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 min/max | CronHPA Target Pod Number | Deployment Current Pod Number | Scaling result | Description |
---|---|---|---|---|
1/10 | 5 | 5 | HPA:5/10 Deployment:5 | If the CronHPA target replica number > HPA minimum replica number, modify the minimum replicas in HPA |
5/10 | 4 | 5 | HPA:4/10 Deployment:5 | If CronHPA target replica number < HPA minimum replica number, modify the minimum replicas in HPA 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 Deployment:11 | If CronHPA target replica number > HPA maximum replica number, modify both the maximum and minimum replicas in HPA |