Home > Enterprise >  Can we have multiple targets in K8s Horizontal Pod Autoscaler?
Can we have multiple targets in K8s Horizontal Pod Autoscaler?

Time:03-10

We are considering to use HPA to scale number of pods in our cluster. This is how a typical hpa object would like:

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
 name: hpa-demo
 namespace: default
spec:
 scaleTargetRef:
   apiVersion: apps/v1
   kind: Deployment
   name: hpa-deployment
 minReplicas: 1
 maxReplicas: 10
 targetCPUUtilizationPercentage: 20

My question is - Can we have multiple targets (scaleTargetRef) for HPA ? Or each deployment/RS/SS etc has to have its own HPA?

Tried to look into K8s doco but could not find any info on this. Any help appreciated, thanks.

https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/#support-for-metrics-apis

https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/

CodePudding user response:

Can we have multiple targets (scaleTargetRef) for HPA ?

One HorizontalPodAutoscaler has only one scaleTargetRef that hold one referred resource only.

CodePudding user response:

HorizontalPodAutoscaler controls the scale of a single resource - Deployment/StatefulSet/ReplicaSet. It is actually stated in documentation, though not that directly:

HorizontalPodAutoscaler controls the scale of a Deployment and its ReplicaSet

Here there is a reference to it as well - single target resource is defined by the scaleTargetRef, horizontal pod autoscaler learns the current resource consumption for it and will set the desired number of pods by using its Scale subresource.

From practical experience, reference for multiple workload resources in a single HorizontalPodAutoscaler definition will work for only one of them. In addition, when applying kubectl autoscale command with several resources to create a HorizontalPodAutoscaler object, separate hpa object will be created for each of them.

  • Related