To help you reduce resource waste, Container Service provides the kubernetes-cronhpa-controller component to automatically scale resources at specific times. This topic describes how to implement cron HPA.

Prerequisites

Background information

kubernetes-cronhpa-controller is a Kubernetes Horizontal Pod Autoscaler (HPA) controller that scales cluster resources based on a crontab-like schedule. You can use CronHorizontalPodAutoscaler (cronhpa) with any Kubernetes object (such as deployment and StatefulSet) that supports scale subresources. kubernetes-cronhpa-controller is now an open-source project in GitHub.

The following example introduces cronhpa parameters.
apiVersion: autoscaling.alibabacloud.com/v1beta1
kind: CronHorizontalPodAutoscaler
metadata:
  labels:
    controller-tools.k8s.io: "1.0"
  name: cronhpa-sample
  namespace: default 
spec:
   scaleTargetRef:
      apiVersion: apps/v1
      kind: Deployment
      name: nginx-deployment-basic
   excludeDates:
   # exclude November 15th
   - "* * * 15 11 *"
   # exclude every Friday 
   - "* * * * * 5"
   jobs:
   - name: "scale-down"
     schedule: "30 */1 * * * *"
     targetSize: 1
   - name: "scale-up"
     schedule: "0 */1 * * * *"
     targetSize: 3
     runOnce: true
Parameter Description
scaleTargetRef scaleTargetRef specifies the scaling target. The scaling target must support scale subresources.
excludeDates excludeDates specifies an array of dates. Jobs are not executed on dates specified by excludeDates.
Note The minimum unit is Days.
To not execute jobs on November 15th, set excludeDates to the following value:
excludeDates:
  - "* * * 15 11 *"
jobs You can set multiple cronhpa jobs in one spec field. A cronhpa job has the following parameters:
  • name: The name must be unique under one cronhpa object.
  • schedule: The schedule of job execution. kubernetes-cronhpa-controller uses the go-cron Golang library, which supports a variety of rules. The cron expression must conform to the following format rules. Otherwise, an error occurs during creation.
    The format rules are as follows:
    Field name   | Mandatory? | Allowed values  | Allowed special characters
      ----------   | ---------- | --------------  | --------------------------
      Seconds      | Yes        | 0-59            | * / , -
      Minutes      | Yes        | 0-59            | * / , -
      Hours        | Yes        | 0-23            | * / , -
      Day of month | Yes        | 1-31            | * / , - ?
      Month        | Yes        | 1-12 or JAN-DEC | * / , -
      Day of week  | Yes        | 0-6 or SUN-SAT  | * / , - ?
  • targetSize: The target number of pods at the specific time.
  • runOnce: If you set runOnce to true, the job exits after one run.

Install kubernetes-cronhpa-controller

  1. Log on to the Container Service console.
  2. In the left-side navigation pane, choose Marketplace > App Catalog. The Alibaba Cloud Apps tab appears.
  3. In the upper-right corner, enter kubernetes-cronhpa-controller in the search box next to Name and click ack-kubernetes-cronhpa-controller.metric cronhpa
  4. On the App Catalog - ack-kubernetes-cronhpa-controller page, set the parameters in the Deploy section at the right.cronhpa
    • Cluster: Select the cluster where the resource object is to be deployed.
    • Namespace: Select the namespace where the resource object belongs. The default namespace is default. Except for underlying computing resources such as nodes and PVs, most resources are scoped to namespaces.
    • Release Name: Enter the release name of the resource object.
  5. Click Create to install kubernetes-cronhpa-controller.

Example 1: Deploy cronhpa alone

After kubernetes-cronhpa-controller is installed, the following example demonstrates how to deploy cronhpa.

  1. Run the following command to deploy a sample application and cronhpa.
    kubectl apply -f deployment_cronhpa.yaml

    The deployment_cronhpa.yaml file is defined as follows:

    ---
    apiVersion: apps/v1 
    kind: Deployment
    metadata:
      name: nginx-deployment-basic
      labels:
        app: nginx
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.7.9 # replace it with your exactly <image_name:tags>
            ports:
            - containerPort: 80
    ---
    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: 3
  2. Run the following command to query the number of replicas of the sample application.
    kubectl get deploy nginx-deployment-basic 
    ➜  kubernetes-cronhpa-controller git:(master) ✗ kubectl get deploy nginx-deployment-basic
    NAME                     DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    nginx-deployment-basic   2         2         2            2           9s
  3. Run the kubectl describe cronhpa cronhpa-sample command to query the cronhpa status and verify that cronhpa jobs have been submitted.
    Name:         cronhpa-sample
    Namespace:    default
    Labels:       controller-tools.k8s.io=1.0
    Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                    {"apiVersion":"autoscaling.alibabacloud.com/v1beta1","kind":"CronHorizontalPodAutoscaler","metadata":{"annotations":{},"labels":{"controll...
    API Version:  autoscaling.alibabacloud.com/v1beta1
    Kind:         CronHorizontalPodAutoscaler
    Metadata:
      Creation Timestamp:  2019-04-14T10:42:38Z
      Generation:          1
      Resource Version:    4017247
      Self Link:           /apis/autoscaling.alibabacloud.com/v1beta1/namespaces/default/cronhorizontalpodautoscalers/cronhpa-sample
      UID:                 05e41c95-5ea2-11e9-8ce6-00163e12e274
    Spec:
      Jobs:
        Name:         scale-down
        Schedule:     30 */1 * * * *
        Target Size:  1
        Name:         scale-up
        Schedule:     0 */1 * * * *
        Target Size:  3
      Scale Target Ref:
        API Version:  apps/v1
        Kind:         Deployment
        Name:         nginx-deployment-basic
    Status:
      Conditions:
        Job Id:           38e79271-9a42-4131-9acd-1f5bfab38802
        Last Probe Time:  2019-04-14T10:43:02Z
        Message:
        Name:             scale-down
        Schedule:         30 */1 * * * *
        State:            Submitted
        Job Id:           a7db95b6-396a-4753-91d5-23c2e73819ac
        Last Probe Time:  2019-04-14T10:43:02Z
        Message:
        Name:             scale-up
        Schedule:         0 */1 * * * *
        State:            Submitted
    Events:               <none>
    Note You can download the code used in the preceding example at Examples.
  1. Run the following command to deploy a sample application, HPA, and cronhpa.
    kubectl apply -f deployment_cronhpa_hpa.yaml

    The deployment_cronhpa_hpa.yaml file is defined as follows:

    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment-basic
      labels:
        app: nginx
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.7.9 # replace it with your exactly <image_name:tags>
            resources:
              requests:
                cpu: 50m
                memory: 60m
            ports:
            - containerPort: 80
    ---
    apiVersion: autoscaling/v1
    kind: HorizontalPodAutoscaler
    metadata:
      name: nginx-deployment-basic-hpa
      namespace: default
    spec:
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: nginx-deployment-basic
      minReplicas: 1
      maxReplicas: 10
      targetCPUUtilizationPercentage: 50
    ---
    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: true
       - name: "scale-up"
         schedule: "0 */1 * * * *"
         targetSize: 3
         runOnce: true
    					
  2. Run the following commands to query the number of replicas of the sample application and the HPA status.
    kubectl get deploy nginx-deployment-basic
    NAME                     DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    nginx-deployment-basic   2         2         2            2           9s
    -------
    kubectl get hpa  nginx-deployment-basic-hpa
    NAME                         REFERENCE                           TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    nginx-deployment-basic-hpa   Deployment/nginx-deployment-basic   0%/50%    1         10        2          150m
  3. Run the kubectl describe cronhpa cronhpa-sample command to query the cronhpa status.
    Name:         cronhpa-sample
    Namespace:    default
    Labels:       controller-tools.k8s.io=1.0
    Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                    {"apiVersion":"autoscaling.alibabacloud.com/v1beta1","kind":"CronHorizontalPodAutoscaler","metadata":{"annotations":{},"labels":{"contro
    ll...
    API Version:  autoscaling.alibabacloud.com/v1beta1
    Kind:         CronHorizontalPodAutoscaler
    Metadata:
      Creation Timestamp:  2020-04-15T06:02:26Z
      Generation:          32
      Resource Version:    45484074
      Self Link:           /apis/autoscaling.alibabacloud.com/v1beta1/namespaces/default/cronhorizontalpodautoscalers/cronhpa-sample
      UID:                 ae9c547e-7ede-11ea-8a4c-00163e080212
    Spec:
      Exclude Dates:  <nil>
      Jobs:
        Name:         scale-down
        Run Once:     false
        Schedule:     30 */1 * * * *
        Target Size:  1
        Name:         scale-up
        Run Once:     false
        Schedule:     0 */1 * * * *
        Target Size:  3
      Scale Target Ref:
        API Version:  autoscaling/v1
        Kind:         HorizontalPodAutoscaler
        Name:         nginx-deployment-basic-hpa
    Status:
      Conditions:
        Job Id:           6d5dca76-d120-4d2a-b154-2c39a6650241
        Last Probe Time:  2020-04-15T09:36:30Z
        Message:          cron hpa job scale-down executed successfully. Skip scale replicas because HPA nginx-deployment-basic-hpa current replicas:3 >= de
    sired replicas:1.
        Name:             scale-down
        Run Once:         false
        Schedule:         30 */1 * * * *
        State:            Succeed
        Target Size:      1
        Job Id:           26a6176f-c476-4efa-bcf9-3388c6fd6059
        Last Probe Time:  2020-04-15T09:36:00Z
        Message:          cron hpa job scale-up executed successfully. current replicas:2, desired replicas:3.
        Name:             scale-up
        Run Once:         false
        Schedule:         0 */1 * * * *
        State:            Succeed
        Target Size:      3
      Exclude Dates:      <nil>
      Scale Target Ref:
        API Version:  autoscaling/v1
        Kind:         HorizontalPodAutoscaler
        Name:         nginx-deployment-basic-hpa
    Events:
      Type    Reason   Age                  From                            Message
      ----    ------   ----                 ----                            -------
      Normal  Succeed  31s (x4 over 3h33m)  cron-horizontal-pod-autoscaler  cron hpa job scale-up executed successfully. current replicas:2, desired replica
    s:3.
      Normal  Succeed  1s (x8 over 121m)    cron-horizontal-pod-autoscaler  cron hpa job scale-down executed successfully. Skip scale replicas because HPA n
    ginx-deployment-basic-hpa current replicas:3 >= desired replicas:1.

If the State of a cronhpa job is Succeed, it indicates that the last execution of the job was successful. If the State is Submitted, it indicates that the cronhpa job has been submitted to the cron engine but has not been executed. Wait for 30 seconds and query again. If the State of every job is Succeed, it indicates that all job executions are successful.