Molay
Molay

Reputation: 1404

Kubernetes - HPA metrics - memory & cpu together

Is it possible to keep 'cpu' and 'memory' metrics together as shown below ? This seems to be not working. I tried below script as HPA. But instently pods has grown upto 5. That's not what i was expecting.

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: myservice-metrics
  namespace: myschema
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: myservice
  minReplicas: 1
  maxReplicas: 5
  metrics:
  - type: Resource
    resource:
      name: memory
      targetAverageValue: 500Mi
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 70

If i keep it individually, it is not complaining. Is it the best practice to set both the metrics for a service ? is there any other way to set both the metrics.

apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: myservice-metrics-memory
  namespace: myschema
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: myservice
  minReplicas: 1
  maxReplicas: 3
  metrics:
  - type: Resource
    resource:
      name: memory
      targetAverageValue: 500Mi


apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: myservice-metrics-cpu
  namespace: myschema
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: myservice
  minReplicas: 1
  maxReplicas: 3
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: 70

Upvotes: 1

Views: 3047

Answers (2)

Rifqi
Rifqi

Reputation: 73

https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/horizontal-pod-autoscaler-v2beta2/

metrics is of type []MetricSpec and

the maximum replica count across all metrics will be used

Single file is possible.

Upvotes: 0

Niron Koren
Niron Koren

Reputation: 46

Starting from Kubernetes v1.6 support for scaling based on multiple metrics has been added. I would suggest to try and switch to the autoscaling/v2beta2 API.

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

Upvotes: 1

Related Questions