Updated on 2024-06-17 GMT+08:00

Creating a CronFederatedHPA to Scale Pods at Regular Intervals

This section describes how you can create a CronFederatedHPA so that pods in workloads are automatically scaled in or out at regular intervals.

Before creating a CronFederatedHPA, you must have learnt the basic working principle and concepts of CronFederatedHPA (How CronFederatedHPA Works). To know the differences between the FederatedHPA and CronFederatedHPA, see Overview.

Constraints

CronFederatedHPA can be configured only for clusters 1.19 or later.

Creating a CronFederatedHPA

Using the console

  1. Log in to the UCS console and choose Fleets in the navigation pane.
  2. Click the name of the fleet with federation enabled.
  3. Choose Workload Scaling in the navigation pane and click the Scheduled Policies tab. Then click Create Scheduled Policy in the upper right corner.
  4. Configure parameters for the CronFederatedHPA by referring to Table 1.

    Table 1 Basic parameters

    Parameter

    Description

    Policy Name

    Enter a name for the CronFederatedHPA.

    Namespace

    Select the namespace for the workload for which you want to configure automatic scaling.

    Object

    Select Workloads or Metric-based Policy.

    • Workloads: Select or create a workload you will associate the policy with. For details, see Creating a Workload.
    • Metric-based Policy: Select an existing metric-based policy or click Create Metric-based Policy on the right to create one. For details, see Creating a FederatedHPA.

  5. Click Add Rule in Policy Settings. In the displayed dialog box, configure parameters by referring to Table 2.

    Table 2 Parameters for adding a rule

    Parameter

    Description

    Rule Name

    Enter a name for the CronFederatedHPA.

    Expected Copies

    Enter the desired number of pods scaled when the CronFederatedHPA is triggered.

    Triggered

    Select Hourly, Daily, Weekly, Monthly, Yearly, or Cron.

    • Hourly: a specific minute in an hour when the policy is executed. For example, if you select 5, the policy is executed at the fifth minute of every hour.
    • Daily: a specific minute every day when the policy is executed.
    • Weekly: a specific minute on a day of each week when the policy is executed.
    • Monthly: a specific minute on a day of each month when the policy is executed.
    • Yearly: a specific minute on a day of a month in each year when the policy is executed.
    • Cron:

      Cron expression syntax:

     ┌───────────── Minute (0 to 59)
     │ ┌───────────  Hour (0 to 23)
     │ │ ┌────────── A day in a month (1 to 31)
     │ │ │ ┌────────  Month (1 to 12)
     │ │ │ │ ┌─────── A day in a week (0 to 6)
     │ │ │ │ │ 
     │ │ │ │ │                           
     *  *  *  *  * 

    For example, 0 0 13 * 5 indicates that a task is started at 00:00 on every Friday and the 13th day of each month.

    Time Zone

    Select Shanghai or Singapore.

  6. Click OK and then click Create.

    In the displayed policy list, you can view the policy details.

Using kubectl

  1. Use kubectl to connect to the federation. For details, see Using kubectl to Connect to a Federation.
  2. Create and edit a cfhpa.yaml file.

    vi cfhpa.yaml

    For details about the parameters in this file, see Table 3. In this example, the CronFederatedHPA named cron-federated-hpa is used for the test workload and contains two rules (Scale-Up and Scale-Down) for scheduled scaling. Scale-Up specifies that 10 pods are scaled out at 8:30 daily, and Scale-Down specifies that 5 pods are scaled in at 21:00 daily.

    apiVersion: autoscaling.karmada.io/v1alpha1 
    kind: CronFederatedHPA 
    metadata: 
      name: cron-federated-hpa           # CronFederatedHPA name
    spec: 
      scaleTargetRef: 
        apiVersion: apps/v1 
        kind: Deployment                 # Select Deployment or FederatedHPA.
        name: test                       # Name of the workload or FederatedHPA
      rules: 
      - name: "Scale-Up"                 # Rule name
        schedule: 30 08 * * *            # Time when the policy is triggered
        targetReplicas: 10               # Desired number of pods, which is a non-negative integer
        timeZone: Asia/Shanghai          # Time zone
      - name: "Scale-Down"               # Rule name
        schedule: 0 21 * * *             # Time when the policy is triggered
        targetReplicas: 5                # Desired number of pods, which is a non-negative integer
        timeZone: Asia/Shanghai          # Time zone
    Table 3 Key parameters

    Parameter

    Mandatory

    Type

    Description

    kind

    Yes

    String

    Select Deployment or FederatedHPA.

    • Deployment: The CronFederatedHPA is used separately.
    • FederatedHPA: Both FederatedHPA and CronFederatedHPA are used.

    name

    Yes

    String

    Enter the rule name of 1 to 32 characters in the CronFederatedHPA.

    schedule

    Yes

    String

    Time when the policy is triggered. Cron expression syntax:

     ┌───────────── Minute (0 to 59)
     │ ┌───────────  Hour (0 to 23)
     │ │ ┌────────── A day in a month (1 to 31)
     │ │ │ ┌────────  Month (1 to 12)
     │ │ │ │ ┌─────── A day in a week (0 to 6)
     │ │ │ │ │ 
     │ │ │ │ │                           
      *  *  *  *  * 

    For example, 0 0 13 * 5 indicates that a task is started at 00:00 on every Friday and the 13th day of each month.

    targetReplicas

    Yes

    String

    Enter the desired number of pods scaled when the CronFederatedHPA is triggered.

    timeZone

    Yes

    String

    Select Shanghai or Singapore.

    • Shanghai: Asia/Shanghai
    • Singapore: Asia/Singapore

  3. Create a CronFederatedHPA.

    kubectl apply -f cfhpa.yaml

    If information similar to the following is displayed, the policy has been created:

    CronFederatedHPA.autoscaling.karmada.io/cron-federated-hpa created

    You can run the following commands to check the workload scaling:

    • kubectl get deployments: checks the current number of pods in a workload.
    • kubectl describe cronfederatedhpa cron-federated-hpa: views scaling events (latest three records) of the CronFederatedHPA.

    You can run the following commands to manage CronFederatedHPA cron-federated-hpa (replaced with the actual name):

    • kubectl get cronfederatedhpa cron-federated-hpa: obtains the CronFederatedHPA.
    • kubectl edit cronfederatedhpa cron-federated-hpa: updates the CronFederatedHPA.
    • kubectl delete cronfederatedhpa cron-federated-hpa: deletes the CronFederatedHPA.