- FederatedHPA scales with custom metrics
- Prerequisites
- Deploy workload in
member1andmember2cluster - Monitor your application in
member1andmember2cluster - Launch you adapter in
member1andmember2cluster - Register metrics API in
member1andmember2cluster - Deploy FederatedHPA in Karmada control plane
- Export service to
member1cluster - Install hey http load testing tool in member1 cluster
- Test scaling up
- Test scaling down
FederatedHPA scales with custom metrics
In Karmada, a FederatedHPA scales up/down the workload’s replicas across multiple clusters, with the aim of automatically scaling the workload to match the demand.
FederatedHPA not only supports resource metrics such as CPU and memory, but also supports custom metrics which may expand the use cases of FederatedHPA.
This document walks you through an example of enabling FederatedHPA to automatically manage scale for a cross-cluster app with custom metrics.
The walkthrough example will do as follows:

- One sample-deployment’s pod exists in
member1cluster. - The service is deployed in
member1andmember2cluster. - Request the multi-cluster service and trigger an increase in the pod’s custom metrics(http_requests_total).
- The replicas will be scaled up in
member1andmember2cluster.
Prerequisites
Karmada has been installed
You 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.
Member Cluster Network
Ensure that at least two clusters have been added to Karmada, and the container networks between member clusters are connected.
- If you use the
hack/local-up-karmada.shscript to deploy Karmada, Karmada will have three member clusters, and the container networks of themember1andmember2will be connected. - You can use
Submarineror other related open source projects to connect networks between member clusters.
Note: In order to prevent routing conflicts, Pod and Service CIDRs of clusters need non-overlapping.
The ServiceExport and ServiceImport CRDs have been installed
You need to install ServiceExport and ServiceImport in the member clusters to enable multi-cluster service.
After ServiceExport and ServiceImport have been installed on the Karmada Control Plane, you can create ClusterPropagationPolicy to propagate those two CRDs to the member clusters.
# propagate ServiceExport CRDapiVersion: policy.karmada.io/v1alpha1kind: ClusterPropagationPolicymetadata:name: serviceexport-policyspec:resourceSelectors:- apiVersion: apiextensions.k8s.io/v1kind: CustomResourceDefinitionname: serviceexports.multicluster.x-k8s.ioplacement:clusterAffinity:clusterNames:- member1- member2---# propagate ServiceImport CRDapiVersion: policy.karmada.io/v1alpha1kind: ClusterPropagationPolicymetadata:name: serviceimport-policyspec:resourceSelectors:- apiVersion: apiextensions.k8s.io/v1kind: CustomResourceDefinitionname: serviceimports.multicluster.x-k8s.ioplacement:clusterAffinity:clusterNames:- member1- member2
prometheus and prometheus-adapter have been installed in member clusters
You need to install prometheus and prometheus-adapter for member clusters to provide the custom metrics. You can install it by running the following in member clusters:
git clone https://github.com/prometheus-operator/kube-prometheus.gitcd kube-prometheuskubectl apply --server-side -f manifests/setupkubectl wait \--for condition=Established \--all CustomResourceDefinition \--namespace=monitoringkubectl apply -f manifests/
You can verify the installation by the following command:
$ kubectl --kubeconfig=/root/.kube/members.config --context=member1 get po -nmonitoringNAME READY STATUS RESTARTS AGEalertmanager-main-0 2/2 Running 0 30halertmanager-main-1 2/2 Running 0 30halertmanager-main-2 2/2 Running 0 30hblackbox-exporter-6bc47b9578-zcbb7 3/3 Running 0 30hgrafana-6b68cd6b-vmw74 1/1 Running 0 30hkube-state-metrics-597db7f85d-2hpfs 3/3 Running 0 30hnode-exporter-q8hdx 2/2 Running 0 30hprometheus-adapter-57d9587488-86ckj 1/1 Running 0 29hprometheus-adapter-57d9587488-zrt29 1/1 Running 0 29hprometheus-k8s-0 2/2 Running 0 30hprometheus-k8s-1 2/2 Running 0 30hprometheus-operator-7d4b94944f-kkwkk 2/2 Running 0 30h
karmada-metrics-adapter has been installed in Karmada control plane
You need to install karmada-metrics-adapter in Karmada control plane to provide the metrics API, install it by running:
hack/deploy-metrics-adapter.sh ${host_cluster_kubeconfig} ${host_cluster_context} ${karmada_apiserver_kubeconfig} ${karmada_apiserver_context_name}
If you use the hack/local-up-karmada.sh script to deploy Karmada, karmada-metrics-adapter will be installed by default.
Deploy workload in member1 and member2 cluster
You need to deploy a sample deployment(1 replica) and service in member1 and member2.
apiVersion: apps/v1kind: Deploymentmetadata:name: sample-applabels:app: sample-appspec:replicas: 1selector:matchLabels:app: sample-apptemplate:metadata:labels:app: sample-appspec:containers:- image: luxas/autoscale-demo:v0.1.2name: metrics-providerports:- name: httpcontainerPort: 8080---apiVersion: v1kind: Servicemetadata:labels:app: sample-appname: sample-appspec:ports:- name: httpport: 80protocol: TCPtargetPort: 8080selector:app: sample-apptype: ClusterIP---apiVersion: policy.karmada.io/v1alpha1kind: PropagationPolicymetadata:name: app-propagationspec:resourceSelectors:- apiVersion: apps/v1kind: Deploymentname: sample-app- apiVersion: v1kind: Servicename: sample-appplacement:clusterAffinity:clusterNames:- member1- member2replicaScheduling:replicaDivisionPreference: WeightedreplicaSchedulingType: DividedweightPreference:staticWeightList:- targetCluster:clusterNames:- member1weight: 1- targetCluster:clusterNames:- member2weight: 1
After deploying, you can check the distribution of the pods and service:
$ karmadactl get podsNAME CLUSTER READY STATUS RESTARTS AGEsample-app-9b7d8c9f5-xrnfx member1 1/1 Running 0 111s$ karmadactl get svcNAME CLUSTER TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ADOPTIONsample-app member1 ClusterIP 10.11.29.250 <none> 80/TCP 3m53s Y
Monitor your application in member1 and member2 cluster
In order to monitor your application, you’ll need to set up a ServiceMonitor pointing at the application. Assuming you’ve set up your Prometheus instance to use ServiceMonitors with the app: sample-app label, create a ServiceMonitor to monitor the app’s metrics via the service:
apiVersion: monitoring.coreos.com/v1kind: ServiceMonitormetadata:name: sample-applabels:app: sample-appspec:selector:matchLabels:app: sample-appendpoints:- port: http
kubectl create -f sample-app.monitor.yaml
Now, you should see your metrics (http_requests_total) appear in your Prometheus instance. Look them up via the dashboard, and make sure they have the namespace and pod labels. If not, check the labels on the service monitor match the ones on the Prometheus CRD.
Launch you adapter in member1 and member2 cluster
After you deploy prometheus-adapter, you need to update to the adapter config which is necessary in order to expose custom metrics.
apiVersion: v1kind: ConfigMapmetadata:name: adapter-confignamespace: monitoringdata:config.yaml: |-"rules":- "seriesQuery": |{namespace!="",__name__!~"^container_.*"}"resources":"template": "<<.Resource>>""name":"matches": "^(.*)_total""as": """metricsQuery": |sum by (<<.GroupBy>>) (irate (<<.Series>>{<<.LabelMatchers>>}[1m]))
kubectl apply -f prom-adapter.config.yaml# Restart prom-adapter podskubectl rollout restart deployment prometheus-adapter -n monitoring
Register metrics API in member1 and member2 cluster
You also need to register the custom metrics API with the API aggregator (part of the main Kubernetes API server). For that you need to create an APIService resource.
apiVersion: apiregistration.k8s.io/v1kind: APIServicemetadata:name: v1beta2.custom.metrics.k8s.iospec:group: custom.metrics.k8s.iogroupPriorityMinimum: 100insecureSkipTLSVerify: trueservice:name: prometheus-adapternamespace: monitoringversion: v1beta2versionPriority: 100
kubectl create -f api-service.yaml
The API is registered as custom.metrics.k8s.io/v1beta2, and you can use the following command to verify:
kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta2/namespaces/default/pods/*/http_requests?selector=app%3Dsample-app"
The output is similar to:
{"kind": "MetricValueList","apiVersion": "custom.metrics.k8s.io/v1beta2","metadata": {},"items": [{"describedObject": {"kind": "Pod","namespace": "default","name": "sample-app-9b7d8c9f5-9lw6b","apiVersion": "/v1"},"metric": {"name": "http_requests","selector": null},"timestamp": "2023-06-14T09:09:54Z","value": "66m"}]}
If karmada-metrics-adapter is installed successfully, you can also verify it with the above command in Karmada control plane.
Deploy FederatedHPA in Karmada control plane
Then let’s deploy FederatedHPA in Karmada control plane.
apiVersion: autoscaling.karmada.io/v1alpha1kind: FederatedHPAmetadata:name: sample-appspec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: sample-appminReplicas: 1maxReplicas: 10behavior:scaleDown:stabilizationWindowSeconds: 10scaleUp:stabilizationWindowSeconds: 10metrics:- type: Podspods:metric:name: http_requeststarget:averageValue: 700mtype: Value
After deploying, you can check the FederatedHPA:
NAME REFERENCE-KIND REFERENCE-NAME MINPODS MAXPODS REPLICAS AGEsample-app Deployment sample-app 1 10 1 15d
Export service to member1 cluster
As mentioned before, you need a multi-cluster service to route the requests to the pods in member1 and member2 cluster, so let create this mult-cluster service.
Create a
ServiceExportobject on Karmada Control Plane, and then create aPropagationPolicyto propagate theServiceExportobject tomember1andmember2cluster.apiVersion: multicluster.x-k8s.io/v1alpha1kind: ServiceExportmetadata:name: sample-app---apiVersion: policy.karmada.io/v1alpha1kind: PropagationPolicymetadata:name: serve-export-policyspec:resourceSelectors:- apiVersion: multicluster.x-k8s.io/v1alpha1kind: ServiceExportname: sample-appplacement:clusterAffinity:clusterNames:- member1- member2
Create a
ServiceImportobject on Karmada Control Plane, and then create aPropagationPolicyto propagate theServiceImportobject tomember1cluster.apiVersion: multicluster.x-k8s.io/v1alpha1kind: ServiceImportmetadata:name: sample-appspec:type: ClusterSetIPports:- port: 80protocol: TCP---apiVersion: policy.karmada.io/v1alpha1kind: PropagationPolicymetadata:name: serve-import-policyspec:resourceSelectors:- apiVersion: multicluster.x-k8s.io/v1alpha1kind: ServiceImportname: sample-appplacement:clusterAffinity:clusterNames:- member1
After deploying, you can check the multi-cluster service:
$ karmadactl get svcNAME CLUSTER TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ADOPTIONderived-sample-app member1 ClusterIP 10.11.59.213 <none> 80/TCP 9h Y
Install hey http load testing tool in member1 cluster
In order to do http requests, here you can use hey.
- Download
heyand copy it to kind cluster container.
wget https://hey-release.s3.us-east-2.amazonaws.com/hey_linux_amd64chmod +x hey_linux_amd64docker cp hey_linux_amd64 member1-control-plane:/usr/local/bin/hey
Test scaling up
Check the pod distribution firstly.
$ karmadactl get podsNAME CLUSTER READY STATUS RESTARTS AGEsample-app-9b7d8c9f5-xrnfx member1 1/1 Running 0 111s
Check multi-cluster service ip.
$ karmadactl get svcNAME CLUSTER TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ADOPTIONderived-sample-app member1 ClusterIP 10.11.59.213 <none> 80/TCP 20m Y
Request multi-cluster service with hey to increase the nginx pods’ custom metrics(http_requests_total).
docker exec member1-control-plane hey -c 1000 -z 1m http://10.11.59.213/metrics
Wait 15s, the replicas will be scaled up, then you can check the pod distribution again.
$ karmadactl get po -l app=sample-appNAME CLUSTER READY STATUS RESTARTS AGEsample-app-9b7d8c9f5-454vz member2 1/1 Running 0 84ssample-app-9b7d8c9f5-7fjhn member2 1/1 Running 0 69ssample-app-9b7d8c9f5-ddf4s member2 1/1 Running 0 69ssample-app-9b7d8c9f5-mxqmh member2 1/1 Running 0 84ssample-app-9b7d8c9f5-qbc2j member2 1/1 Running 0 69ssample-app-9b7d8c9f5-2tgxt member1 1/1 Running 0 69ssample-app-9b7d8c9f5-66n9s member1 1/1 Running 0 69ssample-app-9b7d8c9f5-fbzps member1 1/1 Running 0 84ssample-app-9b7d8c9f5-ldmhz member1 1/1 Running 0 84ssample-app-9b7d8c9f5-xrnfx member1 1/1 Running 0 87m
Test scaling down
After 1 minute, the load testing tool will be stopped, then you can see the workload is scaled down across clusters.
$ karmadactl get pods -l app=sample-appNAME CLUSTER READY STATUS RESTARTS AGEsample-app-9b7d8c9f5-xrnfx member1 1/1 Running 0 91m