Configure CronHPA to adjust HPA's minReplicas and maxReplicas instead of scaling the Deployment directly.
If you deploy HPA and CronHPA through the ACK console, ACK configures them automatically. Skip this topic.
How it works
By default, both CronHPA and HPA set their scaleTargetRef to the same Deployment. Whichever acts last overwrites the other's result.
The fix: set CronHPA's scaleTargetRef to the HPA resource instead of the Deployment. With this setup:
-
CronHPA reads HPA's
minReplicas,maxReplicas, anddesiredReplicasbefore acting. -
CronHPA adjusts HPA's
minReplicasormaxReplicasbased on its scheduledtargetSize. -
HPA scales pods through the Deployment and its ReplicaSet.
CronHPA never changes the pod count directly, eliminating conflicts.
Configure CronHPA to target HPA
In a typical setup, CronHPA and HPA both point at the same Deployment:
Sample CronHPA template (before modification)
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
Sample HPA template
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hpa-sample
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
To resolve the conflict, change scaleTargetRef in your CronHPA manifest to point at the HPA resource:
| Field | Before | After |
|---|---|---|
apiVersion |
apps/v1 |
autoscaling/v2 |
kind |
Deployment |
HorizontalPodAutoscaler |
name |
nginx-deployment-basic |
hpa-sample |
Example: CronHPA configured to scale business-hours traffic
Assume hpa-sample manages traffic from 09:00 to 17:00 (UTC+8). Configure CronHPA to scale out at 09:00 and in at 17:00:
apiVersion: autoscaling.alibabacloud.com/v1beta1
kind: CronHorizontalPodAutoscaler
metadata:
labels:
controller-tools.k8s.io: "1.0"
name: cronhpa-sample
spec:
scaleTargetRef:
apiVersion: autoscaling/v2
# Set the resource type to HPA.
kind: HorizontalPodAutoscaler
# Specify the name of the HPA resource.
name: hpa-sample
jobs:
- name: "scale-up-9am"
schedule: "0 0 9 * * * "
targetSize: 20
runOnce: false
- name: "scale-down-5pm"
schedule: "0 0 17 * * *"
targetSize: 3
runOnce: false
Compatibility rules
When CronHPA targets HPA, it acts as a dynamic floor for replicas: raising minReplicas to scale out and lowering minReplicas to scale in, without setting the pod count directly. HPA scales within those bounds based on metrics.
The following table shows how CronHPA and HPA interact under each scaling condition.
Table headers:
-
HPA (min/max): HPA's
minReplicas/maxReplicas. -
CronHPA desired: The CronHPA job's
targetSize. -
Current replicas: Running pod count.
-
Final replicas: Pod count after scaling.
| HPA (min/max) | CronHPA desired | Current replicas | Final replicas | Behavior |
|---|---|---|---|---|
| 1/10 | 5 | 5 | 5 | Desired equals current. minReplicas and maxReplicas unchanged. No scaling. |
| 1/10 | 4 | 5 | 5 | Desired is less than current. No scaling. |
| 1/10 | 6 | 5 | 6 | Desired exceeds current. CronHPA sets minReplicas to 6. HPA scales to 6. New HPA (min/max): 6/10. |
| 5/10 | 4 | 5 | 5 | Desired is less than current. No scaling. CronHPA sets minReplicas to 4. New HPA (min/max): 4/10. |
| 5/10 | 11 | 5 | 11 | Desired exceeds maxReplicas. CronHPA sets both minReplicas and maxReplicas to 11. HPA scales to 11. New HPA (min/max): 11/11. |