沪ICP备2021032517号-1

Prometheus

  |   0 评论   |   0 浏览

Prometheus 组件及作用

  • Prometheus Server 用于抓取指标、存储时间序列数据
  • exporter 暴露指标让任务抓取
  • Pushgateway push的方式将指标数据推送到网关
  • alertmanager 处理报警的报警组件
  • adhoc 用于数据查询

prometheus部署

下载并解压二进制安装包

https://prometheus.io/download/

https://prometheus.io/download/
https://prometheus.io/download/

解压

tar -zxf prometheus-2.16.0.linux-amd64.tar.gz 

mv prometheus-2.16.0.linux-amd64 prometheus-2.16.0

创建prometheus的用户及数据存储目录

useradd  -s /sbin/nologin -M prometheus 

mkdir  /data/prometheus -p

chown -R prometheus:prometheus /usr/local/prometheus/

chown -R prometheus:prometheus /data/prometheus/

创建Systemd服务启动prometheus

热加载配置方式,热更新加载方法有两种:

1.  kill -HUP pid   
2.  curl -X POST http://IP/-/reload  

我们使用的是第一种热加载方式,systemd unit文件如下:

vi /etc/systemd/system/prometheus.service

[Unit]  
Description=Prometheus  
Documentation=https://prometheus.io/  
After=network.target  
[Service]  
Type=simple  
User=prometheus  
ExecStart=/usr/local/prometheus-2.16.0/prometheus --config.file=/usr/local/prometheus-2.16.0/prometheus.yml --storage.tsdb.path=/data/prometheus  
ExecReload=/bin/kill -HUP $MAINPID  
Restart=on-failure  
[Install]  
WantedBy=multi-user.target  

ExecReload=/bin/kill -HUP $MAINPID 参数表示开启热加载配置文件

ExecReload=/bin/kill -HUP $MAINPID 参数表示开启热加载配置文件
ExecReload=/bin/kill -HUP $MAINPID 参数表示开启热加载配置文件

在仅需要重新加载配置,而不需重启进程时,只需要运行 systemctl reload prometheus 即可

systemctl start prometheus
systemctl status prometheus
systemctl enable prometheus

置说明

# 全局配置
global:
  scrape_interval:     15s # 设置抓取间隔,默认为1分钟
  evaluation_interval: 15s #估算规则的默认周期,每15秒计算一次规则。默认1分钟
  # scrape_timeout  #默认抓取超时,默认为10s

# Alertmanager相关配置
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# 规则文件列表,使用'evaluation_interval' 参数去抓取
rule_files:
   - "rules/*rules.yml"
   - "rules/second_rules.yml"

#  抓取配置列表
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']   ## 这里的localhost可以修改成主机IP

检查配置文件是否书写正确

./promtool check config prometheus.yml

打开prometheus的web页面

image.png

Grafana安装

最新版安装参考

wget https://dl.grafana.com/oss/release/grafana-6.6.2-1.x86_64.rpm

yum install grafana-6.6.2-1.x86_64.rpm

systemctl start grafana-server

systemctl enable grafana-server

打开grafana的web页面

到这里,grafana已经安装完毕。默认情况下,grafana-server会启动3000端口,我们使用浏览器打开grafana页面,然后输入默认的账号密码 admin/admin登录。

在Grafana添加数据源

grafana虽然已经安装好了,但是这个时候还没有数据,没办法作图。下面我们把grafana和prometheus关联起来,也就是在grafana中添加添加数据源。

在配置页面点击添加数据源,然后选择prometheus,输入prometheus服务的参数即可

image.png

image.png

image.png

image.png

导入自带的 Prometheus Dashboards

image.png

image.png

部署Node Exporter采集主机运行数据

与传统的监控zabbix来对比的话,prometheus-server就像是mysql,负责存储数据。只不过这是时序数据库而不是关系型的数据库。数据的收集还需要其他的客户端,在prometheus中叫做exporter。针对不同的服务,有各种各样的exporter,就好比zabbix的zabbix-agent一样。

cd /usr/local/

wget https://github.com/prometheus/node_exporter/releases/download/v1.0.0-rc.0/node_exporter-1.0.0-rc.0.linux-amd64.tar.gz

tar -zxf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz

cd /usr/local/

wget https://github.com/prometheus/node_exporter/releases/download/v1.0.0-rc.0/node_exporter-1.0.0-rc.0.linux-amd64.tar.gz

tar -zxf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz
cd /usr/local/

wget https://github.com/prometheus/node_exporter/releases/download/v1.0.0-rc.0/node_exporter-1.0.0-rc.0.linux-amd64.tar.gz

tar -zxf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz

启动node exporter

直接打开node_exporter的可执行文件即可启动 node export,默认会启动9100端口。建议使用nohup来启动

/usr/local/prometheus_exporter/node_exporter/node_exporter

nohup /usr/local/prometheus_exporter/node_exporter/node_exporter >/dev/null 2>&1 &

加入开机启动

在 /etc/rc.local 加入上面的启动命令即可

#node exporter

nohup /usr/local/prometheus_exporter/node_exporter/node_exporter >/dev/null 2>&1 &

#node exporter

nohup /usr/local/prometheus_exporter/node_exporter/node_exporter >/dev/null 2>&1 &
#node exporter

nohup /usr/local/prometheus_exporter/node_exporter/node_exporter >/dev/null 2>&1 &

配置Prometheus,收集node exporter的数据

可以看到node exporter启动后也就是暴露了9100端口,并没有把数据传到prometheus,我们还需要在prometheus中配置,让prometheus去pull这个接口的数据。
编辑prometheus.yml文件,增加后面4行.

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

   #采集node exporter监控数据
  - job_name: 'node'
    static_configs:
    - targets:
      - 10.10.0.2:9100

如果后面再添加其他主机的 node exporter 增加一行 - 10.10.0.x:9100 即可,如下

  - job_name: 'node'
    static_configs:
    - targets: 
      - 10.10.0.12:9100
      - 10.10.0.2:9100

然后重新加载prometheus配置,prometheus页面就有对应的数据了。

image.png

导入grafana模板,数据展示

在导入界面,我们输入模板的编号,这里我使用的是9276号模板,如要使用其他的模板,请到grafana的官网去查找 https://grafana.com/dashboards

image.png

image.png

Prometheus黑盒监控 blackbox exporter 部署

blackbox exporter是允许在 HTTP, HTTPS, DNS, TCP and ICMP等协议的端点进行黑盒探测的采取器

官方github

wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.16.0/blackbox_exporter-0.16.0.linux-amd64.tar.gz

mv blackbox_exporter-0.16.0.linux-amd64 blackbox_exporter-0.16.0

nohup ./blackbox_exporter --config.file=/usr/local/blackbox_exporter/blackbox.yml &

默认监听端口为9115

默认监听端口为9115
默认监听端口为9115

prometheus.yml中加入blackbox_exporter

下面分别是 主机存活状态、端口状态、URL的监控

  - job_name: ICMP 监控 #监控主机存活状态
    metrics_path: /probe
    params:
      module: [icmp]
    static_configs:
      - targets: 
        - 10.10.2.33
        labels:
          instance: icmp_2.33
      - targets: 
        - 10.10.2.2
        labels:
          instance: icmp_2.2 '监控'  #中文需要''号
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - target_label: __address__
        replacement: 10.10.0.2:9115
#===============================================================
  - job_name: 'prometheus_port_status' #监控主机端口存活状态
    metrics_path: /probe
    params:
      module: [tcp_connect]
    static_configs:
      - targets: 
        - 10.10.0.23:3306
        labels:
          instance: port
          group: 'tcp'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 10.10.0.2:9115
#===============================================================
  - job_name: web_status #监控URL
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets: 
         - http://www.zifuy.cn
         - http://10.10.0.2:3000 
        labels:
          instance: user_status
          group: 'web'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - target_label: __address__
        replacement: 10.10.0.2:9115

检查配置文件是否书写正确

./promtool check config prometheus.yml

systemctl reload prometheus

./promtool check config prometheus.yml

systemctl reload prometheus
./promtool check config prometheus.yml

systemctl reload prometheus

grafana中加入blackbox_exporter监控数据

在grafana导入 7587 或者 11175 模板

在grafana导入 7587 或者 11175 模板
在grafana导入 7587 或者 11175 模板

image.png

中间件mysql监控

参考文章

说明 mysqld_exporter可以部署在Prometheus服务端,也可以部署在mysql服务所在的主机。

mysqld_exporter-0.12.1下载

cd /usr/local/

wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz

tar -zxf mysqld_exporter-0.12.1.linux-amd64.tar.gz

mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter

mysql授权mysqld_exporter客户端连接

set global validate_password_policy=0;

set global validate_password_length=4;

grant all privileges on *.* to root@"%" identified by 'root';

flush privileges;

# vim /etc/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld_exporter
After=network.target

[Service]
Environment=DATA_SOURCE_NAME=root:root@(192.168.22.22:3306)/
Restart=on-failure
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter

[Install]
WantedBy=multi-user.target

部署在mysql服务所在主机时修改上面的ip地址即可

服务管理命令

systemctl daemon-reload  
systemctl start mysqld_exporter  
systemctl status mysqld_exporter

启动后查看

http://10.10.2.30:9104/metrics

http://10.10.2.30:9104/metrics
http://10.10.2.30:9104/metrics

确保上面地址可访问后添加 prometheus 监控

添加 prometheus 监控

  - job_name: 'Msql'
    static_configs:
    - targets:
      - 10.10.0.23:9104

systemctl reload prometheus

配置 grafana 展示

导入 7362


在上面的配置项目中主机负载、icmp、tcp、http协议、中间件、包括grafana等都有大概的介绍。主要是拿到需要的各种类型的数据。

下面篇幅的内容主要介绍用拿到的数据添加可视化及配置报警

常用应用监控的grafana可视化配置

grafana是一款可视化工具,其中看板展示的内容是根据 Prometheus 查询语言进行展示

PromQL (Prometheus Query Language) 是 Prometheus 自己开发的数据查询 DSL 语言,语言表现力非常丰富,内置函数很多,在日常数据可视化以及rule 告警中都会使用到它。

下面以在grafana 6.0版本上配置展示一组 ICMP协议的监控为例介绍grafana的简单用法

比如定义的下面一组icmp协议监控项

  - job_name: icmp
    metrics_path: /probe
    params:
      module: [icmp]
    static_configs:
      - targets: 
        - 10.10.2.35
        labels:
          instance: icmp_2.35
      - targets: 
        - 10.10.0.27
        labels:
          instance: icmp_0.27
      - targets: 
        - 10.10.0.30
        labels:
          instance: icmp_0.30
      - targets: 
        - 10.10.2.2
        labels:
          instance: icmp_2.2
      - targets: 
        - 35.221.192.89
        labels:
          instance: icmp_google

注意上面的信息 在grafana面板配置展示的时候我们可以根据上面配置文件中的 job、instance 筛选需要的值

点击左侧的 + ---> New dashboard--->Chosse Visualization

再点击右上部位的设置,如下

image.png

切换到 Variables 点击 Add variable

image.png

Query 写法例子:

  1. 以 label 值的 instance 筛选

image.png

Name 自定义

Type Query

Data source 选择 Prometheus

Refresh 选择 On Dashbord Load

Query 这个是关键,查询语句作用是你可以通过该条件山选出你需要的值

上面就筛选出了所有 instance 的值的项目


如果再筛选结果中进一步筛选,可以在上面的 Regex 中处理,如下

image.png

这样就实现了更详细的匹配

  1. 以 job 筛选

image.png

这样就实现了更大范围的筛选

如果要具体到某一个 job ,在 Regex 中 写入某个 job 的名字

image.png

变量配置完成后点击 Add--->Save--->输入看板名称 即可,变量设置完成后看板还没有数值展示,这里还没有设置完成 还需要具体设置看板的内容,如下

image.png

打开编辑

image.png

image.png
我上面这张图是单位设置好的

设置一下单位。如下

image.png

最后设置一下看板名称

image.png

最后注意保存已设置的面板,这样就完成了icmp信息的展示

PromQL 基本使用 参考

常用监控项grafana展示配置

image.png

上面这张图展示了URL的:

icmp响应时间

是否可用(是否在线)

状态码

SSL过期时间

下面就来说说如何设置这样一个可视化看板

  1. 监控项配置
  - job_name: url
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets: 
        - https://www.zifuy.xyz
        labels:
          instance: 'https://www.zifuy.xyz'
      - targets: 
        - https://www.baidu.com
        labels:
          instance: 'https://www.baidu.com'
      - targets: 
        - http://www.zifuy.cn
        labels:
          instance: 'http://www.zifuy.cn'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - target_label: __address__
        replacement: 10.10.0.2:9115

上面定义了使用黑盒监控三个URL地址

可视化配置界面

  1. 添加可视化面板、Visualization 选择Table

image.png

  1. 添加列,Value #A、 Value #B 、Value #C、 Value #D

image.png

image.png

image.png

上面三个图六个模块必须都要设置正确,注意参数。

再到 Queries设置筛选匹配参数

image.png

这里设置完成后,就会出现开始上面的可视化看板。

将url替换成你的job名即可

probe_success{job=~"url"} - 0

probe_http_ssl{job=~"url"} - 0

probe_http_status_code{job=~"url"} - 0

probe_ssl_earliest_cert_expiry{job=~"url"}-time()

prometheus监控docker容器

参考

docker run --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --volume=/dev/disk/:/dev/disk:ro --publish=8080:8080 --detach=true --name=cadvisor google/cadvisor:latest

docker run --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --volume=/dev/disk/:/dev/disk:ro --publish=8080:8080 --detach=true --name=cadvisor google/cadvisor:latest
docker run --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --volume=/dev/disk/:/dev/disk:ro --publish=8080:8080 --detach=true --name=cadvisor google/cadvisor:latest

prometheus.yml添加

  - job_name: 'node'
    static_configs:
    - targets:
      -   ---
      - 10.10.2.31:8080

grafana导入 179

或者自己添加常用监控指标

容器CPU使用率:
sum(irate(container_cpu_usage_seconds_total{image!=""}[1m])) without (cpu)

查询容器内存使用量(单位:字节):
container_memory_usage_bytes{image!=""}

查询容器网络接收量速率(单位:字节/秒):
sum(rate(container_network_receive_bytes_total{image!=""}[1m])) without (interface)

查询容器网络传输量速率(单位:字节/秒):
sum(rate(container_network_transmit_bytes_total{image!=""}[1m])) without (interface)

查询容器文件系统读取速率(单位:字节/秒):
sum(rate(container_fs_reads_bytes_total{image!=""}[1m])) without (device)

查询容器文件系统写入速率(单位:字节/秒):
sum(rate(container_fs_writes_bytes_total{image!=""}[1m])) without (device)

看让板只显示容器 name

image.png

Legend 写上 {{ name }}

报警配置

alertmanager部署

wget https://github.com/prometheus/alertmanager/releases/download/v0.19.0/alertmanager-0.19.0.linux-amd64.tar.gz  
  
tar xf alertmanager-0.19.0.linux-amd64.tar.gz  
  
mv alertmanager-0.19.0.linux-amd64 alertmanager-0.19.0  
  
chown prometheus:prometheus /usr/local/prometheus-2.16.0/alertmanager -R  

启动

nohup ./alertmanager > /dev/null &

部署完毕alertmanager, 需要告知prometheus告警信息推送的位置, 通过如下配置即可完成

vi prometheus.yml

alerting:  
 alertmanagers:  
 - static_configs:  
 - targets:  
 - 10.10.0.2:9093  

创建报警规则

这里添加一个磁盘根目录的容量报警规则。使用量大于 50就触发报警

mkdir rules  
  
cd  rules  

vi node_rules

groups:  
 - name: node-alert  
 rules:  
 - alert: disk-full  
 expr: 100 - ((node_filesystem_avail_bytes{mountpoint="/",fstype=~"ext4|xfs"} * 100) / node_filesystem_size_bytes {mountpoint="/",fstype=~"ext4|xfs"}) >50  
 for: 1m  
 labels:   
 serverity: page  
 annotations:   
 summary: "{{ $labels.instance }} disk full "  
 description: "{{ $labels.instance }} disk > {{ $value }}  "  

配置 prometheus 配置如下部分

alerting:  
 alertmanagers:  
 - static_configs:  
 - targets:  
   - 10.10.0.2:9093  

重启 prometheus,可以在web界面看到如下信息

image.png

如下已检测到触发报警值。

image.png

登录 alertmanager

15837474741.png

已有告警信息

配置告警信息接收

----待续