0
点赞
收藏
分享

微信扫一扫

云原生监控|Prometheus

一、背景

Prometheus是由SoundCloud开发的开源监控系统的开源版本。2016年,由Google发起的Linux基金会(Cloud Native Computing Foundation,CNCF)将Prometheus纳入其第二大开源项目。Prometheus在开源社区也十分活跃,成为受欢迎度仅次于 Kubernetes 的项目

Prometheus 原理:

Prometheus 的基本原理是通过周期性地从各种目标(如应用程序、服务、数据库等)拉取指标数据,并将其存储在本地数据库中。然后,可以使用 PromQL(Prometheus 查询语言)对存储的指标数据进行查询和聚合,并设置警报规则,以便在指标超过或低于阈值时触发警报。 <br>

Prometheus 架构图与核心组件:

image.png

  • Prometheus Server:Prometheus Server 负责拉取指标数据并存储在本地的时间序列数据库中。它还处理查询和警报规则,并触发警报。
  • Exporters:Exporters 是用于从各种目标中获取指标数据的插件。Prometheus 社区提供了许多常用的 Exporters,如 Node Exporter(用于监控主机)、Blackbox Exporter(用于网络探测)等。此外,用户还可以编写自定义 Exporters 来收集自定义应用程序的指标数据。
  • Alertmanager:Alertmanager 负责处理 Prometheus Server 生成的警报,并将其发送给配置的接收器(如电子邮件、PagerDuty、Slack 等)。

除了以上核心组件外,Prometheus 还有一些其他工具,如 Grafana(用于可视化监控数据)、Pushgateway(用于推送临时性的指标数据)等。

Prometheus 的特点

普罗米修斯对传统监控告警模型进行了彻底的颠覆

  • 易于管理:核心部分只有单独二进制文件,不存在第三方依赖,只需要本地磁盘,没有级联故障风险
  • 基于Pull模型的架构模式,可以在任意地方搭建监控系统,对于一些复杂的情况,还可以使用 Prometheus 服务发现(Service Discovery)的能力动态管理监控目标。

高效 对于监控系统而言,大量的监控任务必然导致有大量的数据产生。而 Prometheus 可以高效地处理这些数据,对于单一 Prometheus Server 实例而言它可以处理:

  1. 数以百万的监控指标
  2. 每秒处理数十万的数据点

可视化

  1. Prometheus Server 中自带的 Prometheus UI,可以方便地直接对数据进行查询,并且支持直接以图形化的形式展示数据。同时 Prometheus 还提供了一个独立的基于Ruby On Rails 的 Dashboard 解决方案 Promdash。
  2. 最新的 Grafana 可视化工具也已经提供了完整的 Prometheus 支持,基于 Grafana 可以创建更加精美的监控图标。
  3. 基于 Prometheus 提供的 API 还可以实现自己的监控可视化 UI。

二、部署 Prometheus

以下部署只用到了两个组件,由于环境关系,其他组件可自行搭建学习

环境:

类型 IP 组件
服务端 192.168.100.100 prometheus
客户端 192.168.100.101 node-exporter
客户端 192.168.100.102 node-exporter

2.1 服务端配置

创建prometheus用户和用户组

[root@localhost ~]# groupadd prometheus
[root@localhost ~]# useradd prometheus -g prometheus

安装prometheus服务

#将下载好的prometheus-2.10.0.linux-amd64.tar.gz传到服务端的/usr/local/目录下
#解压源码包
[root@localhost local]# tar -zxvf prometheus-2.10.0.linux-amd64.tar.gz
[root@localhost local]# mkdir /etc/prometheus
[root@localhost local]# cp /usr/local/prometheus-2.10.0.linux-amd64/prometheus.yml /etc/prometheus/prometheus.yml

创建启动文件

[root@localhost local]# vim /etc/systemd/system/prometheus.service
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus-2.10.0.linux-amd64/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus
Restart=on-failure
[Install]
WantedBy=multi-user.target

启动prometheus服务

[root@localhost ~]# mkdir /data/prometheus
[root@localhost ~]# chown -R prometheus:prometheus /data/prometheus
[root@localhost ~]# chown -R prometheus:prometheus /etc/prometheus
[root@localhost ~]# systemctl start prometheus

测试

浏览器访问 http://ip:9090

2.2 客户端配置

创建prometheus用户和用户组

[root@localhost ~]# groupadd prometheus
[root@localhost ~]# useradd prometheus -g prometheus

安装node_exporter服务

#将下载好的node_exporter-0.18.1.linux-amd64.tar.gz 文件上传到客户端的/usr/local/目录下
#解压源码包
[root@localhost local]# tar -zxvf node_exporter-0.18.1.linux-amd64.tar.gz
[root@localhost local]# vim /etc/systemd/system/node_exporter.service

配置启动文件

[Unit]
Description=node_exporter
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter-0.18.1.linux-amd64/node_exporter --web.listen-address=:9091
Restart=on-failure
[Install]
WantedBy=multi-user.target

启动服务

[root@localhost local]# systemctl start node_exporter.service
[root@localhost local]# systemctl enable node_exporter.service
[root@localhost local]# systemctl status node_exporter.service

2.3 服务端加入客户端

修改prometheus配置

[root@dev local]# vim /etc/prometheus/prometheus.yml
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'prometheus'

# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.

static_configs:
- targets: ['localhost:9090']
- job_name: 'file_monitor'
file_sd_configs:
- files:
- /etc/prometheus/file/target.json

注意!!!! 一定要注意格式,与上方的配置对齐,一旦不对齐,服务无法启动

[root@dev ~]# mkdir /etc/prometheus/file
[root@dev ~]# vim /etc/prometheus/file/target.json
[{
targets: [192.168.100.101:9091],
labels: {
instance: 192.168.100.101,
job: node,
area:Client
}
},{
targets: [192.168.100.102:9091],
labels: {
instance: 192.168.100.102,
job: node,
area:Client
}
}]

添加授权,重启prometheus

[root@localhost ~]# chown -R prometheus:prometheus /etc/prometheus
[root@localhost ~]# systemctl restart prometheus

测试访问

浏览器访问 http://ip:9090
举报

相关推荐

0 条评论