一、Prometheus 监控框架介绍
1.1 核心概述
Prometheus 是谷歌开源的监控软件,采用 Golang 开发,通过部署在目标机器上的 Exporter 采集异构数据,统一转化为标准格式后通过 HTTP 协议传输至服务端,存储在本地时序数据库中。前端搭配 Grafana 实现数据可视化,广泛支持 Kubernetes、容器、OpenStack 等云原生环境及传统服务器、中间件监控。
1.2 Exporter 工作原理
Exporter 可理解为监控适配器,针对不同监控目标提供专属实现:
- Node Exporter:读取 Linux
/proc//sys 目录文件,获取操作系统运行状态 - MySQL Exporter:通过读取数据库监控表采集 MySQL 性能数据
- Redis Exporter:通过 Redis 命令行接口获取缓存服务指标
- WMI Exporter:适配 Windows 系统,采集系统资源与服务状态
二、环境准备
| 角色 | IP 地址 | 系统版本 | 核心组件 |
|---|
| Prometheus 服务端 | 192.168.2.45 | Ubuntu 20.04/18.04 | Prometheus、Grafana |
| 客户端(被监控端) | 192.168.2.44 | Ubuntu 20.04/18.04 | Node Exporter、MySQL Exporter、Redis Exporter |
| Windows 客户端 | 192.168.2.125 | Windows Server 2008+ | WMI Exporter |
依赖安装(Ubuntu 通用)
1
2
3
4
| # 更新软件源
sudo apt update && sudo apt upgrade -y
# 安装必要依赖
sudo apt install -y wget tar curl fontconfig init-system-helpers
|
三、Prometheus 服务端安装配置(Ubuntu)
3.1 下载与解压
1
2
3
4
5
6
| # 下载 Prometheus(当前稳定版 v2.25.0)
wget https://github.com/prometheus/prometheus/releases/download/v2.25.0/prometheus-2.25.0.linux-amd64.tar.gz
# 解压到 /usr/local 目录
sudo tar xf prometheus-2.25.0.linux-amd64.tar.gz -C /usr/local/
# 重命名简化目录
sudo mv /usr/local/prometheus-2.25.0.linux-amd64/ /usr/local/prometheus
|
3.2 配置文件说明(prometheus.yml)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| # 全局配置
global:
scrape_interval: 15s # 默认数据采集间隔,默认1分钟
evaluation_interval: 15s # 规则评估间隔,默认1分钟
# 告警管理器配置(暂不启用)
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# 规则文件加载(暂不配置)
rule_files:
# - "first_rules.yml"
# 采集配置
scrape_configs:
# 监控 Prometheus 自身
- job_name: 'prometheus'
scrape_interval: 5s # 覆盖全局采集间隔
static_configs:
- targets: ['192.168.2.45:9090'] # 服务端地址:端口
|
3.3 启动服务
方式1:临时启动
1
2
| cd /usr/local/prometheus
sudo ./prometheus --config.file=prometheus.yml
|
方式2:配置系统服务(推荐,Ubuntu 16.04+)
1
2
| # 创建服务文件
sudo vim /etc/systemd/system/prometheus.service
|
写入以下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| [Unit]
Description=Prometheus Monitoring System
Documentation=https://prometheus.io/docs/
After=network.target
[Service]
Restart=on-failure
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--web.listen-address=:9090 \
--storage.tsdb.path=/usr/local/prometheus/data \
--storage.tsdb.retention=15d \
--web.enable-lifecycle
[Install]
WantedBy=multi-user.target
|
启动并设置开机自启:
1
2
3
4
5
6
7
8
| # 重新加载系统服务
sudo systemctl daemon-reload
# 启动 Prometheus
sudo systemctl start prometheus
# 设置开机自启
sudo systemctl enable prometheus
# 查看服务状态
sudo systemctl status prometheus
|
3.4 验证访问
- 访问地址:
http://192.168.2.45:9090(Ubuntu 需确保 9090 端口开放) - 查看指标:
http://192.168.2.45:9090/metrics - 防火墙配置(如需):
1
| sudo ufw allow 9090/tcp
|
四、客户端监控配置(Ubuntu)
4.1 监控 Linux 主机(Node Exporter)
4.1.1 安装 Node Exporter
1
2
3
4
5
| # 下载 Node Exporter v1.1.2
wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
# 解压安装
sudo tar xf node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local/
sudo mv /usr/local/node_exporter-1.1.2.linux-amd64/ /usr/local/node_exporter
|
4.1.2 配置系统服务
1
| sudo vim /etc/systemd/system/node_exporter.service
|
写入以下内容(支持监控 docker/sshd/nginx 服务):
1
2
3
4
5
6
7
8
9
10
11
12
| [Unit]
Description=Node Exporter for Prometheus
After=network.target
[Service]
Restart=on-failure
ExecStart=/usr/local/node_exporter/node_exporter \
--collector.systemd \
--collector.systemd.unit-whitelist="(docker|sshd|nginx).service"
[Install]
WantedBy=multi-user.target
|
启动服务:
1
2
3
4
5
| sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter
# 验证端口(默认 9100)
sudo ss -lntp | grep 9100
|
4.1.3 服务端添加监控项
编辑 Prometheus 配置文件:
1
| sudo vim /usr/local/prometheus/prometheus.yml
|
添加 Linux 主机监控配置:
1
2
3
4
5
6
| scrape_configs:
# 原有 prometheus 监控配置...
- job_name: 'linux'
static_configs:
- targets: ['192.168.2.44:9100', '192.168.2.43:9100'] # 多个客户端用逗号分隔
|
重启 Prometheus 生效:
1
| sudo systemctl restart prometheus
|
4.2 监控 MySQL(mysqld_exporter)
4.2.1 安装配置 Exporter
1
2
3
4
5
| # 下载 mysqld_exporter v0.12.1
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
# 解压安装
sudo tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/
sudo mv /usr/local/mysqld_exporter-0.12.1.linux-amd64/ /usr/local/mysqld_exporter
|
创建 MySQL 连接配置:
1
2
| cd /usr/local/mysqld_exporter
sudo vim .my.cnf
|
写入 MySQL 认证信息:
1
2
3
4
5
| [client]
user=root
password=123456 # 替换为你的 MySQL 密码
host=127.0.0.1
port=3306
|
4.2.2 启动 Exporter
1
2
3
4
| # 临时启动(后台运行)
sudo ./mysqld_exporter --config.my-cnf=".my.cnf" &
# 验证端口(默认 9104)
sudo ss -lntp | grep 9104
|
4.2.3 服务端添加监控配置
1
2
3
4
5
6
| scrape_configs:
# 原有配置...
- job_name: 'mysql'
static_configs:
- targets: ['192.168.2.44:9104']
|
重启 Prometheus:
1
| sudo systemctl restart prometheus
|
4.3 监控 Redis(redis_exporter)
4.3.1 安装 Exporter
1
2
3
4
5
| # 下载 redis_exporter v0.15.0
wget https://github.com/oliver006/redis_exporter/releases/download/v0.15.0/redis_exporter-v0.15.0.linux-amd64.tar.gz
# 解压安装
sudo tar xf redis_exporter-v0.15.0.linux-amd64.tar.gz -C /usr/local/
sudo mv /usr/local/redis_exporter-v0.15.0.linux-amd64/ /usr/local/redis_exporter
|
4.3.2 启动 Exporter
1
2
3
4
| # 连接 Redis 并启动(默认端口 9121)
sudo /usr/local/redis_exporter/redis_exporter redis://192.168.2.44:6379 &
# 验证端口
sudo ss -lntp | grep 9121
|
4.3.3 服务端添加监控配置
1
2
3
4
5
6
| scrape_configs:
# 原有配置...
- job_name: 'redis'
static_configs:
- targets: ['192.168.2.44:9121']
|
重启 Prometheus:
1
| sudo systemctl restart prometheus
|
4.4 监控 Windows 机器(WMI Exporter)
4.4.1 安装 Exporter
- 下载地址:https://github.com/prometheus-community/windows_exporter/releases/download/v0.16.0/windows_exporter-0.16.0-amd64.msi
- 双击 MSI 文件默认安装,自动创建开机自启服务
- 默认端口:9182
4.4.2 服务端添加监控配置
1
2
3
4
5
6
| scrape_configs:
# 原有配置...
- job_name: 'windows'
static_configs:
- targets: ['192.168.2.125:9182']
|
重启 Prometheus:
1
| sudo systemctl restart prometheus
|
4.5 验证监控目标
访问 Prometheus 控制台:http://192.168.2.45:9090/targets,所有监控目标状态应显示为 UP。
五、Grafana 安装与可视化配置(Ubuntu)
5.1 安装 Grafana
1
2
3
4
5
6
7
8
9
10
| # 下载 Grafana v7.4.3(适配 Prometheus 稳定版)
wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-7.4.3-1.x86_64.rpm
# 转换为 deb 包安装(Ubuntu 适用)
sudo apt install -y alien
sudo alien -i grafana-7.4.3-1.x86_64.rpm
# 启动 Grafana 并设置开机自启
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
# 开放 3000 端口
sudo ufw allow 3000/tcp
|
5.2 初始访问与数据源配置
- 访问地址:
http://192.168.2.45:3000 - 初始账号密码:
admin/admin(首次登录需修改密码)
添加 Prometheus 数据源
- 点击左侧
Configuration → Data Sources → Add data source - 选择
Prometheus 类型 - 配置核心参数:
- Name:Prometheus(自定义)
- URL:
http://192.168.2.45:9090 - Scrape interval:15s
- 点击
Save & Test,显示 Data source is working 即为成功
5.3 导入 Dashboard 模板
5.3.1 Linux 系统监控模板(ID: 8919)
- 点击左侧
Create → Import - 在
Import via grafana.com 输入模板 ID:8919 - 选择数据源为之前创建的
Prometheus - 点击
Import 完成导入 - 查看 Dashboard:
Dashboards → Manage → 1 Node Exporter for Prometheus Dashboard CN
5.3.2 MySQL 监控模板
- 下载模板文件:https://pan.baidu.com/s/12S33kZkZd9gIXF8tMHKECg(提取码:9b0s)
- 点击
Import → Upload JSON file,选择下载的 JSON 文件 - 选择 MySQL 数据源(需提前在 Grafana 中添加 MySQL 数据源)
- 完成导入
5.3.3 Redis 监控模板
- 模板下载:https://pan.baidu.com/s/1LIkVC0o3lyTT59JbX0mSpg(提取码:gf1d)
- 导入步骤同上,选择 Prometheus 数据源
5.3.4 Windows 监控模板(ID: 10467)
- 导入模板 ID:10467
- 选择 Prometheus 数据源
- 完成导入,可查看 Windows 系统 CPU、内存、磁盘等监控数据
六、常用操作与故障排查
6.1 Prometheus 配置热加载
无需重启服务,通过 API 热加载配置:
1
| curl -X POST 192.168.2.45:9090/-/reload
|
6.2 服务状态查看
1
2
3
4
5
6
| # Prometheus 状态
sudo systemctl status prometheus
# Grafana 状态
sudo systemctl status grafana-server
# Exporter 状态(以 node_exporter 为例)
sudo systemctl status node_exporter
|
6.3 端口占用排查
1
2
3
| # 查看指定端口占用情况
sudo lsof -i :9100 # node_exporter 端口
sudo lsof -i :9090 # Prometheus 端口
|
6.4 日志查看
1
2
3
4
| # Prometheus 日志
sudo journalctl -u prometheus -f
# Grafana 日志
sudo journalctl -u grafana-server -f
|
七、相关资源
- Prometheus 官方下载:https://prometheus.io/download/
- Exporter 集成列表:http://www.coderdocument.com/docs/prometheus/v2.14/instrumenting/exporters_and_integrations.html
- Grafana 模板市场:https://grafana.com/grafana/dashboards
- WMI Exporter 下载:https://github.com/prometheus-community/windows_exporter/releases