Autoscaling FederatedHPA with CronFederatedHPA

In Karmada, a CronFederatedHPA scales workload’s replicas (any kinds of workload with scale subresource, e.g. Deployment) or FederatedHPA’s minReplicas/maxReplicas, with the aim of scaling the business in advance to meet a sharp load peak.

CronFederatedHPA is designed to scale resources at a specific time. When a workload is only scaled directly by CronFederatedHPA, its replicas will remain unchanged until the specified time is reached. This means that it loses the ability to handle more requests until then.

So, to ensure that the workload can be scaled up in advance to meet peak loads and real-time business demands later on, we recommend using CronFederatedHPA to scale FederatedHPA firstly. Then, FederatedHPA can scale the workloads based on their metrics.

This document walk you through an example of enabling CronFederatedHPA to a FederatedHPA.

Prerequisites

Karmada has been installed

We can install Karmada by referring to Quick Start, or directly run hack/local-up-karmada.sh script which is also used to run our E2E cases.

Deploy workload in member1 and member2 cluster

We need to deploy deployment(2 replica) in member1 and member2:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx
  5. labels:
  6. app: nginx
  7. spec:
  8. replicas: 2
  9. selector:
  10. matchLabels:
  11. app: nginx
  12. template:
  13. metadata:
  14. labels:
  15. app: nginx
  16. spec:
  17. containers:
  18. - image: nginx
  19. name: nginx
  20. resources:
  21. requests:
  22. cpu: 25m
  23. memory: 64Mi
  24. limits:
  25. cpu: 25m
  26. memory: 64Mi
  27. ---
  28. apiVersion: policy.karmada.io/v1alpha1
  29. kind: PropagationPolicy
  30. metadata:
  31. name: nginx-propagation
  32. spec:
  33. resourceSelectors:
  34. - apiVersion: apps/v1
  35. kind: Deployment
  36. name: nginx
  37. placement:
  38. clusterAffinity:
  39. clusterNames:
  40. - member1
  41. - member2
  42. replicaScheduling:
  43. replicaDivisionPreference: Weighted
  44. replicaSchedulingType: Divided
  45. weightPreference:
  46. staticWeightList:
  47. - targetCluster:
  48. clusterNames:
  49. - member1
  50. weight: 1
  51. - targetCluster:
  52. clusterNames:
  53. - member2
  54. weight: 1

After deploying, you can check the created pods:

  1. $ karmadactl get pods
  2. NAME CLUSTER READY STATUS RESTARTS AGE
  3. nginx-777bc7b6d7-cmt6k member1 1/1 Running 0 27m
  4. nginx-777bc7b6d7-8lmcg member2 1/1 Running 0 27m

Deploy FederatedHPA in Karmada control plane

Let’s create a FederatedHPA in the Karmada control plane to manage the cross-cluster nginx deployment:

  1. apiVersion: autoscaling.karmada.io/v1alpha1
  2. kind: FederatedHPA
  3. metadata:
  4. name: nginx-fhpa
  5. spec:
  6. scaleTargetRef:
  7. apiVersion: apps/v1
  8. kind: Deployment
  9. name: nginx
  10. minReplicas: 1
  11. maxReplicas: 10
  12. behavior:
  13. scaleDown:
  14. stabilizationWindowSeconds: 10
  15. scaleUp:
  16. stabilizationWindowSeconds: 10
  17. metrics:
  18. - type: Resource
  19. resource:
  20. name: cpu
  21. target:
  22. type: Utilization
  23. averageUtilization: 80

The FederatedHPA will scale up the workload’s replicas when the average CPU utilization exceeds 80%. Conversely, if the average CPU utilization falls below 80%, the workload’s replicas will be scaled down.

Deploy CronFederatedHPA in Karmada control plane

To autoscale the minReplicas of FederatedHPA, let’s create CronFederatedHPA in the Karmada control plane:

  1. apiVersion: autoscaling.karmada.io/v1alpha1
  2. kind: CronFederatedHPA
  3. metadata:
  4. name: nginx-cronfhpa
  5. namespace: default
  6. spec:
  7. scaleTargetRef:
  8. apiVersion: autoscaling.karmada.io/v1alpha1
  9. kind: FederatedHPA
  10. name: nginx-fhpa
  11. rules:
  12. - name: "scale-up"
  13. schedule: "*/1 * * * *"
  14. targetMinReplicas: 5

The spec.schedule follows the following format:

  1. # ┌───────────── minute (0 - 59)
  2. # │ ┌───────────── hour (0 - 23)
  3. # │ │ ┌───────────── day of the month (1 - 31)
  4. # │ │ │ ┌───────────── month (1 - 12)
  5. # │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
  6. # │ │ │ │ │ 7 is also Sunday on some systems)
  7. # │ │ │ │ │ OR sun, mon, tue, wed, thu, fri, sat
  8. # │ │ │ │ │
  9. # * * * * *

The expression */1 * * * * means that the FederatedHPA’s minReplicas should be updated to 5 every minute. This ensures that the workload’s replicas will be scaled up to at least 5 in order to handle sudden load peaks.

Test scaling up

After one minute, the minReplicas of FederatedHPA is updated to 5 by CronFederatedHPA. This triggers the scaling up of replicas for the nginx deployment to 5. Let’s now check the number of pods to verify if the scaling has been done as expected:

  1. $ karmadactl get po
  2. NAME CLUSTER READY STATUS RESTARTS AGE
  3. nginx-777bc7b6d7-7vl2r member1 1/1 Running 0 59s
  4. nginx-777bc7b6d7-cmt6k member1 1/1 Running 0 27m
  5. nginx-777bc7b6d7-pc5dk member1 1/1 Running 0 59s
  6. nginx-777bc7b6d7-8lmcg member2 1/1 Running 0 27m
  7. nginx-777bc7b6d7-pghl7 member2 1/1 Running 0 59s

And if the business demand is needs more replicas, FederatedHPA will scale the replicas up based on the metrics (.e.g. cpu utilization).

And checking the CronFederatedHPA’s status field, you can see the scaling history:

  1. $ kubectl --kubeconfig $HOME/.kube/karmada.config --context karmada-apiserver get cronfhpa/nginx-cronfhpa -oyaml
  2. apiVersion: autoscaling.karmada.io/v1alpha1
  3. kind: CronFederatedHPA
  4. metadata:
  5. name: nginx-cronfhpa
  6. namespace: default
  7. spec:
  8. rules:
  9. - failedHistoryLimit: 3
  10. name: scale-up
  11. schedule: '*/1 * * * *'
  12. successfulHistoryLimit: 3
  13. suspend: false
  14. targetMinReplicas: 5
  15. scaleTargetRef:
  16. apiVersion: autoscaling.karmada.io/v1alpha1
  17. kind: FederatedHPA
  18. name: nginx-fhpa
  19. status:
  20. executionHistories:
  21. - nextExecutionTime: "2023-07-29T07:53:00Z" # The next expected scaling time
  22. ruleName: scale-up
  23. successfulExecutions:
  24. - appliedMinReplicas: 5 # CronFederatedHPA updates the minReplicas to 5
  25. executionTime: "2023-07-29T07:52:00Z" # The actual scaling time
  26. scheduleTime: "2023-07-29T07:52:00Z" # The last expected scaling time

The scaling history includes information about both successful and failed scaling operations.