prometheus和node_exporter的部署

一: Prometheus 的介绍与架构

1.1 Prometheus 的概述

1.prometheus 的介绍
Prometheus是一个开源的系统监控和告警工具包,最初由SoundCloud开发。自2012年开
始,许多公司和组织开始使用了Prometheus,该项目拥有非常活跃的开发者和用户社区。
Prometheus现在是一个独立的开源项目,独立于任何公司进行维护。为了强调这一点,并澄
清项目的治理结构,Prometheus在2016年加入了云原生计算基金会(CNCF),成为继
Kubernetes之后的第二个托管项目,也是从CNCF第二个毕业的项目。

2. Prometheus的主要特征:

2.1 Prometheus使用的是 度量(metric)名称和键/值对标签(label)的时间序列数据,是一种多
维的数据模型。
2.2 PromQL是一种灵活的查询语言,可以利用度量(metric)名称和标签进行查询、聚合。
2.3 不依赖于分布式存储,单个Prometheus服务也是自治理的。
2.4 使用基于HTTP的拉(pull)模型进行时间序列的数据收集。
2.5 同时也支持通过一个中间网关(pushgateway)来推送时间序列。
2.6 目标对象(主机)是通过静态配置或者服务发现来添加的。
2.7 支持多种图形模式和仪表盘。

3. prometheus的组件

Prometheus目前已经是一个生态系统,具有众多的可选组件。
3.1 Prometheus Server 本身用于抓取并存储时间序列数据。
3.2 客户端程序库用于检测各种编程语言编写的程序代码。
3.3 pushgateway用于支持短生命周期(short-lived)的作业(job)。
3.4 可以针对不同的服务提供对应的导出器(exporters)用于采集度量数据,如HAProxy、MySQL等服务。
3.5 用于告警的alertmanager组件
3.6 各种支持工具。
备注:大多数Prometheus组件都是用Go编写的,这使得它们很容易以静态二进制文件的形式
构建和部署。

prometheus 的官网:
          https://prometheus.io/

  

1.2 prometheus 的 架构

image

 

从上述架构图我们可以知道,Prometheus通过从Jobs/exporters中拉取度量数据;而短周期
的jobs在结束前可以先将度量数据推送到网关(pushgateway),然后Prometheus再从
pushgateway中获取短周期jobs的度量数据;还可以通过自动发现目标的方式来监控
kubernetes集群。所有收集的数据可以存储在本地的TSDB数据库中,并在这些数据上运行规
则、检索、聚合和记录新的时间序列,将产生的告警通知推送到Alertmanager组件。通过
PromQL来计算指标,再结合Grafana或其他API客户端来可视化数据。
Prometheus主要用于大规模的云端环境和容器化微服务(k8s)的监控,通过拉取(pull)应用程
序暴露出来的HTTP接口或exporter来获取时间序列数据。
Prometheus不适用于对监控要求100%准确的度量数据,比如每个请求的账单,因为收集的
数据可能还不够详细和完整。
Prometheus将其可以拉取指标的来源称为endpoint(端点),endpoint可以是各种
exporter(导出器)或应用程序。然后,为了拉取endpoint里的数据,Prometheus定义了名为
target(目标)的配置,告诉拉取时要如何进行连接等信息,多个具有相同功能角色的target组
合在一起就构成了一个job(作业),例如,具有相同用途的一组主机的资源监控器
(node_exporter),又或者是MySQL数据库监控器(mysqld_exporter)。
Prometheus默认是将收集到的时间序列数据存储在本地tsdb数据库中,且默认只保留15天,
也可以配置发送到其他时间序列数据库中。

  

3.2 安装prometheus 

部署机器在192.168.175.184-186三台

image

 操作步骤如下所示:

部署目录在/usr/local/prometheus/ 下面

image

 启动的用户是prometheus

useradd prometheus

要注意权限

image

 根据图片中的错误信息,您的 Prometheus 服务启动失败,原因是权限问题。具体错误显示无法打开查询日志文件,提示 "permission denied"(权限被拒绝)。

这表明 Prometheus 进程没有足够的权限访问其数据目录中的文件。

解决方案

1. 检查并修复数据目录权限

这是最主要的解决方案。Prometheus 进程运行用户(通常是 prometheus或您指定的用户)需要对数据目录有读写权限。

# 查看数据目录当前权限
ls -ld /usr/local/prometheus/prometheus-3.5.1.linux-amd64/data/

# 确保数据目录存在,如果不存在则创建
sudo mkdir -p /usr/local/prometheus/prometheus-3.5.1.linux-amd64/data

# 修改目录所有者为 prometheus 用户(假设使用 prometheus 用户运行)
sudo chown -R prometheus:prometheus /usr/local/prometheus/prometheus-3.5.1.linux-amd64/data/

# 设置适当的权限(通常 755 或 775)
sudo chmod -R 755 /usr/local/prometheus/prometheus-3.5.1.linux-amd64/data/

  

接下来就是开机自动机

sudo vim /etc/systemd/system/prometheus.service

[Unit]
Description=prometheus
Documentation=https://github.com/prometheus/node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus-3.5.1.linux-amd64/prometheus \
    --config.file=/usr/local/prometheus/prometheus-3.5.1.linux-amd64/prometheus.yml \
    --storage.tsdb.path=/usr/local/prometheus/prometheus-3.5.1.linux-amd64/data \
    --web.console.templates=/usr/local/prometheus/prometheus-3.5.1.linux-amd64/consoles \
    --web.console.libraries=/usr/local/prometheus/prometheus-3.5.1.linux-amd64/console_libraries \
    --web.listen-address=:9090
Restart=on-failure
[Install]
WantedBy=multi-user.target

  

vim /etc/systemd/system/node_exporter.service

[Unit]
Description=node_exporter
Documentation=https://github.com/prometheus/node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/node_exporter/node_exporter-0.17.0.linux-amd64/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target

  

启动命令

# 重新加载 systemd 配置

sudo systemctl daemon-reload

# 启动服务

sudo systemctl start node_exporter

# 设置开机自启

sudo systemctl enable node_exporter

# 检查服务状态

sudo systemctl status node_exporter

 

 

# 重新加载 systemd 配置

 

sudo systemctl daemon-reload

 

# 启动服务

 

sudo systemctl start prometheus

 

# 设置开机自启

 

sudo systemctl enable prometheus

 

# 检查服务状态

 

sudo systemctl status prometheus

 

访问路径

 

http://192.168.175.184:9100/metrics

 

http://192.168.175.184:9090/query

 

http://192.168.175.185:9090/targets

 

http://192.168.176.185:9090/targets

 

 

posted on 2026-02-26 11:36  luzhouxiaoshuai  阅读(34)  评论(0)    收藏  举报

导航