Prometheus

概述

一、监控简介

Prometheus是由SoundCloud开源的监控报警解决方案。

  • 采用Go语言开发
  • prometheus存储的是时序数据(时序数据库),即指标信息与记录时的时间戳以及称为标签的可选键值对一起存储
  • 数据带时间标签
  • 如<metric name>{<label name>=<label value>, ...}
  • Prometheus主要用在监控容器数据,也可以监控常规主机
  • Prometheus重视高可用,如果您需要100%准确性、那么该软件不适合您,因为它所收集的数据可能不会足够详细和完整

二、监控拓扑

1、Prometheus架构图

部署监控服务器

一、配置时间

# 查看时区
[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

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

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

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

二、安装软件

# 拷贝Prometheus相关软件包到服务器
[root@prometheus ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.45.4/prometheus-2.45.4.linux-amd64.tar.gz

# 解压即可直接执行
[root@prometheus ~]# tar -xf prometheus-2.45.4.linux-amd64.tar.gz -C /usr/local/prometheus

三、配置文件

配置文件为prometheus.yml

配置文件中包含三个配置块:global、rule_files和scrape_configs

  • global 块控制 Prometheus 服务器的全局配置(我们有两个选择。第一个,scrape_interval 控制 Prometheus 抓取目标的频率,您可以为单个目标覆盖它。在这种情况下,全局设置是每15秒抓取一次。该 evaluation_interval 选项控制 Prometheus 评估规则的频率。)
  • rule_files 块指定我们希望 Prometheus 服务器加载的任何规则的位置
  • scrape_configs 控制 Prometheus 监控的资源。由于 Prometheus 还将有关自身的数据公开为HTTP端点,因此它可以抓取和监控自身的健康状态。在默认配置中,有一个作业prometheus,用于抓取Prometheus服务器公开的时间序列数据。该作业包含一个单一的、静态配置的目标,即localhost的9090端口。Prometheus期望度量在/metrics路径上的目标上可用。
    所以这个默认作业是通过URL抓取的:http://localhost:9090/metrics。

四、systemd

1、新建service文件,启动服务(默认端口9090)

[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 ~]# systemctl daemon-reload
[root@prometheus ~]# systemctl enable prometheus.service --now
[root@prometheus ~]# ss -ntulp | grep 9090
tcp   LISTEN 0      128                *:9090            *:*    users:(("prometheus",pid=1483,fd=7))

五、prometheus控制台

1、浏览器访问prometheus控制台

http://192.168.88.5:9090/

查看主机状态

查看某项监控数据

go_memstats_alloc_bytes

查看监控图形 

被监控端

一、监控方式

1、Pull:

服务端主动向客户端拉取数据,这样需要客户端上安装exporters(导出器)作为守护进程

2、Push:

客户端需要安装pushgateway插件,然后运维人员用脚本把监控数据组织成键值形式提交给pushgateway,再由它提交给服务端

二、部署通用的监控exporter

node-exporter用于监控硬件和系统的常用指标

exporter运行于被监控端,以服务的形式存在。每个exporter所使用的端口号都不一样

# 解压即可直接运行
[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

# 创建service文件,并启动服务
[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 -ntulp | grep 9100
tcp   LISTEN 0      128                *:9100            *:*    users:(("node_exporter",pid=18775,fd=3))

三、在Prometheus服务器上添加监控节点 

# 修改配置文件,追加以下内容。注意缩进
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
scrape_configs:
... ...
  - job_name: "web1-hw"
    static_configs:
      - targets: ["192.168.88.100:9100"]

[root@prometheus ~]# systemctl restart prometheus.service

四、查看控制台

1、查看监控主机状态

2、查看监控数据

Grafana

一、部署

[root@prometheus ~]# yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-9.3.2-1.x86_64.rpm
[root@prometheus ~]# systemctl enable grafana-server.service --now
[root@prometheus ~]# ss -ntulp | grep 3000
tcp   LISTEN 0      128                *:3000            *:*    users:(("grafana-server",pid=2586,fd=11))

二、访问

http://192.168.88.5:3000/

1、初次登录

初始用户名和密码都是admin。第一次登陆时,要求改密码

2、偏好设置

3、添加数据源(有数据才能生成图形)

4、导入dashboards模板

主机监控的图形

一、导入图形模板

二、数据源选择Prometheus的数据源

三、查看主机数据图

监控数据库

一、安装软件

[root@web1 ~]# yum -y install mysql-server
[root@web1 ~]# systemctl enable mysqld --now 

二、配置数据库账户和密码

[root@web1 ~]# mysql
mysql> create user testuser@localhost identified by '123456';
mysql> grant all privileges on *.* to testuser@localhost;
mysql> flush privileges;
mysql> quit

三、安装数据库exporter

安装exporter,创建访问数据库的账户密码配置文件

# 安装exporter
[root@web1 ~]# tar -xf mysqld_exporter-0.14.0.linux-amd64.tar.gz 
[root@web1 ~]# mv mysqld_exporter-0.14.0.linux-amd64 /usr/local/mysqld_exporter

# 编写用于连接mysql服务的配置文件
[root@web1 ~]# vim /usr/local/mysqld_exporter/.my.cnf
[client]
host=127.0.0.1
port=3306
user=testuser
password=123456

# 创建service文件
[root@web1 ~]# vim /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld_export
After=network.target

[Service]
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf

[Install]
WantedBy=multi-user.target

[root@web1 ~]# systemctl daemon-reload 
[root@web1 ~]# systemctl start mysqld_exporter.service 
[root@web1 ~]# ^start^enable
[root@web1 ~]# ss -ntulp | grep mysqld_exporter
tcp   LISTEN 0      128                *:9104             *:*    users:(("mysqld_exporter",pid=4689,fd=3))

四、修改Prometheus配置

[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
... ...
  - job_name: "mysql"
    static_configs:
      - targets: ["192.168.88.100:9104"]

[root@prometheus ~]# systemctl restart prometheus.service

五、查看监控主机

六、导入数据库可视化模块

自动发现和告警

一、自动发现

1、概述

自动发现是指Prometheus自动对节点进行监控,不需要手动逐个添加,和Zabbix的自动发现、自动注册一个道理。

Prometheus有多种自动发现,比如 file_sd_configs 基于文件自动发现、基于 K8S 自动发现、基于 openstack 自动发现、基于 consul 自动发现。

2、基于文件自动发现

file_sd_configs 实现文件级别的自动发现。

使用文件自动发现功能后,Prometheus 会定期检查配置文件是否有更新

如果有更新的话就将新加入的节点接入监控,服务端无需重启服务

3、修改Prometheus使用自动发现

# 备份现有配置文件
[root@prometheus ~]# cp /usr/local/prometheus/prometheus.yml /usr/local/prometheus/prometheus.yml.bak

# 修改配置文件,删除静态配置,添加自动发现配置
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
scrape_configs:
  - job_name: "prometheus"
    file_sd_configs:
      - refresh_interval: 120s
        files:
          - /usr/local/prometheus/sd_config/*.yml

# 重启服务
[root@prometheus ~]# systemctl restart prometheus.service

4、web中将没有任何监控项目

5、创建自动发现规则文件

[root@prometheus ~]# mkdir /usr/local/prometheus/sd_config

[root@prometheus ~]# vim /usr/local/prometheus/sd_config/discovery.yml
- targets:
    - 192.168.88.5:9090
    - 192.168.88.100:9100
    - 192.168.88.100:9104

6、查看结果

二、Alertmanager

1、概述

Prometheus服务器中的告警规则向Alertmanager发送告警,然后,Alertmanager管理这些告警,包括静默、抑制、分组以及通过电子邮件、即时消息系统和聊天平台等方法发出通知

2、告警和通知主要步骤

设置和配置Alertmanager;

配置Prometheus与Alertmanager对接;

在Prometheus中创建告警规则;

3、告警规则构成

告警名称:用户需要为告警规则命名;

告警规则:告警规则实际上主要由PromQL进行定义,其实际意义是当表达式(PromQL)查询结果持续多长时间(During)后发出告警;

4、Alertmanager特性

Alertmanager 处理客户端应用程序(如Prometheus服务器)发送的警报。它负责重复数据删除、分组,并将其路由到正确的接收方集成;

分组:分组将性质相似的警告分类到单个通知中。这在较大的停机期间特别有用,此时许多系统同时发生故障,数百到数千个警告可能同时发生;

抑制:抑制是当某一告警发出后,可以停止重复发送由此告警引发的其它告警的机制;

静默提供了一个简单的机制可以快速根据标签对告警进行静默处理。如果接收到的告警符合静默的配置,Alertmanager则不会发送告警通知。静默设置需要在Alertmanager的Web页面上进行设置;

5、Alertmanager部署

# 解压即部署
[root@prometheus ~]# tar -xf alertmanager-0.25.0.linux-amd64.tar.gz 
[root@prometheus ~]# mv alertmanager-0.25.0.linux-amd64 /usr/local/alertmanager

# 编写服务文件并启动
[root@prometheus ~]# vim /usr/lib/systemd/system/alertmanager.service
[Unit]
Description=alertmanager System

[Service]
ExecStart=/usr/local/alertmanager/alertmanager --config.file=/usr/local/alertmanager/alertmanager.yml

[Install]
WantedBy=multi-user.target

[root@prometheus ~]# systemctl daemon-reload 
[root@prometheus ~]# systemctl enable alertmanager.service --now
[root@prometheus ~]# ss -ntulp | grep 9093
tcp   LISTEN 0      128                *:9093            *:*    users:(("alertmanager",pid=1273,fd=8))

6、访问

7、配置文件构成

Alertmanager的配置主要包括两个部分:路由(route)以及接收器(receivers)。所有的告警信息都会从配置中的顶级路由(route)进入路由树,根据路由规则将告警信息发送给相应的接收器;

在Alertmanager中可以定义一组接收器;

目前配置文件中只设置了一个顶级路由route并且定义的接收器为default-receiver;

因此在Alertmanager配置中一般会包含以下几个主要部分:

  • 全局配置(global):用于定义一些全局的公共参数
  • 模板(templates):用于定义告警通知时的模板
  • 告警路由(route):根据标签匹配,确定当前告警应该如何处理
  • 接收器(receivers):接收器是一个抽象的概念,它可以是一个邮箱也可以是一个微信,webhook等,接收器一般配合告警路由使用
  • 抑制规则(inhibit_rules):合理设置抑制规制可以减少垃圾告警的产生

8、与Prometheus对接

# 编辑Prometheus配置文件,修改alerting配置
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          - localhost:9093

[root@prometheus ~]# systemctl restart prometheus.service 

访问 http://192.168.88.5:9090/config

查看配置是否生效

9、配置Alertmanager通过邮件发送告警

# 备份配置文件
[root@prometheus ~]# cp /usr/local/alertmanager/alertmanager.yml /usr/local/alertmanager/alertmanager.yml.bak

# 修改配置文件
[root@prometheus ~]# vim /usr/local/alertmanager/alertmanager.yml 
global:
  smtp_from: 'test@admin.cn'       # 发件人地址
  smtp_smarthost: 'localhost:25'   # 邮件服务器地址
  smtp_require_tls: false          # 是否使用TLS安全连接
route:
  group_by: ['alertname']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 1h
  receiver: 'default-receiver'  # 接收器
receivers:
  - name: 'default-receiver'    # 配置接收器为邮件
    email_configs:
      - to: 'root@localhost.localdomain'
inhibit_rules:
  - source_match:
      severity: 'critical'
    target_match:
      severity: 'warning'
    equal: ['alertname', 'dev', 'instance']

# 定义告警规则
[root@prometheus ~]# mkdir /usr/local/prometheus/rules
[root@prometheus ~]# vim /usr/local/prometheus/rules/hoststats-alert.rules
groups:
- name: example
  rules:

  - alert: InstanceDown
    expr: up == 0
    for: 5m 
    labels:
      severity: warn
    annotations:
      summary: "Instance {{ $labels.instance }} down"
      description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."

  - alert: hostMemUsageAlert
    expr: (node_memory_MemTotal - node_memory_MemAvailable)/node_memory_MemTotal > 0.85
    for: 1m 
    labels:
      severity: warn
    annotations:
      summary: "Instance {{ $labels.instance }} MEM usgae high"

# 在Prometheus中声明规则文件位置
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml 
...略...
 15 rule_files:
 16   - /usr/local/prometheus/rules/*.rules
...略...

# 重启服务
[root@prometheus ~]# systemctl restart alertmanager.service 
[root@prometheus ~]# systemctl restart prometheus.service 

# 安装并启动邮件服务
[root@prometheus ~]# yum install -y postfix mailx
[root@prometheus ~]# systemctl enable postfix --now

10、查看加载的规则文件

11、测试告警

# 将web1关机
[root@web1 ~]# poweroff

# 查看邮件
[root@prometheus ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 test@admin.cn         Wed Mar 27 16:56 263/12896 "[FIRING:2] Instanc"
& 1
Message  1:
... ...
Subject: [FIRING:2] InstanceDown (prometheus warn)
... ...

将告警邮件内容从【<!DOCTYPE ...】这一行到【</html>】复制,粘贴到一个文件中,如文件名为alert.html,用firefox打开,可以得到以下页面:

12、查看Alertmanager

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值