All Products
Search
Document Center

Container Service for Kubernetes:Make CronHPA compatible with HPA

Last Updated:Mar 26, 2026

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, and desiredReplicas values before acting.

  • CronHPA adjusts HPA's minReplicas or maxReplicas based on its scheduled targetSize.

  • HPA performs the actual pod scaling through the Deployment and its ReplicaSet.

CronHPA never directly changes the pod count. This eliminates conflicts.

Diagram showing CronHPA targeting HPA, which in turn targets the Deployment

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: 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 the scaleTargetRef in your CronHPA manifest from the Deployment to the HPA resource:

FieldBeforeAfter
apiVersionapps/v1autoscaling/v2
kindDeploymentHorizontalPodAutoscaler
namenginx-deployment-basichpa-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: false

Compatibility 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/maxReplicas values of HPA.

  • CronHPA desired: The targetSize specified in the CronHPA job.

  • Current replicas: The current number of running pods.

  • Final replicas: The pod count after scaling completes.

HPA (min/max)CronHPA desiredCurrent replicasFinal replicasBehavior
1/10555CronHPA desired equals current replicas. No change to minReplicas or maxReplicas. No scaling triggered.
1/10455CronHPA desired is less than current replicas. No scaling triggered.
1/10656CronHPA desired is greater than current replicas. CronHPA updates minReplicas to 6. HPA scales out to 6. HPA (min/max) becomes 6/10.
5/10455CronHPA desired is less than current replicas. No scaling triggered. CronHPA updates minReplicas to 4. HPA (min/max) becomes 4/10.
5/1011511CronHPA desired exceeds maxReplicas. CronHPA updates both minReplicas and maxReplicas to 11. HPA scales out to 11. HPA (min/max) becomes 11/11.

What's next