Sub topic 7
Configuring Prometheus to Scrape Targets
Assume you can edit YAML, manage systemd services, and reach HTTP endpoints over TCP. This chapter configures Prometheus to collect metrics from Prometheus itself and two Linux servers running node_exporter.
How a scrape job works
A scrape job tells Prometheus where to request metrics. Prometheus periodically sends an HTTP request to each target's /metrics endpoint. The target returns text in the Prometheus exposition format. Prometheus stores the samples with labels such as job and instance.
A target address includes a host name or IP address and a port. node_exporter normally listens on port 9100.
| Setting | Purpose |
|---|---|
| `job_name` | Groups similar targets and sets the `job` label |
| `scrape_interval` | Controls how often Prometheus collects samples |
| `static_configs` | Lists fixed target addresses |
| `targets` | Contains host and port pairs |
Create the scrape configuration
Edit the Prometheus configuration file, commonly /etc/prometheus/prometheus.yml:
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- localhost:9090
- job_name: node
scrape_interval: 30s
static_configs:
- targets:
- server1.example.com:9100
- server2.example.com:9100The global interval applies to every job unless a job provides its own interval. Here, Prometheus scrapes itself every 15 seconds and the Linux servers every 30 seconds.
Prometheus creates an instance label from each target address. For example, a sample from server1.example.com:9100 has labels similar to job="node" and instance="server1.example.com:9100".
Keep target addresses resolvable from the Prometheus host. A name that works in your interactive shell might not work for the Prometheus service if DNS, /etc/hosts, routing, or firewall rules differ.
Validate and reload
Validate the file before applying it. The exact command depends on how Prometheus was installed, but the Prometheus binary provides a configuration check:
promtool check config /etc/prometheus/prometheus.ymlA successful check reports that the syntax is valid. Fix indentation, quoting, and spelling errors before reloading.
If Prometheus runs under systemd, restart it when a reload endpoint is not configured:
sudo systemctl restart prometheus
sudo systemctl status prometheus --no-pagerA restart interrupts collection briefly. In a production setup, enable Prometheus's reload mechanism and send SIGHUP or use its reload endpoint according to the way the service was installed.
Check target health
Open Prometheus's Status and Targets page, usually at http://prometheus.example.com:9090/targets. Each target should show UP, its last scrape time, and its scrape duration.
You can also query the built-in up metric:
up{job="node"}The value is 1 when the last scrape succeeded and 0 when it failed. A query returning no series usually indicates a configuration, reload, or label-selection problem rather than a failed scrape.
Exercise
What does up{job="node"} == 0 tell you?
Show answer
Prometheus knows about the node target, but its most recent scrape failed. Check the target page, DNS, network access, port 9100, and the exporter service logs.