主题
监控服务部署指南
🚀 一、部署步骤
1. 系统环境检查
bash
# 检查系统版本
cat /etc/redhat-release
# 检查可用内存(建议至少2GB)
free -h
# 检查磁盘空间(建议至少10GB)
df -h
# 检查网络连接
ping -c 4 8.8.8.81
2
3
4
5
6
7
8
2
3
4
5
6
7
8
2. 创建系统用户
bash
# 切换成root用户
su - root
# 为Prometheus创建专用用户
useradd --no-create-home --shell /bin/false prometheus
# 为Node Exporter创建专用用户
useradd --no-create-home --shell /bin/false node_exporter
# 为Grafana创建专用用户
useradd --no-create-home --shell /bin/false grafana
# 验证用户创建
cat /etc/passwd | grep prometheus
cat /etc/passwd | grep node_exporter
cat /etc/passwd | grep grafana
# 或者直接
cat /etc/passwd #查看后几行1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15

📊 二、Prometheus部署
3. 下载并安装Prometheus
bash
# 切换成root用户
su - root
# 下载Prometheus最新稳定版
cd /tmp
# 国内可能无法下载,可使用下方教师提供的安装包,人多找老师统一分发。
wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz
# 解压安装包
tar -xvzf prometheus-2.45.0.linux-amd64.tar.gz
# 创建目录结构
mkdir -p /etc/prometheus
mkdir -p /var/lib/prometheus
mkdir -p /var/log/prometheus
# 复制二进制文件
cp prometheus-2.45.0.linux-amd64/prometheus /usr/local/bin/
cp prometheus-2.45.0.linux-amd64/promtool /usr/local/bin/
# 复制配置文件
cp -r prometheus-2.45.0.linux-amd64/consoles /etc/prometheus/
cp -r prometheus-2.45.0.linux-amd64/prometheus.yml /etc/prometheus/
# 设置文件权限
chown -R prometheus:prometheus /etc/prometheus/
chown -R prometheus:prometheus /var/lib/prometheus/
chown -R prometheus:prometheus /var/log/prometheus/
chown prometheus:prometheus /usr/local/bin/prometheus
chown prometheus:prometheus /usr/local/bin/promtool1
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
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
Prometheus安装包:点击下载
4. 配置Prometheus
bash
# 编辑Prometheus主配置文件
vim /etc/prometheus/prometheus.yml1
2
2
配置文件内容:
yaml
# /etc/prometheus/prometheus.yml
global:
scrape_interval: 15s # 数据收集间隔
evaluation_interval: 15s # 告警规则评估间隔
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node'
static_configs:
- targets: ['localhost:9100']1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
5. 创建Prometheus系统服务
bash
# 创建systemd服务文件
vim /etc/systemd/system/prometheus.service1
2
2
服务文件内容:
ini
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
--config.file /etc/prometheus/prometheus.yml \
--storage.tsdb.path /var/lib/prometheus/ \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries \
--web.listen-address=0.0.0.0:9090 \
--web.enable-admin-api
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bash
# 重新加载systemd并启动服务
systemctl daemon-reload
systemctl start prometheus
systemctl enable prometheus
systemctl status prometheus1
2
3
4
5
2
3
4
5
🔍 三、Node Exporter部署
6. 下载并安装Node Exporter
bash
# 下载Node Exporter
cd /tmp
# 国内可能无法下载,可使用下方教师提供的安装包,人多找老师统一分发。
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.0/node_exporter-1.6.0.linux-amd64.tar.gz
# 解压安装包
tar -xvzf node_exporter-1.6.0.linux-amd64.tar.gz
# 复制二进制文件
cp node_exporter-1.6.0.linux-amd64/node_exporter /usr/local/bin/
# 设置文件权限
chown node_exporter:node_exporter /usr/local/bin/node_exporter1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
node_exporter安装包:点击下载
7. 创建Node Exporter系统服务
bash
# 创建systemd服务文件
vim /etc/systemd/system/node_exporter.service1
2
2
服务文件内容:
ini
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter \
--collector.cpu \
--collector.meminfo \
--collector.diskstats \
--collector.netdev \
--collector.filesystem \
--web.listen-address=0.0.0.0:9100
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bash
# 启动Node Exporter服务
systemctl daemon-reload
systemctl start node_exporter
systemctl enable node_exporter
systemctl status node_exporter1
2
3
4
5
2
3
4
5
📈 四、Grafana部署
8. 安装Grafana
1.rpm包安装(推荐)
bash
cd /tmp
# 国内可能无法下载,可使用下方教师提供的安装包,人多找老师统一分发。
wget https://dl.grafana.com/oss/release/grafana-6.2.1-1.x86_64.rpm
yum localinstall grafana-6.2.1-1.x86_64.rpm1
2
3
4
2
3
4
grafana安装包:点击下载
2.yum安装(国内不推荐)
bash
# 官方下载Grafana(国内速度很慢)
cat > /etc/yum.repos.d/grafana.repo << 'EOF'
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
EOF
# 导入GPG密钥
rpm --import https://packages.grafana.com/gpg.key
# 安装Grafana
yum install -y grafana1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bash
# 启动Grafana服务
systemctl start grafana-server
systemctl enable grafana-server
systemctl status grafana-server1
2
3
4
2
3
4
9. 配置Grafana
bash
# 编辑Grafana配置文件
vim /etc/grafana/grafana.ini1
2
2
关键配置项:
ini
# /etc/grafana/grafana.ini
[server]
# 监听地址
http_addr = 0.0.0.0
# 监听端口
http_port = 3000
[security]
# 允许所有域名嵌入
allow_embedding = true
[database]
# 数据库类型(默认SQLite)
type = sqlite3
path = /var/lib/grafana/grafana.db1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bash
# 重启Grafana使配置生效
systemctl restart grafana-server1
2
2
🔥 五、防火墙配置
10. 开放必要端口
bash
# 开放Prometheus端口
firewall-cmd --permanent --add-port=9090/tcp
# 开放Node Exporter端口
firewall-cmd --permanent --add-port=9100/tcp
# 开放Grafana端口
firewall-cmd --permanent --add-port=3000/tcp
# 重新加载防火墙配置
firewall-cmd --reload
# 验证端口开放
firewall-cmd --list-ports1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
或者直接关闭防火墙(测试环境):
bash
systemctl stop firewalld
systemctl disable firewalld
setenforce 01
2
3
2
3
🧪 六、部署后测试
11. 服务状态测试
bash
# 检查所有服务状态
systemctl status prometheus node_exporter grafana-server
# 检查端口监听
netstat -tlnp | grep -E '9090|9100|3000'
# 检查进程
ps aux | grep -E 'prometheus|node_exporter|grafana'1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
12. Prometheus测试
bash
# 测试Prometheus本地访问
curl -I http://localhost:9090
# 获取Prometheus目标状态
curl http://localhost:9090/api/v1/targets
# windows宿主机浏览器访问Prometheus
http://虚拟机IP:9090/ # 虚拟机IP通过ifconfig查看1
2
3
4
5
6
2
3
4
5
6

点击Status → Targets → 显示如下,即表示Prometheus已成功发现Node Exporter

13. Node Exporter测试
bash
# 测试Node Exporter
curl -I http://localhost:9100
# 获取系统指标
curl http://localhost:9100/metrics | head -20
# windows宿主机浏览器访问Node Exporter
http://虚拟机IP:9100/ # 虚拟机IP通过ifconfig查看1
2
3
4
5
6
2
3
4
5
6

14. Grafana测试
bash
# 测试Grafana本地访问
curl -I http://localhost:3000
# windows宿主机浏览器访问Grafana
http://虚拟机IP:3000/ # 虚拟机IP通过ifconfig查看1
2
3
4
2
3
4

15. 浏览器访问测试
在浏览器中访问以下地址:
Prometheus Web界面:
http://服务器IP:9090- 查看状态:Status → Targets
- 执行查询:在查询框输入
up
Grafana Web界面:
http://服务器IP:3000- 默认用户名:admin
- 默认密码:admin(首次登录需修改,可以改成123456)
📊 七、Grafana配置
16. 添加Prometheus数据源
- 登录Grafana(
http://服务器IP:3000) 默认用户名: admin 改后密码: 123456 - 点击"Configuration"(齿轮图标)
- 选择"Data Sources"
- 点击"Add data source"
- 选择"Prometheus"
- 配置数据源:
- Name: Prometheus
- URL: http://localhost:9090
- Access: Server (default)
- 点击"Save & Test"

或者




17. 导入监控仪表板
1.导入默认仪表盘


导入成功后,进入Dashboard显示效果如下:
Prometheus Stats 
Prometheus 2.0 Stats 
2.导入node-exporter仪表盘
- 下方下载链接,鼠标右键,将链接另存为
- 点击"+"图标,选择"Import"
- 上传下载的JSON文件
node-exporter仪表盘:点击下载



导入成功后,进入Dashboard显示效果如下:

参考链接:https://cloud.tencent.com/developer/article/2034797
额外说明:实测如果使用如下图中browser模式,由于浏览器的CORS问题,会导致仪表板无法正常显示,可以在浏览器控制台看到报错。因此改成使用server模式。

🔧 八、常用管理命令
服务管理
bash
# Prometheus服务管理
systemctl start prometheus
systemctl stop prometheus
systemctl restart prometheus
systemctl status prometheus
# Node Exporter服务管理
systemctl start node_exporter
systemctl stop node_exporter
systemctl restart node_exporter
systemctl status node_exporter
# Grafana服务管理
systemctl start grafana-server
systemctl stop grafana-server
systemctl restart grafana-server
systemctl status grafana-server1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
日志管理
bash
# 查看Prometheus日志
tail -f /var/log/prometheus/prometheus.log
# 查看Node Exporter日志(通过journalctl)
journalctl -u node_exporter -f
# 查看Grafana日志
tail -f /var/log/grafana/grafana.log1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
配置管理
bash
# 检查Prometheus配置语法
/usr/local/bin/promtool check config /etc/prometheus/prometheus.yml
# 重新加载Prometheus配置
curl -X POST http://localhost:9090/-/reload
# 备份配置文件
cp /etc/prometheus/prometheus.yml /etc/prometheus/prometheus.yml.backup1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
📈 九、常用监控指标
系统资源指标
promql
# CPU使用率
100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
# 内存使用率
(1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100
# 磁盘使用率
(1 - (node_filesystem_avail_bytes{fstype!="tmpfs"} / node_filesystem_size_bytes{fstype!="tmpfs"})) * 100
# 网络流量
rate(node_network_receive_bytes_total[5m]) * 8
rate(node_network_transmit_bytes_total[5m]) * 8
# 系统负载
node_load1
node_load5
node_load151
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
服务状态指标
promql
# 检查服务是否在线
up{job="prometheus"}
up{job="node"}
# Prometheus数据收集延迟
prometheus_target_interval_length_seconds1
2
3
4
5
6
2
3
4
5
6
🚨 十、告警配置
18. 创建告警规则
bash
# 创建告警规则目录
mkdir -p /etc/prometheus/rules
# 创建告警规则文件
vim /etc/prometheus/rules/node_alerts.yml1
2
3
4
5
2
3
4
5
告警规则内容:
yaml
# /etc/prometheus/rules/node_alerts.yml
groups:
- name: node_alerts
rules:
- alert: HighCPUUsage
expr: 100 - (avg by(instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 80
for: 2m
labels:
severity: warning
annotations:
summary: "服务器{{ $labels.instance }} CPU使用率过高"
description: "CPU使用率已超过80%,当前值:{{ $value }}%"
- alert: HighMemoryUsage
expr: (1 - (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes)) * 100 > 85
for: 2m
labels:
severity: warning
annotations:
summary: "服务器{{ $labels.instance }} 内存使用率过高"
description: "内存使用率已超过85%,当前值:{{ $value }}%"
- alert: DiskSpaceLow
expr: (1 - (node_filesystem_avail_bytes{fstype!="tmpfs"} / node_filesystem_size_bytes{fstype!="tmpfs"})) * 100 > 90
for: 1m
labels:
severity: critical
annotations:
summary: "服务器{{ $labels.instance }} 磁盘空间不足"
description: "磁盘{{ $labels.mountpoint }}使用率已超过90%,当前值:{{ $value }}%"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
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
19. 配置Alertmanager(可选)
bash
# 下载Alertmanager
cd /tmp
wget https://github.com/prometheus/alertmanager/releases/download/v0.25.0/alertmanager-0.25.0.linux-amd64.tar.gz
# 解压并安装
tar xvfz alertmanager-0.25.0.linux-amd64.tar.gz
cp alertmanager-0.25.0.linux-amd64/alertmanager /usr/local/bin/
cp alertmanager-0.25.0.linux-amd64/amtool /usr/local/bin/
# 创建目录和配置
mkdir -p /etc/alertmanager
mkdir -p /var/lib/alertmanager
# 创建alertmanager.yml配置文件
vim /etc/alertmanager/alertmanager.yml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
🔧 十一、故障排除
常见问题及解决方案
Prometheus无法启动
bash
# 检查配置文件语法
/usr/local/bin/promtool check config /etc/prometheus/prometheus.yml
# 检查端口是否被占用
netstat -tlnp | grep 9090
# 检查权限
ls -la /etc/prometheus/
ls -la /var/lib/prometheus/1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Node Exporter无数据
bash
# 检查Node Exporter状态
curl http://localhost:9100/metrics
# 检查Prometheus目标配置
curl http://localhost:9090/api/v1/targets1
2
3
4
5
2
3
4
5
Grafana无法连接Prometheus
bash
# 检查网络连通性
curl -I http://localhost:9090
# 检查防火墙配置
firewall-cmd --list-ports
# 检查Grafana日志
tail -f /var/log/grafana/grafana.log1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
🔄 十二、其他Exporter安装方法
安装模式对比
| Exporter类型 | 下载地址示例 | 默认端口 | 安装步骤相似度 |
|---|---|---|---|
| Node Exporter | node_exporter-1.6.0.linux-amd64.tar.gz | 9100 | 参考文档 |
| Nginx Exporter | nginx-prometheus-exporter-0.11.0.linux-amd64.tar.gz | 9113 | 95%相同 |
| MySQL Exporter | mysqld_exporter-0.15.0.linux-amd64.tar.gz | 9104 | 90%相同 |
| Redis Exporter | redis_exporter-1.50.0.linux-amd64.tar.gz | 9121 | 95%相同 |
| PostgreSQL Exporter | postgres_exporter-0.13.2.linux-amd64.tar.gz | 9187 | 90%相同 |
✅ 总结
通过以上步骤,您已经成功部署了完整的Prometheus + Grafana监控系统:
- ✅ Prometheus服务:数据收集和存储
- ✅ Node Exporter:系统指标导出
- ✅ Grafana:数据可视化和仪表板
- ✅ 防火墙配置:网络访问控制
- ✅ 基础监控仪表板:系统资源可视化
- ✅ 告警规则:异常情况预警
现在您可以:
- 通过Prometheus Web界面查看指标数据
- 使用Grafana创建精美的监控仪表板
- 设置告警规则及时发现问题
- 扩展更多Exporter监控其他服务
这个监控系统为Linux服务器管理提供了强大的观察能力,帮助您更好地了解和维护系统运行状态。