【prometheus学习过程】

发布于:2024-04-26 ⋅ 阅读:(20) ⋅ 点赞:(0)

一、linux服务器监控

环境介绍:

主机 说明
主服务器监控 安装prometheus
被监控设备 安装node_exporter

被监控设备安装完成后测试:http://192.168.85.133:9100/metrics

然后在监控服务器上的prometheus.yml文件中添加地址:

#node-exporter配置
  - job_name: 'node-exporter'
    scrape_interval: 15s
    static_configs:
    - targets: ['192.168.85.141:9190']
      labels:
        instance: Prometheus服务器
    - targets: ['192.168.85.133:9100']
      labels:
        instance: 被监控的服务器 

然后加载配置:

curl -X POST http://192.168.85.141:9090/-/reload

常用的监控指标

cpu采集:
node_cpu_seconds_total

名称 含义
node_load1 一分钟内cpu负载
node_load5 五分钟内cpu负载
node_load15 15分钟内cpu负载

内存采集
/proc/meminfo文件
mode_memory_

名称 含义
node_memory_MemTotal_bytes 内存总大小
node_memory_MemAvailable_bytes 空闲可使用的内存大小(=free +buffer+cache)
node_memory_MemFree_bytes 空闲物理内存大小
node_memory_SwapFree_bytes node_memory_SwapFree_bytes
node_memory_SwapTotal_bytes swap内存总大小

磁盘/文件系统采集

名称 含义
node_filesystem_avail_bytes 空闲磁盘大小,单位字节
node_filesystem_size_bytes 磁盘总大小
node _filesystem_files_free 空闲inode大小,单位个
node_filesystem_files inode总大小,单位个

网络采集
node_network_

名称 含义
node_network_transmit_bytes_total 网络流出流量,单位字节(Byte),/1024/1024=Mb/s
node_network_receive_bytes_total 网络流入流量,单位字节(Byte)

二、监控docker

为了能够获取到Docker容器的运行状态,用户可以通过Docker的stats命令获取到当前主机上运行容器的统计信息,可以查看容器的cpu利用率、内存使用量、网络IO总量以及磁盘IO总量等信息。

docker stats

除了使用命令以外,用户还可以通过Docker提供的HTTP API查看容器详细的监控统计信息。

1、使用CAdvisor

CAdvisor是Google开源的一款用于展示和分析容器运行状态的可视化工具。通过在主机上运行CAdvisor用户可以轻松的获取到当前主机上容器的运行统计信息,并以图表的形式向用户展示。

docker run \
  --volume=/:/rootfs:ro \
  --volume=/var/run:/var/run:ro \
  --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 \
  --privileged \
  --device=/dev/kmsg \
  lagoudocker/cadvisor:v0.37.0

好消息是CAdvisor已经内置了对Prometheus的支持。访问 http://ip地址:8080/metrics 即可获取到标准的Prometheus监控样本输出.

2.配置prometheus采集docker的样本数据

#修改配置
vim /opt/prometheus/prometheus/prometheus.yml  
  - job_name: 'cadvisor'
    scrape_interval: 15s
    static_configs:
    - targets: ['192.168.85.133:8080']
      labels:
        instance: 被监控的docker服务器

3.添加触发器

rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
  - "alert.yml"
  - "rules/*.yml"   #把触发器规则文件写在rulers目录下

3.添加触发器

去grafana创建仪表盘的时候选择11600

11600

三、监控MySQL容器

用docker安装好mysql后:

1.创建普通用户向数据库授予权限

[root@localhost ~]# docker exec -it mysql mysql -uroot -p
mysql> create user 'exporter'@'%' identified by 'redhat' with max_user_connections 3;
mysql> grant process,replication client,select on *.* to 'exporter'@'%';
mysql> flush privileges;

2.Docker部署部署MySQLD Exporter

#下载docker-compose文件
curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#给他一个执行权限
chmod +x /usr/local/bin/docker-compose
#查看是否安装成功
docker-compose -version

#创建docker-compose目录
mkdir ~/docker-compose
cd  ~/docker-compose
vim docker-compose.yml
version: '3'
services:
  mysqlexporter:
    image: prom/mysqld-exporter
    restart: always
    ports:
      - "9104:9104"
    environment:
      - DATA_SOURCE_NAME=exporter:redhat@(192.168.85.133:3306)/database


#执行安装命令
docker-compose up -d

二进制安装mysqld-exporter

下载地址:https://prometheus.io/download/#mysqld_exporter

可以看别人的这个博客:

测试成功:http://192.168.85.133:9104/metrics

然后再去普罗米修斯的配置文件增加一台监控数据集
使用dashboards:7362

四、进程监控

1、process exporter功能

如果想要对主机的进程进行监控,例如chronyd,sshd等服务进程以及自定义脚本程序运行状态监控。我们使用node exporter就不能实现需求了,此时就需要使用process exporter来做进程状态的监控。
项目地址: https://github.com/ncabatoff/process-exporter

五、黑盒监控

“白盒监控”-需要把对应的Exporter程序安装到被监控的目标主机上,从而实现对主机各种资源及其状态的数据采集工作。
但是由于某些情况下操作技术或其他原因,不是所有的Exporter都能部署到被监控的主机环境中,最典型的例子是监控全国网络质量的稳定性,通常的方法是使用ping操作,对选取的节点进行ICMP测试,此时不可能在他人应用环境中部署相关的Exporter程序。针对这样的应用的场景,Prometheus社区提供了黑盒解决方案,Blackbox Exporter无须安装在被监控的目标环境中,用户只需要将其安装在与Prometheus和被监控目标互通的环境中,通过HTTP、HTTPS、DNS、TCP、ICMP等方式对网络进行探测监控,还可以探测SSL证书过期时间。
Prometheus官方提供的exporter之一,可以提供 http、dns、tcp、icmp的监控数据采集

六、pushgateway

Pushgateway,是Prometheus生态中一个重要工具,使用它的原因主要是:
1、Prometheus 采用pull模式,可能由于不在一个子网或者防火墙原因,导致Prometheus 无法直接拉取各个target数据。
2、在监控业务数据的时候,需要将不同数据汇总,由Prometheus统一收集。
3、当exporter不能满足需要时,也可以通过自定义(python、shell、java)监控我们想要的数据。
由于以上原因,不得不使用pushgateway,但在使用之前,有必要了解一下它的一些弊端:
将多个节点数据汇总到pushgateway,如果pushgateway挂了,受影响比多个target大。.
Prometheus拉取状态up只针对pushgateway,无法做到对每个节点有效。
.Pushgateway可以持久化推送给它的所有监控数据。
因此,即使你的监控已经下线,prometheus还会拉取到旧的监控数据,需要手动清理pushgateway不要的数据。

为了方便,直接前台启动,当然也可以写systemed服务
./pushgateway 

测试:http://192.168.85.141:9091/

向Pushgateway推送监控数据

使用curl:
正常情况我们会使用Client SDK推送数据到pushgateway,但是我们还可以curl调用API来管理,例如:

#提交一条数据到 {job=‘some_job’}
echo "some_metric 3.14" | curl --data-binary @- http://192.168.85.141:9091/metrics/job/some_job
#下面我们加上instance的值
echo "some_metrics 3.14" | curl --data-binary @- http://192.168.85.141:9091/metrics/job/some_job/instance/some_instance
#下面加上标签测试
echo "some_metrics{tag=\"test\"} 3.14" | curl --data-binary @- http://192.168.85.141:9091/metrics/job/some_job/instance/some_instance
#删除某个组下某实例的所有数据
curl -XDELETE http://192.168.85.141:9091/metrics/job/some_job/instance/some_instance
#删除某个组下的所有数据
curl -X DELETE http://192.168.85.141:9091/metrics/job/some_job
#删除所有,需要开启 --web.enable-admin-api
curl -X PUT http://192.168.85.141:9091/api/v1/admin/wipe

prometheus 的配置

因为 Prometheus 配置 pushgateway 的时候,指定 job 和 instance, 但是它只表示 pushgateway 实例,不能真正表达收集数据的含义。所以在 prometheus 中配置 pushgateway 的时候,需要添加 honor_labels: true 参数, 从而避免收集数据被push本身的 job 和 instance 被覆盖。不加 honor_labels: true ,会取gateway的job和instance,设置了的话会取push过来的数据,job必填,instance没有就为 “” 空字符串.

- job_name: 'pushgateway'
    honor_labels: true
    static_configs:
    - targets: ['192.168.85.141:9091']
      labels:
        instance: pushgataway

使用python

安装prometheus_client模块

#安装pip
yum insta17 python3-pip
#通过pip安装prometheus_client
pip insta11 prometheus_client

使用shell脚本

例如:监控data数据目录下的文件数量(需求):

#!/bin/bash
file_num=$(ls /root/data/ | wc -l)
echo "data_file_num ${file_num}" | curl --data-binary @- http://192.168.85.141:9091/metrics/job/test_job/instance/test_instance

#在写一个定时任务指定时间采集数据
* * * * /bin/sh test.sh >/dev/nu11 2>&1

配置告警规则

例如:当data目录下的文件数量超过5,报警出来.

groups:
- name: /data数据目录超载告警
  rules:
  - alert: /data数据目录告警
    expr: data_file_num > 5
    for: 1m
    labels:
      severity: warning
    annotations:
      summary: "/data数据目录>5, 当前值:{{ $value }}"

重新加载并检查:

curl -X POST http://192.168.85.141:9090/-/reload
./promtool check config prometheus.yml

七、基于文件的服务发现

我们知道在Prometheus 配置文件中可以通过一个static_configs 来配置静态的抓取任务,但是在云环境下,特别是容器环境下,抓取目标地址是经常变动的,所以用静态的方式就不能满足这些场景了,还有特别在很多服务器需要监控时。所以我们需要监控系统能够动态感知这个变化,不可能每次变动都去手动重新配的,为了应对复杂的动态环境,Prometheus也提供了与基设施中的服务发现集成的功能。

一、创建文件

接下来我们来创建一个用于服务发现的目标文件,在与prometheus.yml文件相同目录下面创建一个名为targets.yml的文件,内容如下所示:

[root@localhost prometheus]# mkdir target
[root@localhost prometheus]# cd target/
[root@localhost target]# vim targets.yml

 - targets: ['localhost:9090']
  2   labels:
  3     job: prometheus
  4 - targets: ['192.168.85.133:8080']
  5   labels:
  6     instance: promethues服务器
  7     job: cadvisor
  8 - targets: ['192.168.85.133:9100']
  9   labels:
 10     instance: 被监控的服务器
 11     job: node-exporter
 12 - targets: ['192.168.85.141:9091']
 13   labels:
 14     instance: test服务器
 15     job: pushgateway
                        

二、配置文件自动发现

用于发现的目标文件创建完成后,要让Prometheus能够从上面的 targets.yml文件中自动读取抓取目标,需要在prometheus.yml配置文件中的scrape_configs部分添加如下所示的抓取配置:

scrape_configs:
  - job_name: "file-sd-test"
    file_sd_configs:
    - refresh_interval: 10s
      files:
      - "target/targets.yml"

重新加载并检查:

curl -X POST http://192.168.85.141:9090/-/reload
./promtool check config prometheus.yml

基于文件的自动发现,可以让我们动态地改变Prometheus的监控目标,而不需要重新启动或重新加载prometheus服务器。

八、基于Consul的自动发现

Consul是由HashiCorp开发的一个支持多数据中心的分布式服务发现和键值对存储服务的开源软件,是一个通用的服务发现和注册中心工具,被大量应用于基于微服务的软件架构当中。
我们通过api将exporter服务注册到Consul,然后配置Prometheus 从 Consul中发现实例。关于Consul本身的使用可以查看官方文档https://learn.hashicorp.com/consul了解更多。

1、二进制安装配置Consul

在页面https://www.consul.io/downloads 下载符合自己系统的安装文件,比如我们这里是Linux系统,使用下面命令下载安装即可.

[root@localhost ~]# unzip consul_1.18.0_linux_amd64.zip 
mv consul /usr/local/bin/

2.启动consul

为了查看更多的日志信息,我们可以在dev模式下运行Consul,如下所示:

consul agent -dev -client 0.0.0.0

启动命令后面使用-client参数指定了客户端绑定的地址,默认为127.0.0.1

测试启动成功没:http://192.168.85.141:8500/

3.通过api注册到consul

接下来,我们要注册服务到 Consul 中,可以通过其提供的 API 标准接口来添加。那么先注册一个测试服务,该测试数据为本机 pushgateway 服务信息,服务地址及端口为 pushgatewayr 默认提供指标数据的地址,执行如下命令:

curl -X PUT -d '{"id": "pushgateway","name": "pushgateway","address": "192.168.85.141","port": 9191,"tags": ["test"],"checks": [{"http": "http://192.168.85.141:9091/metrics", "interval": "5s"}]}'  http://192.168.85.141:8500/v1/agent/service/register
 
#或者使用配置文件的形式将文件内容注册到consul
[root@node1-prome /zpf/k8s/prometheus/docker-prometheus/prometheus/test]$cat consul1.json
{
  "ID": "node-exporter-75.41",
  "Name": "node-exporter",
  "Tags": [
    "node-exporter"
  ],
  "Address": "192.168.75.41",
  "Port": 9100,
  "Meta": {
    "app": "spring-boot",
    "team": "appgroup",
    "project": "bigdata"
  },
  "EnableTagOverride": false,
  "Check": {
    "HTTP": "http://192.168.75.41:9100/metrics",
    "Interval": "10s"
  },
  "Weights": {
    "Passing": 10,
    "Warning": 1
  }
}
$curl --request PUT --data @consul1.json http://192.168.75.41:8500/v1/agent/service/register?replace-existing-checks=1
如果要注销掉某个服务,可以通过如下 API 命令操作,例如注销上边添加的 node-exporter 服务.这里的node-exporter事这个注册服务的ID

curl -X PUT http://192.168.85.141:8500/v1/agent/service/deregister/node-exporter
 
#说明一下这里最后的node-exporter是consul中exporter的id.

配置promethues

上面我们通过Consul注册了2个node_exporter服务,接下来我们将配置Prometheus通过Consul来自动发现 node_porter服务。在Prometheus的配置文件 prometheus.yml文件中的scrape_configs 部分添加如下所示的抓取配置:
先备份原来的配置

三、ConsulManger

1、ConsulManager需要依赖consul,请先完成Consul的部署。(暂时最高支持Consul v1.14.5)
地址:https://gitee.com/starsl/ConsulManager

九、PromQL基础使用

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

1、PromQL数据类型

在Prometheus的表达式语言中,PromQL数据类型归类为以下四种:

  • 瞬时向量((instant vector),是指同一时刻的一组时间序列,每个时间序列包含一个样本,所有样本共享相同的时间戳,即每个时序只有一个点。
  • 区间向量(range vector),是指在任何一个时间范围内的一组时间序列,包含每个时间序列随时间变化的一系列数据点,这时每个时序有多个点。
  • 标量(scalar),即纯量数据,一个简单的数字浮点值,只有一个数字,没有时序。
  • 字符串(string),一个目前未被使用的简单字符串值。