Mastering Prometheus Service Discovery: File, DNS, and Consul Integration
This tutorial explains Prometheus service discovery types, why automatic discovery is essential, the scrape lifecycle, and provides step‑by‑step demos of file‑based discovery, Consul registration via Docker‑Compose, JSON API, and command‑line methods with full configuration examples.
Types of Prometheus Service Discovery
File‑based discovery
DNS‑based discovery
API‑based discovery (Kubernetes, Consul, Azure…)
Why Prometheus Needs Automatic Discovery
Prometheus pulls metrics, so it must know target locations. Static configs work for small setups, but dynamic cloud environments require automatic discovery to keep target lists up‑to‑date.
Prometheus Scrape Lifecycle
Discovery → Configuration → Relabel → Scrape → Metrics relabel. During each scrape_interval Prometheus generates a target list, attaches __meta_ labels, sets __scheme__, __address__, __metrics_path__, and applies relabeling and metric_relabel_configs.
File‑Based Service Discovery Demo
Define static targets in a YAML file and reload Prometheus.
vi prometheus.yml
# static config nodes
- job_name: 'nodes'
file_sd_configs:
- files:
- targets/nodes-*.yaml
refresh_interval: 2m
scrape_interval: 15s cat targets/nodes-linux.yaml
- targets:
- monitor.example.com:9100
- node.export1.com:9101
- node.export2.com:9101
- node.export3.com:9101
labels:
app: node-exporter
os: aliyunos3 curl -XPOST monitor.example.com:9090/-/reloadConsul‑Based Service Discovery
Consul provides service registration, health checks, KV store, and multi‑datacenter support.
Deploy Consul with Docker‑Compose
vi docker-compose.yml
version: '3.6'
volumes:
consul_data: {}
networks:
monitoring:
driver: bridge
services:
consul:
image: consul:1.14
volumes:
- ./consul_configs:/consul/config
- consul_data:/consul/data/
networks:
- monitoring
ports:
- 8500:8500
command: ["consul","agent","-dev","-bootstrap","-config-dir","/consul/config","-data-dir","/consul/data","-ui","-log-level","INFO","-bind","127.0.0.1","-client","0.0.0.0"]
consul-exporter:
image: prom/consul-exporter:v0.8.0
networks:
- monitoring
ports:
- 9107:9107
command: ["--consul.server=consul:8500"]
depends_on:
- consulStart with docker-compose up -d and verify the containers.
Register Services in Consul
Register via Consul's HTTP API using a JSON definition.
vi grafana.json
{
"ID":"grafana",
"Name":"grafana",
"Tags":["grafana","v9"],
"Address":"monitor.example.com",
"Port":3000,
"Meta":{"grafana_version":"9"},
"EnableTagOverride":false,
"Check":{
"http":"http://monitor.example.com:3000/metrics",
"interval":"5s",
"Timeout":"5s"
},
"Weights":{"Passing":1,"Warning":1}
} curl -XPUT --data @grafana.json http://monitor.example.com:8500/v1/agent/service/registerConsul adds __meta_consul_* labels that can be relabeled in Prometheus.
# Example Prometheus job using Consul
- job_name: 'nodes'
consul_sd_configs:
- server: "monitor.example.com:8500"
tags: ["nodes"]
refresh_interval: 2m
scrape_interval: 15sReload Prometheus after changes:
curl -XPOST monitor.example.com:9090/-/reloadConsul Command‑Line Registration Demo
Prepare a nodes.json file with multiple services, place it in Consul’s data directory, and run consul reload to apply.
{
"services": [
{
"id":"node.export1.com",
"name":"node.export1.com",
"address":"node.export1.com",
"port":9101,
"tags":["nodes"],
"checks":[{"http":"http://node.export1.com:9101/metrics","interval":"5s"}]
},
{
"id":"node.export2.com",
"name":"node.export2.com",
"address":"node.export2.com",
"port":9101,
"tags":["nodes"],
"checks":[{"http":"http://node.export2.com:9101/metrics","interval":"5s"}]
}
// ... other services ...
]
}After reloading, both Consul and Prometheus display the discovered targets.
Thus the basic demonstration of Prometheus automatic discovery via Consul is complete.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
