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:
apiVersion: apps/v1kind: Deploymentmetadata:name: nginxlabels:app: nginxspec:replicas: 2selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- image: nginxname: nginxresources:requests:cpu: 25mmemory: 64Milimits:cpu: 25mmemory: 64Mi---apiVersion: policy.karmada.io/v1alpha1kind: PropagationPolicymetadata:name: nginx-propagationspec:resourceSelectors:- apiVersion: apps/v1kind: Deploymentname: nginxplacement:clusterAffinity:clusterNames:- member1- member2replicaScheduling:replicaDivisionPreference: WeightedreplicaSchedulingType: DividedweightPreference:staticWeightList:- targetCluster:clusterNames:- member1weight: 1- targetCluster:clusterNames:- member2weight: 1
After deploying, you can check the created pods:
$ karmadactl get podsNAME CLUSTER READY STATUS RESTARTS AGEnginx-777bc7b6d7-rmmzv member1 1/1 Running 0 104snginx-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:
apiVersion: autoscaling.karmada.io/v1alpha1kind: CronFederatedHPAmetadata:name: nginx-cronfhpanamespace: defaultspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: nginxrules:- name: "scale-up"schedule: "*/1 * * * *"targetReplicas: 5suspend: false
The spec.schedule follows the following format:
# ┌───────────── minute (0 - 59)# │ ┌───────────── hour (0 - 23)# │ │ ┌───────────── day of the month (1 - 31)# │ │ │ ┌───────────── month (1 - 12)# │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;# │ │ │ │ │ 7 is also Sunday on some systems)# │ │ │ │ │ OR sun, mon, tue, wed, thu, fri, sat# │ │ │ │ │# * * * * *
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:
$ karmadactl get podsNAME CLUSTER READY STATUS RESTARTS AGEnginx-777bc7b6d7-8v9b4 member2 1/1 Running 0 18snginx-777bc7b6d7-9gf7g member2 1/1 Running 0 8m2snginx-777bc7b6d7-5snhz member1 1/1 Running 0 18snginx-777bc7b6d7-rmmzv member1 1/1 Running 0 8m2snginx-777bc7b6d7-z9kwg member1 1/1 Running 0 18s
By checking the status field of CronFederatedHPA, you can access the scaling history:
$ kubectl --kubeconfig $HOME/.kube/karmada.config --context karmada-apiserver get cronfhpa/nginx-cronfhpa -oyamlapiVersion: autoscaling.karmada.io/v1alpha1kind: CronFederatedHPAmetadata:name: nginx-cronfhpanamespace: defaultspec:rules:- failedHistoryLimit: 3name: scale-upschedule: '*/1 * * * *'successfulHistoryLimit: 3suspend: falsetargetReplicas: 5scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: nginxstatus:executionHistories:- nextExecutionTime: "2023-07-29T03:27:00Z" # The next expected scaling timeruleName: scale-upsuccessfulExecutions:- appliedReplicas: 5 # CronFederatedHPA updates the nginx deployment's replicas to 5executionTime: "2023-07-29T03:26:00Z" # The actual scaling timescheduleTime: "2023-07-29T03:26:00Z" # The last expected scaling time
The scaling history includes information about both successful and failed scaling operations.