Professional Documents
Culture Documents
AutoScaling in Kubernetes With Horizontal Pod Autoscaler Using Custom Metrics From Prometheus - by Dhruvin Soni - Apr, 2024 - Towards Dev
AutoScaling in Kubernetes With Horizontal Pod Autoscaler Using Custom Metrics From Prometheus - by Dhruvin Soni - Apr, 2024 - Towards Dev
Open in app
Search
Get unlimited access to the best of Medium for less than $1/week. Become a member
KED
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad8… 1/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
scale out (add more pods) when the load increases and scale in (remove pods) when
the load decreases, ensuring that applications maintain performance without
wasting resources.
In this article, we will deploy YACE CloudWatch Exporter to get data from AWS
CloudWatch which exports data into Prometheus format. Once we have data in
Prometheus we can use any available metrics to scale our deployment.
Prerequisites:
eksctl , helm & awscli installed on the server
Set your cluster’s OIDC identity provider to an environment variable with the
following command.
Set variables for the namespace and name of the service account. We will deploy
YACE in default namespace and we will configure YACE to create a service account
named yace-cw-exporter
export namespace=default
export service_account=yace-cw-exporter
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad8… 3/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad8… 4/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
It uses the Prometheus operator to install Prometheus and it has custom CRDs to
read data exported in Prometheus format.
prometheus:
prometheusSpec:
ruleSelectorNilUsesHelmValues: false
serviceMonitorSelectorNilUsesHelmValues: false
podMonitorSelectorNilUsesHelmValues: false
probeSelectorNilUsesHelmValues: false
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad8… 5/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
KEDA provides a wide range of event sources and supports custom metrics,
making it a flexible option for autoscaling based on external metrics. KEDA can
work in conjunction with HPA, allowing you to use external metrics from event
sources to trigger scaling actions with HPA.
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad8… 6/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
Now we will get the default chart values and we need to do some modifications
to give AWS CloudWatch access using IRSA and create servicemonitor custom
resource.
serviceAccount:
# -- Specifies whether a service account should be created
create: true
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad8… 7/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
serviceMonitor:
# When set true then use a ServiceMonitor to configure scraping
enabled: true
config: |-
apiVersion: v1alpha1
sts-region: ap-south-1
discovery:
exportedTagsOnMetrics:
AWS/SQS:
- Name
jobs:
- type: AWS/SQS
regions:
- ap-south-1
period: 60
length: 60
metrics:
- name: NumberOfMessagesSent
statistics: [Sum,Average]
- name: NumberOfMessagesReceived
statistics: [Sum,Average]
- name: NumberOfMessagesDeleted
statistics: [Sum,Average]
- name: ApproximateAgeOfOldestMessage
statistics: [Sum,Average]
- name: NumberOfEmptyReceives
statistics: [Sum,Average]
- name: SentMessageSize
statistics: [Sum,Average]
- name: ApproximateNumberOfMessagesNotVisible
statistics: [Sum,Average]
- name: ApproximateNumberOfMessagesDelayed
statistics: [Sum,Average]
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad8… 8/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
- name: ApproximateNumberOfMessagesVisible
statistics: [Sum,Average]
The above config is very important. The YACE CloudWatch Exporter will get the
metrics of the service defined in the config. So, you have to add all the required
AWS services and their metrics in the config.
All the configuration options are available here and example configs are
available here.
Deploy the YACE CloudWatch Exporter Helm Chart using the following
command.
mkdir portfolio-app
cd portfolio-app
mkdir helmfile
cd helmfile
apiVersion: v2
name: portfolio-app
description: A Helm chart for portfolio-app
type: application
version: 1.0.0
appVersion: "1.0.0"
replicaCount: 1
image:
repository: nginx
pullPolicy: Always
# Overrides the image tag whose default is the chart appVersion.
tag: "latest"
service:
type: ClusterIP
port: 80
resources:
limits:
memory: 256Mi
requests:
cpu: 100m
memory: 128Mi
keda:
enabled: true
pollingInterval: 30 # This is the interval to check each trigger on. By defau
cooldownPeriod: 60 # The period to wait after the last trigger reported activ
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 10/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
In this values file we have added a few variables that we will use while creating
ScaledObject custom resource provided by KEDA to trigger our deployment
when threshold is crossed.
mkdir templates
cd templates
{{/*
Expand the name of the chart.
*/}}
{{- define "portfolio-app.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}
{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this
If release name contains chart name it will be used as a full name.
*/}}
{{- define "portfolio-app.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 11/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "portfolio-app.fullname" . }}
labels:
{{- include "portfolio-app.labels" . | nindent 4 }}
spec:
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 12/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{- include "portfolio-app.selectorLabels" . | nindent 6 }}
template:
metadata:
labels:
{{- include "portfolio-app.labels" . | nindent 8 }}
spec:
containers:
- name: {{ .Chart.Name }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: {{ .Values.service.port }}
protocol: TCP
resources:
{{- toYaml .Values.resources | nindent 12 }}
apiVersion: v1
kind: Service
metadata:
name: {{ include "portfolio-app.fullname" . }}
labels:
{{- include "portfolio-app.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
{{- include "portfolio-app.selectorLabels" . | nindent 4 }}
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 13/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
You can read here for more configuration options of ScaledObject and
configuration options for the Prometheus trigger here.
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 14/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
# Get pod
kubectl get po
# Get service
kubectl get svc
# Get scaledobject
kubectl get scaledobject
# Get HPA
kubectl get hpa
You might have observed in Architecture diagram that KEDA internally creates
native Kubernetes HPA resources.
# If you see following events it means that scaledobject is able to get data fr
Status:
Conditions:
Message: ScaledObject is defined correctly and is ready for scaling
Reason: ScaledObjectReady
Status: True
Type: Ready
Message: Scaling is performed because triggers are active
Reason: ScalerActive
Status: True
Type: Active
Message: No fallbacks are active on this scaled object
Reason: NoFallbackFound
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 15/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
Status: False
Type: Fallback
Status: Unknown
Type: Paused
So, we will send some more sample messages in our SQS queue so that they
cross the threshold of “10.00” (in our example) and we should see a new pod.
QUEUE_URL=<your_queue_url>
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 16/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
Conclusion:
In this article, I walked you through how to scale your Kubernetes Deployment
based on the external metric from Prometheus using KEDA. You can modify the
configs as per your and play with it. You can even use any other service’s metrics to
scale your deployment.
If you found this guide helpful then do click on 👏 the button and also feel free to
drop a comment.
Follow
Senior Cloud Infrastructure Engineer | AWS | Automation | 2x AWS | CKA | Terraform Certified | k8s | Docker |
CI/CD | http://dhsoni.info/
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 17/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
Dhruvin Soni
268
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 18/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
Dependency inversion and dependency injection are two closely related concepts in software
development, often confused due to their…
420 2
186 1
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 19/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
45
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 20/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
Tanmay Bhandge
47
Spacelift in Spacelift
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 21/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
68 2
Lists
Productivity
241 stories · 452 saves
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 22/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
61
24 1
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 23/24
6/11/24, 10:22 PM AutoScaling in Kubernetes with Horizontal Pod Autoscaler using custom Metrics from Prometheus | by Dhru…
16 1
Tejashree Salvi
221 1
https://towardsdev.com/autoscaling-in-kubernetes-with-horizontal-pod-autoscaler-using-custom-metrics-from-prometheus-ad… 24/24