一、环境准备
- 系统:CentOS 7/8
- 关闭防火墙、selinux
bash
运行
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
二、创建运行用户 & 目录
bash
运行
useradd -M -s /sbin/nologin prometheus
mkdir -p /usr/local/prometheus/{bin,conf,data,rules}
chown -R prometheus:prometheus /usr/local/prometheus
三、下载安装 Prometheus
1. 下载二进制包
bash
运行
cd /usr/local
wget https://github.com/prometheus/prometheus/releases/download/v2.53.0/prometheus-2.53.0.linux-amd64.tar.gz
tar zxf prometheus-2.53.0.linux-amd64.tar.gz
cd prometheus-2.53.0.linux-amd64
2. 拷贝程序与默认配置
bash
运行
cp prometheus promtool /usr/local/prometheus/bin/
cp prometheus.yml /usr/local/prometheus/conf/
四、修改主配置文件 /usr/local/prometheus/conf/prometheus.yml
yaml
global:
scrape_interval: 15s # 抓取间隔
evaluation_interval: 15s # 告警规则检测间隔
# 告警规则文件
rule_files:
- "/usr/local/prometheus/rules/*.yml"
# 告警推送地址
alerting:
alertmanagers:
- static_configs:
- targets:
- 127.0.0.1:9093
# 监控抓取目标
scrape_configs:
# 监控自身
- job_name: 'prometheus'
static_configs:
- targets: ['127.0.0.1:9090']
# 服务器节点监控(后续填被监控IP:9100)
- job_name: 'linux-node'
static_configs:
- targets:
- 127.0.0.1:9100
五、配置系统 systemd 服务
新建 /etc/systemd/system/prometheus.service
ini
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/
After=network.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/prometheus/bin/prometheus \
--config.file=/usr/local/prometheus/conf/prometheus.yml \
--storage.tsdb.path=/usr/local/prometheus/data \
--storage.tsdb.retention.time=15d \
--web.listen-address=0.0.0.0:9090
[Install]
WantedBy=multi-user.target
六、启动 & 开机自启
bash
运行
systemctl daemon-reload
systemctl start prometheus
systemctl enable prometheus
systemctl status prometheus
七、访问 WEB 界面
浏览器访问:
plaintext
http://服务器IP:9090
- Status → Targets:查看监控目标状态
- Graph:执行 PromQL 查询
部署 Node-Exporter(监控服务器硬件)
1. 安装
bash
运行
cd /usr/local
wget https://github.com/prometheus/node_exporter/releases/download/v1.9.0/node_exporter-1.9.0.linux-amd64.tar.gz
tar zxf node_exporter-1.9.0.linux-amd64.tar.gz
cd node_exporter-1.9.0.linux-amd64
cp node_exporter /usr/local/prometheus/bin/
2. 新建 node-exporter 服务 /etc/systemd/system/node-exporter.service
ini
[Unit]
Description=Node Exporter
After=network.target
[Service]
User=prometheus
ExecStart=/usr/local/prometheus/bin/node_exporter
[Install]
WantedBy=multi-user.target
3. 启动
bash
运行
systemctl daemon-reload
systemctl start node-exporter
systemctl enable node-exporter
默认端口:9100
部署 AlertManager 告警组件
1. 安装
bash
运行
cd /usr/local
wget https://github.com/prometheus/alertmanager/releases/download/v0.28.0/alertmanager-0.28.0.linux-amd64.tar.gz
tar zxf alertmanager-0.28.0.linux-amd64.tar.gz
cd alertmanager-0.28.0.linux-amd64
cp alertmanager amtool /usr/local/prometheus/bin/
mkdir -p /usr/local/prometheus/alert-data
2. 配置告警 /usr/local/prometheus/conf/alertmanager.yml
yaml
global:
resolve_timeout: 5m
route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 1h
receiver: 'wechat'
receivers:
- name: 'wechat'
webhook_configs:
- url: '你的企业微信/钉钉机器人地址'
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
3. 系统服务 /etc/systemd/system/alertmanager.service
ini
[Unit]
Description=AlertManager
After=network.target
[Service]
User=prometheus
ExecStart=/usr/local/prometheus/bin/alertmanager \
--config.file=/usr/local/prometheus/conf/alertmanager.yml \
--storage.path=/usr/local/prometheus/alert-data
[Install]
WantedBy=multi-user.target
4. 启动
bash
运行
systemctl daemon-reload
systemctl start alertmanager
systemctl enable alertmanager
端口:9093
三、搭配 Grafana 可视化
- 安装 Grafana
- 数据源添加
Prometheus地址http://127.0.0.1:9090 - 导入服务器监控模板 ID:8919 一键出大屏
常用运维命令
bash
运行
# 重启所有组件
systemctl restart prometheus node-exporter alertmanager
# 查看日志
journalctl -u prometheus -f