Autoscaling Deployment across clusters with CronFederatedHPA

In Karmada, the CronFederatedHPA is responsible for scaling replicas of workloads (such as Deployments) or the minReplicas/maxReplicas of FederatedHPAs. Its purpose is to proactively scale the business in order to handle sudden spikes in load.

This document provides an example of how to enable CronFederatedHPA for a cross-cluster nginx deployment.

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-rmmzv member1 1/1 Running 0 104s
  4. nginx-777bc7b6d7-9gf7g member2 1/1 Running 0 104s

Deploy CronFederatedHPA in Karmada control plane

Then let’s deploy CronFederatedHPA in Karmada control plane to scale up the Deployment:

  1. apiVersion: autoscaling.karmada.io/v1alpha1
  2. kind: CronFederatedHPA
  3. metadata:
  4. name: nginx-cronfhpa
  5. namespace: default
  6. spec:
  7. scaleTargetRef:
  8. apiVersion: apps/v1
  9. kind: Deployment
  10. name: nginx
  11. rules:
  12. - name: "scale-up"
  13. schedule: "*/1 * * * *"
  14. targetReplicas: 5
  15. suspend: false

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 nginx deployment’s replicas should be updated to 5 every minute. This ensures that the workload’s replicas will be scaled up to 5 in order to handle sudden load peaks.

Testing scaling up

After one minute, the replicas of nginx deployment is scaled to 5 by CronFederatedHPA. Let’s now check the number of pods to verify if the scaling has been done as expected:

  1. $ karmadactl get pods
  2. NAME CLUSTER READY STATUS RESTARTS AGE
  3. nginx-777bc7b6d7-8v9b4 member2 1/1 Running 0 18s
  4. nginx-777bc7b6d7-9gf7g member2 1/1 Running 0 8m2s
  5. nginx-777bc7b6d7-5snhz member1 1/1 Running 0 18s
  6. nginx-777bc7b6d7-rmmzv member1 1/1 Running 0 8m2s
  7. nginx-777bc7b6d7-z9kwg member1 1/1 Running 0 18s

By checking the status field of CronFederatedHPA, you can access 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. targetReplicas: 5
  15. scaleTargetRef:
  16. apiVersion: apps/v1
  17. kind: Deployment
  18. name: nginx
  19. status:
  20. executionHistories:
  21. - nextExecutionTime: "2023-07-29T03:27:00Z" # The next expected scaling time
  22. ruleName: scale-up
  23. successfulExecutions:
  24. - appliedReplicas: 5 # CronFederatedHPA updates the nginx deployment's replicas to 5
  25. executionTime: "2023-07-29T03:26:00Z" # The actual scaling time
  26. scheduleTime: "2023-07-29T03:26:00Z" # The last expected scaling time

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