Prometheus+Grafana监控部署

Prometheus+Grafana

一、部署Prometheus服务器

环境

Prometheus(ip:192.168.200.147) Web1(ip:192.168.200.148)

配置时间

1
2
3
4
5
6
7
8
# 查看时区
timedatectl

#如果时区不是上海,则改为正确的时区
timedatectl set-timezone Asia/Shanghai

#查看时间&修改时间
date #如果时间不正确 使用date -s "年月日 时:分:秒" //date -s "20260312 12:00:00"

安装Prometheus

从官网下载相应版本,安装到服务器上官网提供的是二进制版,解压就能用,不需要编译

下载地址: https://prometheus.io/download/

解压就能用,不需要编译:

1
2
3
4
5
#解压到指定目录
tar -zxvf prometheus-3.10.0.linux-amd64.tar.gz

#移动到指定目录
mv prometheus-3.10.0.linux-amd64 /usr/local/prometheus
  • 配置文件
    1. 配置文件中包含三个配置块:globalrule_filesscrape_configs
    2. global块控制 Prometheus 服务器的全局配置。我们有两个选择。第一个,scrape_interval控制 Prometheus 抓取目标的频率。您可以为单个目标覆盖它。在这种情况下,全局设置是每 15 秒抓取一次。该evaluation_interval选项控制 Prometheus 评估规则的频率。Prometheus 使用规则来创建新的时间序列并生成警报。
    3. rule_files块指定我们希望 Prometheus 服务器加载的任何规则的位置。现在我们还没有规则。
    4. 最后一个块,scrape_configs控制 Prometheus 监控的资源。由于 Prometheus 还将有关自身的数据公开为 HTTP 端点,因此它可以抓取和监控自身的健康状况。在默认配置中,有一个名为 的作业prometheus,用于抓取 Prometheus 服务器公开的时间序列数据。该作业包含一个单一的、静态配置的目标,即localhost的9090端口。Prometheus期望度量在/metrics路径上的目标上可用,所以这个默认作业是通过 URL 抓取的:http://localhost:9090/metrics。

编写服务启动文件并启动服务

1
2
3
4
5
6
7
8
9
10
11
12
13
vim /usr/lib/systemd/system/prometheus.service
#配置如下
[Unit]
Description=Prometheus Monitoring System
After=network.target

[Service]
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/usr/local/prometheus/data/

[Install]
WantedBy=multi-user.target

启动服务

1
2
3
4
systemctl daemon-reload 
systemctl enable prometheus.service --now
ss -tlnp | grep :9090
LISTEN 0 128 :::9090 :::* users:(("prometheus",pid=10673,fd=8))

访问http://192.168.200.147:9090/ //若不成功关闭防火墙什么的

二、添加被监控端

1、下载软件包解压并部署

1
2
3
4
5
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz

tar xf node_exporter-1.8.2.linux-amd64.tar.gz

mv node_exporter-1.8.2.linux-amd64 /usr/local/node_exporter

2、创建服务文件,启动服务(Web1)

1
2
3
4
5
6
7
8
9
10
11
12
13
vim /usr/lib/systemd/system/node_exporter.service
#配置如下

[Unit]
Description=node_exporter
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter

[Install]
WantedBy=multi-user.target

3、启动服务

1
2
3
systemctl daemon-reload 
systemctl enable node_exporter.service --now
ss -tlnp | grep :9100

4、在Prometheus服务器上添加监控节点(Prometheus)

1
2
3
4
5
6
7
8
9
10
11
12
vim /usr/local/prometheus/prometheus.yml

scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["192.168.200.147:9090"]
labels:
app: "prometheus"
##以上是自带的,添加以下的,注意缩进
- job_name: "web1"
static_configs:
- targets: ["192.168.200.148:9100"]

用这条命令检查配置文件是否有错误

1
/usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml

5、重启Prometheus服务

1
2
systemctl restart prometheus.service
systemctl status prometheus.service

访问http://192.168.200.147:9090/targets 点击Status>Target health即可看见Prometheus和Web1

image-20260312140350106

三、Grafana环境搭建及Prometheus监控指标展示

1、下载Grafana

1
2
3
4
5
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-11.1.4.linux-amd64.tar.gz
tar -zxvf grafana-enterprise-11.1.4.linux-amd64.tar.gz
cd grafana-v11.1.4
ls -l bin/
./bin/grafana-server

访问http://192.168.200.147:3000

默认用户名密码都是admin

2、配置Prometheus数据源

Add new connection—Prometheus

Prometheus server URL:http://192.168.200.147:9090——保存配置(默认记得导入模板)

或者访问https://grafana.com/grafana/dashboards/ 然后挑一个点进去,滑下去右下角有一个ID

image-20260312142731361

再填进去

image-20260312142812596

成果展示

image-20260312152520585