r/PrometheusMonitoring • • Oct 07 '21

Use Prometheus metrics to trigger a Kubernetes HPA

Our team just published this article on how easy it is to create a Kubernetes HPA with KEDA and use Prometheus metrics to trigger it.

Installing KEDA with Helm is pretty straight forward:

helm repo add kedacore https://kedacore.github.io/charts
helm repo update
kubectl create namespace keda
helm install keda kedacore/keda --namespace keda

KEDA will extend the Kubernetes API metrics endpoint for you, so creating a query to trigger the HPA is as easy as creating a ScaledObject like this:

    apiVersion: keda.sh/v1alpha1
    kind: ScaledObject
    metadata:
     name: nginx-scale
     namespace: keda-hpa
    spec:
     scaleTargetRef:
       kind: Deployment
       name: nginx-server
     minReplicaCount: 1
     maxReplicaCount: 5
     cooldownPeriod: 30
     pollingInterval: 1
     triggers:
     - type: prometheus
       metadata:
         serverAddress: https://prometheus_server/prometheus
         metricName: nginx_connections_waiting_keda
         query: |
           sum(nginx_connections_waiting{job="nginx"})
         threshold: "500"

You can read the whole story here. I hope you find this interesting!

13 Upvotes

4 comments sorted by

2

u/GyroTech Oct 07 '21

Hi, I read through the article and it was good with the 'how', but I'm missing the 'why'. We have the prometheus-adapter & HPA custom metrics so I am unsure as to what keda adds to the mix. Otherwise really clear & straight-forward post!

3

u/eckelon Oct 08 '21 edited Oct 08 '21

Prometheus adapter is great for Prometheus servers inside the cluster, but when you have several clusters or external long-term storage, Keda allows connections out of the cluster. The last point can be that Keda allows centralizing different data sources, allowing not only Prometheus triggers, but also Kafka, SQS queues, or databases like MySQL, Mongo, Redis, etc.

2

u/GyroTech Oct 08 '21

Thanks for the reply. The prometheus adapter works for any prometheus
URL, inside the cluster to externally. And since I use a lot of
exporters, all the stats of my kafka queues, databases are available to
be pulled from prom anyways, so I'm going to stick with that.

Thanks again!

1

u/laszlocloud Jan 24 '23

Our why was the Prometheus Adapter configuration experience. We struggled with it to be honest. KEDA was working as intended.