主题
Prometheus 02 · 部署方案
四篇制:01 理论 · 02 部署 · 03 功能 · 04 实战 本篇装 Prometheus + Node Exporter 并配好保留与长期存储。原理见 01 理论。展示与告警组件分别见 Grafana、Alertmanager。
环境:监控机 10.0.0.10,被监控主机 10.0.0.11~13。版本以官方稳定版为准(Prometheus 3.x、Node Exporter 1.x),生产不用 latest。
一、装 Prometheus(二进制)
bash
cd /usr/local/src
wget https://github.com/prometheus/prometheus/releases/download/v3.15.0/prometheus-3.15.0.linux-amd64.tar.gz
tar xf prometheus-3.15.0.linux-amd64.tar.gz
mv prometheus-3.15.0.linux-amd64 /usr/local/prometheus
useradd -r -s /usr/sbin/nologin prometheus
chown -R prometheus:prometheus /usr/local/prometheusprometheus.yml 采集配置
yaml
global:
scrape_interval: 15s # 采集间隔
evaluation_interval: 15s # 规则计算间隔
alerting:
alertmanagers:
- static_configs:
- targets: ['10.0.0.10:9093']
rule_files:
- 'rules/*.yml'
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node_exporter'
static_configs:
- targets: ['10.0.0.11:9100','10.0.0.12:9100','10.0.0.13:9100']systemd 服务
ini
# /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
After=network.target
[Service]
User=prometheus
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/usr/local/prometheus/data \
--storage.tsdb.retention.time=30d \
--storage.tsdb.retention.size=100GB \
--web.enable-lifecycle
Restart=on-failure
[Install]
WantedBy=multi-user.targetbash
/usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml # 先校验
sudo systemctl daemon-reload && sudo systemctl enable --now prometheus--web.enable-lifecycle 开启后可 curl -X POST localhost:9090/-/reload 热加载配置,不用重启。浏览器访问 http://10.0.0.10:9090 看 Targets 页。
二、装 Node Exporter(每台被监控机)
bash
wget https://github.com/prometheus/node_exporter/releases/download/v1.12.1/node_exporter-1.12.1.linux-amd64.tar.gz
tar xf node_exporter-1.12.1.linux-amd64.tar.gz
sudo mv node_exporter-1.12.1.linux-amd64/node_exporter /usr/local/bin/建 systemd 服务跑 node_exporter --web.listen-address=:9100,启动后自检:
bash
ss -lntp | grep 9100 # 确认监听
curl -s localhost:9100/metrics | head # 确认有指标拉不到先查网络,别急着重装
Pull 模式要求 Prometheus 能访问目标。目标 DOWN(up=0)时按序查:ss -lntp 看是否监听 → 只监听 127.0.0.1 的改 0.0.0.0 → 防火墙/安全组放通 9100 → 监控机 curl http://目标:9100/metrics 验证。详见 04 实战。
三、保留策略与磁盘估算
本地 TSDB 最新数据先放内存(Head)+ 写 WAL 防丢,每 2 小时刷成不可变 Block 落盘,后台压缩合并。两个保留参数任一满足就删旧数据:
--storage.tsdb.retention.time=30d:时间兜底。--storage.tsdb.retention.size=100GB:空间兜底(所有块总量,超限先删最旧)。
磁盘估算:保留秒数 × 每秒样本数 × 每样本字节。压缩后每样本约 1~2 字节,按 2 字节再乘 2~3 倍冗余(WAL/Head 峰值)。例:每秒 1 万样本保留 30 天 ≈ 518GB 块数据 + 冗余。生产两个参数都设,防磁盘写爆。
四、长期存储:VictoriaMetrics
本地 TSDB 是单点、不支持集群。要长期保存 + 高可用,用 VictoriaMetrics(兼容 Prometheus 查询 API,Grafana 可直接当数据源)。
单机版(摄入 < 100 万样本/秒)
bash
# 下载开源版(别下 enterprise),跑 victoria-metrics-prod
# -httpListenAddr=:8428 -storageDataPath=/data/victoriametrics -retentionPeriod=6-retentionPeriod 不带后缀单位是月(6=6 个月)。Prometheus 加远程写入:
yaml
remote_write:
- url: http://10.0.0.10:8428/api/v1/writeGrafana 数据源 URL 填 http://10.0.0.10:8428,自带查询界面 /vmui。
集群版(水平扩展/多租户)
三组件:vminsert(接收写入分片,8480)、vmstorage(存储,8482/8400/8401)、vmselect(查询合并,8481)。多租户端点 /insert/<accountID>/prometheus、/select/<accountID>/prometheus。-replicationFactor=2 做副本,vmselect 加 -dedup.minScrapeInterval 去重。vmstorage 数量变更必须同步到所有 insert/select 的 storageNode 列表。
五、对接 Grafana 与 Alertmanager
- Grafana 加 Prometheus 数据源(URL
http://10.0.0.10:9090),导入 Node Exporter 看板 1860,见 Grafana。 - Alertmanager 部署见 Alertmanager,prometheus.yml 的 alerting 段指向它。
官方文档
- 安装:https://prometheus.io/docs/prometheus/latest/installation/
- 配置:https://prometheus.io/docs/prometheus/latest/configuration/configuration/
- 存储:https://prometheus.io/docs/prometheus/latest/storage/
- VictoriaMetrics:https://docs.victoriametrics.com/
黑公网安备23022102000118号