Permetheus简介

prometheus server 是Prometheus组件中的核心部分,负责实现对监控数据的获取,存储以及查询。

exporter简单说是采集端,通过http服务的形式保留一个url地址,prometheus server通过访问该exporter提供的endpoint端点,即可获取到需要采集的监控数据。

AlertManager
在prometheus中,支持基于PromQL常见告警规则,如果满足定义的规则,则会产生一条告警信息,进入AlertManager进行处理。可以集成邮件,微信或者通过webhook自定义报警。

Pushgateway
由于Prometheus数据采集采用pull方式进行设置的,内置必须保证prometheus server和对应的exporter必须通信,当网络情况无法直接满足时,可以使用pushgateway来进行中转,可以通过pushgateway将内部网络数据主动push到gateway里面去,而prometheus采用pull方式拉取pushgateway中数据。

总结
prometheus负责从pushgateway和job在红采集数据,存储到后端storatge中,可以通过PromQL进行查询,推送alerts信息到AlertManager。AlertManager根据不同的路由规则进行报警通知。

Prometheus部署

官网下载官网

  1. tar -zxf prometheus-2.35.0-rc0.linux-amd64.tar.gz
  2. mv prometheus-2.35.0-rc0.linux-amd64 /usr/local/prometheus
  3. mkdir /usr/local/prometheus/data
  4. cd /usr/local/prometheus
  5. ./promtool check config prometheus.yml #拍错工具
  6. vim /usr/lib/systemd/system/prometheus.service #创建配置文件
  7. [Unit]
  8. Description=https : //prometheus.io
  9. [Service]
  10. Restart=on-failure
  11. ExecStart=/usr/local/prometheus/prometheus \
  12. --storage.tsdb.path=/usr/local/prometheus/data \
  13. --config.file=/usr/local/prometheus/prometheus.yml
  14. [Install]
  15. WantedBy=multi-user.target
  16. systemctl start prometheus
  17. systemctl status prometheus或者netstat -ntlp #查看是否启动
  18. ntpdate pool.ntp.org #配置时间同步

image.png

image.png
image.png

Prometheus配置文件介绍

  1. cd /usr/local/prometheus
  2. vim prometheus.yml

global: 全局配置文件

  • scrape_interval: 15s #间隔采集
  • evaluation_interval: 15s #抓取超时间隔

alerting: 报警模块

  • alertmanagers:
  • static_configs:
    • targets:
    • - alertmanager:9093

rule_file: 配置规则,用来根据规则推送报警信息

scrape_configs: 指定抓取配置,数据采集通过此片段配置

static_configs: 静态指定服务job

PromQL

Prometheus提供了一种名为PromQL (Prometheus查询语言)的函数式查询语言,允许用户实时选择和聚合时间序列数据。表达式的结果既可以显示为图形,也可以在 Prometheus的表达式浏览器中作为表格数据查看,或者通过HTTP API由外部系统使用。

主机监控

搭建node节点

  1. tar -zxf node_exporter-1.3.1.linux-amd64.tar.gz
  2. mv node_exporter-1.3.1.linux-amd64 /usr/local/node_exporter
  3. vim /usr/lib/systemd/system/node_exporter.service
  4. [Unit]
  5. Description=Prometheus node_exporter
  6. [Service]
  7. User=nobody
  8. ExecStart=/usr/local/node_exporter/node_exporter --log.level=error
  9. ExecStop=/usr/bin/killall node_exporter
  10. [Install]
  11. WantedBy=default.target
  12. systemctl restart node_exporter

添加到prometheus监控中

  1. vim /usr/local/prometheus/prometheus.yml
  2. # 额外添加
  3. - job_name: "host_monitor"
  4. static_configs:
  5. - targets: ["localhost:9100"]
  6. systemctl restart prometheus
  7. # 如果想要添加其他服务器的监控 可以通过上面配置添加 比如:
  8. - job_name: "node01"
  9. static_configs:
  10. - targets: ["192.168.218.133:9100"]
  11. # 本质上就是安装包后,会新开一个api接口,prometheus去对接接口就完成了监控。

此时就多一个节点
image.png
此时就显示了更多数据
image.png

  1. #带标签的查询
  2. node_cpu_seconds_total #查询全部
  3. node_cpu_seconds_total{mode="idle"} #单个标签查询
  4. node_cpu_seconds_total{mode="user",cpu="0"} #多个标签查询
  5. 100 - (avg(irate(node_cpu_seconds_total{mode="idle"}[5m]))by (instance) * 100) #查询5分钟cpu的使用率
  6. 100 - (node_memory_MemFree_bytes+node_memory_Cached_bytes+node_memory_Buffers_bytes) / node_memory_MemTotal_bytes * 100 #查看内存使用率
  7. 100 - (((node_filesystem_size_bytes{fstype=~"xfs|ext4"}-node_filesystem_free_bytes{fstype=~"xfs|ext4"})/node_filesystem_size_bytes{fstype=~"xfs|ext4"}) * 100)
  8. #查看磁盘使用率

监控Nacos

  1. # 暴露metrics数据
  2. vim conf/application.properties
  3. management.endpoints.web.exposure.include=*
  4. # linux & mac
  5. vim prometheus.yml
  6. - job_name: "nacos"
  7. metrics_path: '/nacos/actuator/prometheus' # nacos的metrics路径
  8. static_configs:
  9. - targets: ["localhost:8848"]

image.png

image.png