Introduction
This article is part of a series on setting up an end-to-end monitoring and alerting stack using Prometheus.
Service discovery (SD) is a mechanism by which the Prometheus monitoring tool can discover monitorable targets automatically. Instead of listing down each and every
target to be scraped in the Prometheus configuration, service discovery acts as a source of targets that Prometheus can query at runtime.
Service discovery becomes crucial when there are dynamically changing hosts, especially in microservices architectures and environments like Kubernetes. In Prometheus parlance,
service discovery is a way of discovering "scrape targets".
For example, pods are created dynamically in Kubernetes as a result of new services being deployed and undeployed, autoscaling events, and errors causing pods to crash and go away.
If you are using Prometheus for scraping pods in such an environment, Prometheus has to know which pods are running and scrapable at any given point in time. The Kubernetes service
discovery pluging enables this. Similarly, there are SD plugins for other common environments.
You can use service discovery in Prometheus with the predefined plugins, or write your own custom ones using file or HTTP, depending on the situation.