监控简介

  • prometheus是由SoundCloud开源的监控报警解决方案
  • 采用go语言开发
  • prometheus存储的是时序数据
  • 数据带时间标签
  • Prometheus主要用在监控容器数据 也可以监控常规主机
  • Prometheus重视高可用 如果您需要100%准确性 那么该软件不适合您 因为它所收集的数据可能不会足够详细和完整
  • Prometheus是一个框架 可以与其他组件完美组合


Prometheus监控服务器 被监控段 Grafana监控可视化 自动发现和告警_服务器


部署Prometheus服务器

  • 环境说明:
  • Prometheus 192.168.88.5
  • web1 192.168.88.100

1.配置时间

# 1. 查看时区
[root@prometheus ~]# timedatectl 
               Local time: Sun 2023-01-01 11:15:11 CST
           Universal time: Sun 2023-01-01 03:15:11 UTC
                 RTC time: Sun 2023-01-01 03:15:11
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
              NTP service: inactive
          RTC in local TZ: no

# 2. 如果时区不正确,则改为正确的时区
[root@prometheus ~]# timedatectl set-timezone Asia/Shanghai

# 3. 查看时间
[root@prometheus ~]# date

# 4. 如果时间不正确,则改为正确的时间
[root@prometheus ~]# date -s "年月日 时:分:秒"

2.安装Prometheus服务器 

  • 1 拷贝相关软件包到服务器
  • 2.解压即部署

配置文件

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

# 启动服务
[root@prometheus prometheus_soft]# systemctl daemon-reload 
[root@prometheus prometheus_soft]# systemctl enable prometheus.service --now
[root@prometheus prometheus_soft]# ss -tlnp | grep :9090
LISTEN 0      128                *:9090             *:*    users:(("prometheus",pid=4396,fd=7))




添加被监控端

  • 监控方式:
  • 拉取:pull。监控端联系被监控端,采集数据
  • 推送:push。被监控端主动把数据发给监控端。在prometheus中,push的方式需要额外的组件pushgateway

部署通用的监控exporter

  • node-exporter用于监控硬件和系统的常用指标
  • exporter运行于被监控端,以服务的形式存在。每个exporter所使用的端口号都不一样。
  • 在web1[192.168.88.100]上部署node exporter
# 1. 拷贝node_exporter到web1
[root@prometheus ~]# scp prometheus_soft/node_exporter-1.5.0.linux-amd64.tar.gz 192.168.88.100:/root/

# 2. 解压即部署
[root@web1 ~]# tar xf node_exporter-1.5.0.linux-amd64.tar.gz 
[root@web1 ~]# mv node_exporter-1.5.0.linux-amd64 /usr/local/node_exporter

# 3. 创建服务文件,并启动服务
[root@web1 ~]# 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

[root@web1 ~]# systemctl daemon-reload 
[root@web1 ~]# systemctl enable node_exporter.service --now
[root@web1 ~]# ss -tlnp | grep :9100
LISTEN 0      128                *:9100             *:*    users:(("node_exporter",pid=7371,fd=3))
  • 在Prometheus服务器上添加监控节点
# 1. 修改配置文件,追加以下内容。特别注意缩进
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml 
...略...
  - job_name: "web1"
    static_configs:
      - targets: ["192.168.88.100:9100"]
      
# 2. 重启服务
[root@prometheus ~]# systemctl restart prometheus.service
  • 查看添加结果

Grafana

概述

  • Grafana是一款开源的、跨平台的、基于web的可视化工具
  • 展示方式:客户端图表、面板插件
  • 数据源可以来自于各种源,如prometheus

部署Grafana

  • 装包、启服务
[root@prometheus ~]# yum install -y prometheus_soft/grafana-enterprise-9.3.2-1.x86_64.rpm
[root@prometheus ~]# systemctl enable grafana-server.service --now
  • 初始化。访问http://192.168.88.5:3000。初始用户名和密码都是admin。第一次登陆时,要求改密码,本例中密码改为tedu.cn。如果登陆报错,请更换其他浏览器。

Prometheus监控服务器 被监控段 Grafana监控可视化 自动发现和告警_vim_02

  • 修改主题

Prometheus监控服务器 被监控段 Grafana监控可视化 自动发现和告警_服务器_03

Prometheus监控服务器 被监控段 Grafana监控可视化 自动发现和告警_服务器_04

  • 对接Prometheus
  • 添加仪表盘
  • 查看仪表盘