When both CronHPA (CronHorizontalPodAutoscaler) and HPA (HorizontalPodAutoscaler) target the same workload, they scale independently and overwrite each other's results — the later scaling activity always wins. To prevent this conflict, ACK lets CronHPA delegate scaling to HPA: instead of scaling a Deployment directly, CronHPA adjusts HPA's minReplicas and maxReplicas at scheduled times, then lets HPA perform the actual scaling.
If you deploy both HPA and CronHPA through the ACK console, ACK automatically configures them to work together. Skip this topic if you use the console.
How it works
By default, both CronHPA and HPA set their scaleTargetRef to the same Deployment. When both run, whichever acts last overwrites the other's result.
The fix is to set CronHPA's scaleTargetRef to the HPA resource instead of the Deployment. With this setup:
CronHPA reads HPA's
minReplicas,maxReplicas, anddesiredReplicasvalues before acting.CronHPA adjusts HPA's
minReplicasormaxReplicasbased on its scheduledtargetSize.HPA performs the actual pod scaling through the Deployment and its ReplicaSet.
CronHPA never directly changes the pod count. This eliminates conflicts.
Configure CronHPA to target HPA
Before making the change, a typical setup has CronHPA and HPA each pointing directly 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: 11Sample 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: 50To resolve the conflict, change the scaleTargetRef in your CronHPA manifest from the Deployment to 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 your workload's daily traffic from 09:00 to 17:00 (UTC+8). Configure CronHPA to scale out at 09:00 and scale 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: falseCompatibility rules
When CronHPA targets HPA, CronHPA acts as a dynamic floor for the replica count: it raises minReplicas when scaling out and lowers minReplicas when scaling in, but never sets the pod count directly. HPA continues to scale within those bounds based on metrics.
The following table describes how CronHPA and HPA interact under each scaling condition.
Table headers:
HPA (min/max): The
minReplicas/maxReplicasvalues of HPA.CronHPA desired: The
targetSizespecified in the CronHPA job.Current replicas: The current number of running pods.
Final replicas: The pod count after scaling completes.
| HPA (min/max) | CronHPA desired | Current replicas | Final replicas | Behavior |
|---|---|---|---|---|
| 1/10 | 5 | 5 | 5 | CronHPA desired equals current replicas. No change to minReplicas or maxReplicas. No scaling triggered. |
| 1/10 | 4 | 5 | 5 | CronHPA desired is less than current replicas. No scaling triggered. |
| 1/10 | 6 | 5 | 6 | CronHPA desired is greater than current replicas. CronHPA updates minReplicas to 6. HPA scales out to 6. HPA (min/max) becomes 6/10. |
| 5/10 | 4 | 5 | 5 | CronHPA desired is less than current replicas. No scaling triggered. CronHPA updates minReplicas to 4. HPA (min/max) becomes 4/10. |
| 5/10 | 11 | 5 | 11 | CronHPA desired exceeds maxReplicas. CronHPA updates both minReplicas and maxReplicas to 11. HPA scales out to 11. HPA (min/max) becomes 11/11. |