All Products
Search
Document Center

Container Service for Kubernetes:Interaction between CronHPA and HPA

Last Updated:Jul 24, 2024

CronHPA and HPA are independent and unaware of each other. As a result, if both CronHPA and HPA are configured in the YAML file of your application, the CronHPA controller and the HPA controller scale for the application separately. The later scaling activity overwrites the earlier one. To resolve this issue, Container Service for Kubernetes (ACK) provides a solution for CronHPA to interact with HPA without conflicts. If ACK detects that both CronHPA and HPA are deployed, it sets the scaling target of CronHPA to HPA, thereby enabling scheduled scaling for the HPA scaling object, such as a Deployment.

Note

Skip this step if both your HPA and CronHPA are created through the Container Service Management Console. ACK automatically resolves the conflict issue.

Understand conflicts between CronHPA and HPA from the configuration templates

CronHPA configuration template

HPA configuration template

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	
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
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	

After comparing the configurations of CronHPA and HPA, you can find that:

  • The scaleTargetRef field is used in the configurations of both CronHPA and HPA to specify the object to be scaled.

  • The crontab rules in the jobs section of the CronHPA configuration specify the number of replicas to be scaled when the scheduled time is reached.

  • HPA triggers scaling activities based on resource usage.

When CronHPA and HPA are both deployed, CronHPA and HPA may scale for the same application that is specified by scaleTargetRef. To resolve this issue, CronHPA needs to be aware of the current status of HPA.

Solution

To resolve the issue that CronHPA and HPA are independent and unaware of each other, ACK supports setting the scaling target of CronHPA to HPA. In the HPA configuration, the scaleTargetRef field specifies the Deployment that is managed by HPA. The Deployment is related to a ReplicaSet. When HPA scales the Deployment, a number of replicas that equals the value of ReplicaSet are scaled.

By setting scaleTargetRef of CronHPA to HPA, CronHPA is aware of the target number of replicas of CronHPA tasks, the values of minReplicas, maxReplicas, and desiredReplicas in the HPA configuration. CronHPA is also aware of the current number of replicas provisioned for the application that is specified by scaleTargetRef.

However, CronHPA does not directly change the number of replicas for the Deployment. It triggers HPA to modify the configuration of Deployment to avoid the conflict between CronHPA and HPA.

image

The following YAML template shows the configurations that enable CronHPA to interact with HPA without conflicts:

apiVersion: autoscaling.alibabacloud.com/v1beta1
kind: CronHorizontalPodAutoscaler
metadata:
  labels:
    controller-tools.k8s.io: "1.0"
  name: cronhpa-sample
spec:
   scaleTargetRef:
      apiVersion: autoscaling/v1
      kind: HorizontalPodAutoscaler
      name:  nginx-deployment-basic-hpa
   jobs:
   - name: "scale-down"
     schedule: "30 */1 * * * *"
     targetSize: 1
     runOnce: false
   - name: "scale-up"
     schedule: "0 */1 * * * *"
     targetSize: 3
     runOnce: false

Example and rule description

The following table describes the rules that enable CronHPA to interact with HPA without conflicts.

Note

The following list describes the parameters in the table:

  • HPA (min/max): The minimum and maximum number of replicas that are specified in HPA configuration (minReplicas, maxReplicas).

  • CronHPA target number of replicas: The target number of replicas for CronHPA tasks.

  • Current number of replicas: The number of replicas before the application scales.

  • Number of replicas: The number of replicas after the application scales.

Scaling conditions

Scaling results

Description

HPA (min/max)

CronHPA target number of replicas

Current number of replicas

1/10

5

5

  • HPA (min/max): 1/10

  • Number of replicas: 5

If the target number of replicas in the CronHPA configuration equals the current number of replicas, CronHAP does not change the minReplicas and maxReplicas in the HPA configuration. In addition, no scaling activity is triggered.

1/10

4

5

  • HPA (min/max): 1/10

  • Number of replicas: 5

If the target number of replicas in the CronHPA configuration is lower than the current number of replicas, no scaling activity is triggered.

1/10

6

5

  • HPA (min/max): 6/10

  • Number of replicas: 6

  • If the target number of replicas in CronHPA is greater than the current number of replicas, CronHPA adds pods to reach the desired number.

  • If the target number of replicas in CronHPA is greater than the value of minReplicas in the HPA configuration, CronHPA changes the value of minReplicas.

5/10

4

5

  • HPA (min/max): 4/10

  • Number of replicas: 5

  • If the target number of replicas in CronHPA is lower than the current number of replicas, no scaling activity is triggered.

  • If the target number of replicas in CronHPA is lower than the value of minReplicas in the HPA configuration, CronHPA changes the value of minReplicas.

5/10

11

5

  • HPA (min/max): 11/11

  • Number of replicas: 11

  • If the target number of replicas in CronHPA is greater than the current number of replicas, CronHPA adds pods to reach the desired number.

  • If the target number of replicas in CronHPA is greater than the value of maxReplicas in the HPA configuration, CronHPA changes the value of maxReplicas.

References

For more information about HPA and CronHPA, see Use horizontal pod autoscaling (HPA) and Use CronHPA for scheduled horizontal scaling.