Prometheus Comprehensive Monitoring Platform - Consul-Based Auto Discovery
Background
Consul Documentation | Consul | HashiCorp Developer
The Prometheus configuration file prometheus-config.yaml contains numerous scraping rules, which are mostly manually managed by DevOps teams. When new nodes or components are added, manual modification of this configuration and a hot reload of Prometheus are required. Is it possible to dynamically monitor microservices? Prometheus provides multiple dynamic service discovery mechanisms, and this guide uses Consul as an example.
Consul-Based Auto Discovery
Consul is a distributed key-value database and a service registry component. Other services can register with Consul, including Prometheus. By leveraging Consul’s service discovery, we can avoid manually specifying large numbers of targets in Prometheus.
The workflow for Prometheus’ Consul-based service discovery is as follows:
- Register or deregister services (monitoring targets) in Consul.
- Prometheus continuously monitors Consul. When changes to services meeting the criteria are detected, Prometheus updates its monitoring targets accordingly.
Service Discovery Mechanisms Supported by Prometheus
Prometheus configuration for data sources falls into two main categories: static configuration and dynamic discovery. Commonly used mechanisms include:
|
|
In Kubernetes monitoring scenarios, frequently updated resources like Pods and Services best demonstrate the advantages of Prometheus’ auto-discovery capabilities.
Working Principles
-
Prometheus queries the configuration information stored in Consul’s KV storage through the Consul API, then extracts service metadata from it;
-
Prometheus uses this information to construct target service URLs and adds them to the service discovery target list;
-
When services are deregistered or become unavailable, Prometheus will automatically remove them from the target list.
Containerized Consul Cluster
For testing and validation purposes only. Not suitable for production use! Production environments must undergo comprehensive cluster-based deployment validation with service process guardianship and monitoring.
Create a single-node Consul cluster:
|
|
Parameter Explanations:
|
|
Validation test:
Access the web UI at, for example: http://192.10.192.109:18500/
|
|
Register Host to Consul
Example: Register node-exporter on a virtual machine to consul.
|
|
Parameter Explanation
|
|
Delete:
|
|
Configuring Prometheus with Consul for Automatic Service Discovery
Modify Prometheus’ ConfigMap configuration file: prometheus-config.yaml
|
|
Reload Prometheus using the above method. Open the Prometheus Target page to see the defined mysql-exporter job:
|
|
Summary
- Dynamic Service Discovery and Monitoring: By integrating with Consul, Prometheus dynamically maintains its target list, ensuring prompt discovery and monitoring as new services are deployed.
- Scalability: Automated service discovery simplifies infrastructure scaling while preserving reliable monitoring availability and performance.
- Seamless Integration: As the service registry, Consul enables Prometheus to integrate seamlessly with other tools in the Consul ecosystem, delivering a comprehensive solution for service infrastructure monitoring and management.
- Self-Healing Capability: Automatic service discovery allows Prometheus to detect infrastructure changes in real time, continuously updating its target list to ensure uninterrupted monitoring data and high performance.