Installing & running Prometheus
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
Prometheus is a single self-contained binary: no external database, no cluster to stand up before it
scrapes its first target. This page covers getting that binary (or its Docker image) running, the
minimal prometheus.yml it needs, the built-in expression browser used to sanity-check what it has
collected, adding node_exporter as a real scrape target, and writing a first alerting rule.
Installing Prometheus
The binary tarball
Prometheus ships as a single statically linked binary plus a default config file, packaged per OS/ architecture on the downloads page. No package manager or root install is required:
tar xvfz prometheus-*.tar.gz
cd prometheus-*/
./prometheus --config.file=prometheus.yml
# https://prometheus.io/docs/prometheus/latest/installation/
The extracted directory contains the prometheus server binary, the promtool binary (used to check
and unit-test config and rule files), a default prometheus.yml, and a console_libraries/
consoles pair used by the (largely superseded) console templates feature. By default the server
listens on port 9090 and writes its time-series database under ./data.
Docker
The official image is published as prom/prometheus on Docker Hub. Bind-mount a config file over the
image’s default one and publish port 9090:
docker run -d --name prometheus -p 9090:9090 \
-v "$PWD/prometheus.yml:/etc/prometheus/prometheus.yml" \
prom/prometheus
# https://prometheus.io/docs/prometheus/latest/installation/
The container’s entrypoint already points --config.file at /etc/prometheus/prometheus.yml, so the
bind-mounted file is picked up on start. For anything beyond a single local instance — persistent
volumes, resource limits, TLS — see
Containers & Kubernetes Monitoring; for the
docker run and bind-mount options themselves, see Running Containers.
A first prometheus.yml
Configuration is a single YAML file, reloadable without a restart via SIGHUP or the /-/reload
HTTP endpoint (when --web.enable-lifecycle is set). The minimal skeleton scrapes Prometheus itself:
global:
scrape_interval: 15s # how often to scrape targets by default
evaluation_interval: 15s # how often to evaluate rules
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
# https://prometheus.io/docs/prometheus/latest/getting_started/
Every entry under scrape_configs is a job: a named group of one or more targets scraped with the
same interval, timeout, and metric relabeling rules. static_configs is the simplest way to list
targets by hand; real deployments usually discover them dynamically instead, which is covered in
Service Discovery & Relabeling. For
the config file’s full syntax and every scrape/global option, see
Getting started and validate a file
before pointing the server at it:
promtool check config prometheus.yml
The lab used throughout this section
Every page of this section runs against the same small system, so results can be compared from page to page. It has four moving parts, all of which you can start on a laptop with Docker and Python:
Python service
metrics on :28101"] LOAD["load generator
3 clients"] end subgraph DOCKER["Docker"] PROM["prom/prometheus
:9090"] NODE["prom/node-exporter
:9100"] PGW["prom/pushgateway
:9091"] BB["prom/blackbox-exporter
:9115"] end LOAD --> APP PROM -- scrape --> APP PROM -- scrape --> NODE PROM -- scrape --> PGW PROM -- "scrape /probe" --> BB BB -- probes --> APP
-
orders-apiis a Python service instrumented with the official client library. Its complete source is in Instrumenting Applications. It serves/orders(variable latency, about one request in ten fails) and/health, and exposes metrics on a separate port. -
A tiny load generator sends random requests to it so the counters and histograms move.
-
Prometheus,
node_exporter, the Pushgateway andblackbox_exporterrun from their official images. Containers reach the host throughhost.docker.internal; on Linux add--add-host host.docker.internal:host-gatewaytodocker run, as done here.
Start the pieces (ports are arbitrary; pick free ones):
# the application and some traffic
python3 -m venv venv && venv/bin/pip install prometheus_client
venv/bin/python orders_api.py 28101 28102 &
venv/bin/python load.py 28102 3000 &
# Prometheus with the configuration below, node_exporter, Pushgateway and blackbox_exporter
docker run -d --name prometheus -p 9090:9090 --add-host host.docker.internal:host-gateway \
-v "$PWD/cfg:/etc/prometheus" prom/prometheus --config.file=/etc/prometheus/prometheus.yml --web.enable-lifecycle
docker run -d --name node-exporter -p 9100:9100 prom/node-exporter
docker run -d --name pushgateway -p 9091:9091 prom/pushgateway
docker run -d --name blackbox -p 9115:9115 --add-host host.docker.internal:host-gateway \
-v "$PWD/blackbox.yml:/etc/blackbox/blackbox.yml" prom/blackbox-exporter --config.file=/etc/blackbox/blackbox.yml
The lab used host ports 29091 for Prometheus (mapped to the container’s 9090), 29100, 29101 and 29102 for
the exporters, and 28101/28102 for the service; the configuration below uses those. A shorter interval
than the default 1m keeps the lab responsive: samples are five seconds apart, so a [1m] range holds about
twelve samples.
global:
scrape_interval: 5s
evaluation_interval: 5s
rule_files:
- "alert_rules.yml"
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: "orders-api"
static_configs:
- targets: ["host.docker.internal:28101"]
labels:
env: lab
- job_name: "node"
static_configs:
- targets: ["host.docker.internal:29100"]
- job_name: "pushgateway"
honor_labels: true
static_configs:
- targets: ["host.docker.internal:29101"]
- job_name: "blackbox-http"
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets:
- http://host.docker.internal:28102/health
- http://host.docker.internal:28102/orders
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: host.docker.internal:29102
- job_name: "pushgateway-nohonor"
static_configs:
- targets: ["host.docker.internal:29101"]
The last job, pushgateway-nohonor, scrapes the same Pushgateway without honor_labels and exists only to show
what that option changes (see Exporters & the Pushgateway).
Validating before loading catches mistakes without touching the running server. With the lab’s own files,
promtool (shipped in the Prometheus image) reported:
docker run --rm --entrypoint promtool -v "$PWD/cfg:/cfg" prom/prometheus check config /cfg/prometheus.yml
Checking /cfg/prometheus.yml
SUCCESS: 1 rule files found
SUCCESS: /cfg/prometheus.yml is valid prometheus config file syntax
and with a deliberate typo (scrape_inteval) it refuses the file and says where:
FAILED: parsing YAML file /cfg/prometheus.yml: yaml: unmarshal errors:
line 2: field scrape_inteval not found in type config.plain
After editing the file on a running server, curl -X POST localhost:9090/-/reload re-reads it (this needs
the --web.enable-lifecycle flag used above) and a config that fails to parse leaves the old one in force.
The expression browser
Once the server is running, http://localhost:9090 serves the built-in web UI — the expression
browser. Its Graph tab runs an ad-hoc PromQL query against the local TSDB and renders it as a
table or a chart; a first useful query is the up metric that Prometheus attaches to every scrape:
up
On the lab server up returned one series per target (shown here as the API would render them, labels
shortened):
up{job="prometheus", instance="localhost:9090"} 1
up{job="orders-api", instance="host.docker.internal:28101", env="lab"} 1
up{job="node", instance="host.docker.internal:29100"} 1
up{job="pushgateway", instance="host.docker.internal:29101"} 1
up{job="blackbox-http", instance="http://host.docker.internal:28102/health"} 1
up{job="blackbox-http", instance="http://host.docker.internal:28102/orders"} 1
up is 1 when the most recent scrape of that target succeeded and 0 otherwise, labeled with the
target’s job and instance — it is the single most useful metric for "is anything broken" and is
the basis of the alerting rule below. The UI also exposes Status > Targets (per-job scrape health
and the last error, if any), Status > Configuration (the effective, fully-resolved YAML currently
loaded), and Status > Rules (recording and alerting rules with their current state). Each of those pages has a JSON equivalent, which is what to script against:
| Web UI page | HTTP endpoint |
|---|---|
Graph (instant) |
|
Status > Targets |
|
Status > Rules |
|
Alerts |
|
Status > Configuration |
|
Status > Runtime & Build Information |
|
(probes for orchestrators) |
|
See First steps for a guided tour of these pages, and PromQL: Basics & Selectors for how to read and write queries like the one above.
Running node_exporter and scraping it
node_exporter exposes host-level metrics — CPU, memory, disk, network — as a /metrics HTTP
endpoint that Prometheus scrapes like any other target. It ships as its own binary, separate from the
Prometheus server, and needs no configuration to start:
tar xvfz node_exporter-*.tar.gz
cd node_exporter-*/
./node_exporter
# listens on :9100/metrics by default
Add it as a second job in prometheus.yml and reload the server:
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: "node"
static_configs:
- targets: ["localhost:9100"]
# https://prometheus.io/docs/prometheus/latest/getting_started/
Running it in Docker works equally well (docker run -d -p 9100:9100 prom/node-exporter); remember that on
Docker Desktop it then reports the Linux VM that hosts the containers, not your laptop. After the next scrape interval, up{job="node"} should read 1 in the expression browser, and
metrics such as node_cpu_seconds_total or node_memory_MemAvailable_bytes become queryable.
In the lab, 1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes returned 0.649 (about 65% of the
8.3 GB VM in use) and 100 * (1 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[1m]))) returned
56.2 percent CPU. The metrics behind them, and the other collectors, are explained in
Exporters & the Pushgateway. node_exporter is one of many first-party and community exporters that adapt a system that does
not natively speak Prometheus’s format; the general pattern, and the Pushgateway for short-lived
batch jobs, is covered in
Exporters & the Pushgateway.
A first alerting rule
Alerting rules live in separate YAML files, referenced from prometheus.yml via rule_files, and
are evaluated by the Prometheus server itself on evaluation_interval — not by Alertmanager, which
only receives and routes alerts that already fired. Point at a rules file:
rule_files:
- "alert_rules.yml"
# https://prometheus.io/docs/prometheus/latest/getting_started/
A minimal rule that fires when a target has been unreachable for five straight minutes:
groups:
- name: example
rules:
- alert: InstanceDown
expr: up == 0
for: 5m
labels:
severity: critical
annotations:
summary: "Instance {{ $labels.instance }} down"
description: "{{ $labels.instance }} (job {{ $labels.job }}) has been down for 5 minutes."
In the lab the same rule was used with for: 15s so the whole life cycle fits in under a minute. Stopping the
node-exporter container produced, within one evaluation after the failed scrape, a synthetic ALERTS
series in the pending state; fifteen seconds later it became firing:
ALERTS{alertname="InstanceDown"}
ALERTS{alertname="InstanceDown", alertstate="pending", instance="host.docker.internal:29100", job="node", severity="critical"} 1
ALERTS{alertname="InstanceDown", alertstate="firing", instance="host.docker.internal:29100", job="node", severity="critical"} 1
/api/v1/alerts listed the firing alert with the rendered annotation Instance host.docker.internal:29100 down
and the time it became active, and /api/v1/rules showed the rule with state: firing, health: ok and
duration: 15. Starting the container again made the list empty within one scrape. A rule that fails to parse
is rejected by promtool check rules with the position of the error, for example (after breaking the
expression to up ==):
/cfg/alert_rules.yml: 5:15: group "example", rule 1, "InstanceDown": could not parse expression: 1:6: parse error: unexpected end of input
for: 5m requires the up == 0 condition to hold continuously across evaluations before the alert
leaves the pending state and becomes firing — it exists specifically to avoid paging on a single
missed scrape. A firing rule shows up under Status > Rules and Alerts in the expression browser
immediately, whether or not Alertmanager is configured to receive it. Validate the rules file the
same way as the main config:
promtool check rules alert_rules.yml
Writing rules well — naming conventions, recording rules for expensive expressions, and multi-window alerting patterns — is covered in Recording & Alerting Rules; routing a firing alert to email/Slack/PagerDuty is Alertmanager's job, not the Prometheus server’s.
Continue with Architecture & Ecosystem for
how the server, exporters, and Alertmanager fit together, and
Data Model & Metric Types for what a
metric like up or node_cpu_seconds_total actually is under the hood.