Prometheus + Grafana 监控搭建指南

简介

Prometheus 是一款开源系统监控和告警工具包,最初由 SoundCloud 开发。自 2012 年诞生以来,众多公司和组织都采用了 Prometheus,该项目拥有非常活跃的开发者和用户社区

普罗米修斯的架构及其部分生态系统组成部分

  • Prometheus server:用于抓取和存储时间序列数据
  • Pushgateway:支持短生命周期任务将指标“推送”网关
  • Alertmanager:用于处理 Prometheus 警报的报警管理器
  • Exporter:适用于 HAProxy、StatsD、Graphite 等服务的导出器

大多数 Prometheus 组件都是用Go 编写的,因此很容易构建和部署为静态二进制文件。

Prometheus 会从已配置的作业中抓取指标,可以直接抓取,也可以通过中间推送网关(Pushgateway)(针对生命周期较短的作业)进行抓取。它会将所有抓取的样本存储在本地(Exporter),并运行规则来处理这些数据,从而聚合现有数据并记录新的时间序列,或者生成警报(Alertmanager)。可以使用Grafana 或其他 API 客户端来可视化收集到的数据。

安装

  • Prometheus: 核心引擎,负责定期拉取(Pull)指标数据、存储时间序列数据并触发告警。
  • Node Exporter: 采集“宿主机”的硬件和操作系统指标(CPU、内存、磁盘、网络)。
  • Blackbox Exporter: 从外部探测服务可用性(如检查 HTTP 是否返回 2xx,端口是否通,ICMP 是否响应)。
  • Alertmanager: 处理 Prometheus 发出的告警,负责去重、分组并路由到邮件/钉钉/微信等。
  • Grafana: 数据可视化面板,将 Prometheus 的数据变成漂亮的图表。

一键脚步安装工具:GitHub - skilladd/skills_tools: 一个方便你在服务器上快速部署服务的脚本文件 · GitHub

创建 Docker 网络

1
docker network create monitoring

Prometheus创建配置文件

prometheus.yml解释:

  • alertmanagers、blackbox-exporter、node-exporter,都使用容器名字进行联通,如9093、9100、9115端口号,防止外部网络,只允许创建的monitoring网络内部访问。
  • nodes.yml:监控主机
  • websites.yml:监控网站
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
global:
scrape_interval: 15s
evaluation_interval: 15s

alerting:
alertmanagers:
- static_configs:
- targets: ['alertmanager:9093']

rule_files:
- "/etc/prometheus/rules/*.yml"

scrape_configs:
- job_name: 'node-exporter'
static_configs:
- targets: ['node-exporter:9100']
file_sd_configs:
- files:
- '/etc/prometheus/targets/nodes.yml'
refresh_interval: 1m

- job_name: 'blackbox-exporter'
metrics_path: /probe
params:
module: [http_2xx]
file_sd_configs:
- files:
- '/etc/prometheus/targets/websites.yml'
refresh_interval: 1m
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: blackbox-exporter:9115

nodes.yml

  • instance_name : ‘web-server-01’ 你的主机名称
  • env:’production’ 设置类型是生产环境
  • team: ‘ops’ 设置主机组
1
2
3
4
5
6
7
8
9
10
11
12
- targets:
- '192.168.1.10:9100'
labels:
instance_name: 'web-server-01'
env: 'production'
team: 'ops'

- targets:
- '192.168.2.50:9100'
labels:
instance_name: 'db-master-01'
env: 'testing'

websites.yml

1
2
3
4
5
6
- targets:
- https://www.google.com
- https://www.github.com
- http://my-internal-app.local
labels:
service: 'public-api'

alertmanager.yml

1
2
3
4
5
route:
receiver: "null"

receivers:
- name: "null"

blackbox.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
modules:
http_2xx:
prober: http
http:
preferred_ip_protocol: "ip4"
http_post_2xx:
prober: http
http:
method: POST
tcp_connect:
prober: tcp
icmp:
prober: icmp

创建 Docker 卷(用于持久化数据)

1
2
3
docker volume create "prometheus-data"  
docker volume create "alertmanager-data"
docker volume create "grafana-data"

启动容器

Prometheus

1
2
3
4
5
6
7
8
9
10
11
12
13
14
docker run -d \
--name prometheus \
--restart unless-stopped \
--network monitoring \
-p 9090:9090 \
-v "$BASE_DIR/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro" \
-v "$BASE_DIR/rules:/etc/prometheus/rules:ro" \
-v "$BASE_DIR/prometheus/targets:/etc/prometheus/targets:ro" \
-v prometheus-data:/prometheus \
prom/prometheus:latest \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/prometheus \
--storage.tsdb.retention.time=15d \
--web.enable-lifecycle

Alertmanager

1
2
3
4
5
6
7
8
docker run -d \
--name alertmanager \
--restart unless-stopped \
--network monitoring \
-v "$BASE_DIR/alertmanager/alertmanager.yml:/etc/alertmanager/alertmanager.yml:ro" \
-v alertmanager-data:/alertmanager \
prom/alertmanager:latest \
--config.file=/etc/alertmanager/alertmanager.yml

Node Exporter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
docker run -d \
--name node-exporter \
--restart unless-stopped \
--network monitoring \
-p 9100:9100 \
--pid="host" \
-v /proc:/host/proc:ro \
-v /sys:/host/sys:ro \
-v /:/rootfs:ro \
prom/node-exporter:latest \
--path.procfs=/host/proc \
--path.sysfs=/host/sys \
--path.rootfs=/rootfs \
--collector.filesystem.mount-points-exclude="^/(sys|proc|dev|host|etc)($$|/)"

blackbox

1
2
3
4
5
6
7
8
docker run -d \
--name blackbox-exporter \
--restart unless-stopped \
--network monitoring \
-p 9115:9115 \
-v "$BASE_DIR/blackbox/blackbox.yml:/etc/blackbox_exporter/config.yml:ro" \
prom/blackbox-exporter:latest \
--config.file=/etc/blackbox_exporter/config.yml

grafana

1
2
3
4
5
6
7
8
9
docker run -d \
--name grafana \
--restart unless-stopped \
--network monitoring \
-p 3000:3000 \
-e GF_SECURITY_ADMIN_USER=admin \
-e GF_SECURITY_ADMIN_PASSWORD=admin \
-v grafana-data:/var/lib/grafana \
grafana/grafana-oss:latest

重载配置文件

1
curl -X POST http://localhost:9090/-/reload

Prometheus + Grafana 监控搭建指南
https://skilladd.org/2026/04/06/48.Prometheus + Grafana 监控搭建指南/
Author
skilladd
Posted on
April 6, 2026
Licensed under