2 min read | by Jordi Prats
Since Kubernetes v1.23 we can now switch to autoscaling/v2, it provides a nicer way of setting the metrics it's going to use to autoscale replicas: External metrics becomes a first-class citizen
With the previous API version (autoscaling/v1) there wasn't a direct way of configuring external metrics since it's attributes were just intended for the CPU/memory use case:
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: demo-targetCPUUtilizationPercentage
spec:
maxReplicas: 1
minReplicas: 2
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: demo-autoscale
targetCPUUtilizationPercentage: 50
With the introduction of autoscaling/v2beta1 we could configure external metrics (Prometheus, Datadog): It was using annotations to be able to keep using the autoscaling/v1 for persistence
With the introduction of autoscaling/v2 we have the spec.metrics attribute that allows us to use metrics from different sources to configure it. We can adapt the previous example (autoscaling/v1) to use the new API version as follows:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: demo-targetCPUUtilizationPercentage
spec:
maxReplicas: 1
minReplicas: 2
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: demo-autoscale
metrics:
- resource:
name: cpu
target:
averageUtilization: 80
type: Utilization
type: Resource
If we need use an external metric, we can set it in a similar way we configure the CPU and memory metrics:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: demo-externalmetrics
spec:
minReplicas: 2
maxReplicas: 22
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: demo-autoscale
metrics:
- type: External
external:
metric:
name: kubernetes.kubelet.cpu.usage
selector:
matchLabels:
cluster_name: example
target:
averageValue: "50"
type: AverageValue
Posted on 22/06/2022