How to supply external metrics into HPA?
The easiest way will be to feed metrics into Prometheus (which is a commonly solved problem), and then setup a Prometheus-based HPA (also a commonly solved problem).
1. Feed own metrics to Prometheus
- Start with Prometheus-Operator to get the cluster itself monitored, and get access to
ServiceMonitor
objects. ServiceMonitors are pointers to services in the cluster. They let your pod's/metrics
endpoint be discovered and scraped by a prometheus server. - Write a pod that reads metrics from your 3rd party API and shows them in own
/metrics
endpoint. This will be the adapter between your API and Prometheus format. There are clients of course: https://github.com/prometheus/client_python#exporting - Write a
Service
of typeClusterIP
that represents your pod. - Write a
ServiceMonitor
that points to a service. - Query your custom metrics thru Prometheus dashboard to ensure this stage is done.
2. Setup Prometheus-based HPA
- Setup Prometheus-Adapter and follow the HPA walkthrough.
- Or follow the guide https://github.com/stefanprodan/k8s-prom-hpa
This looks like a huge pile of work to get the HPA. However, only the adapter pod is a custom part here. Everything else is a standard stack setup in most of the clusters, and you will get many other use cases for it anyways.