Prometheus监控系统(二)Prometheus部署与使用

Prometheus监控系统(⼆)Prometheus部署与使⽤
1. Prometheus安装
Prometheus基于Golang编写,编译后的软件包不依赖于任何第三⽅依赖。只需要下载对应平台的⼆进制包,解压并添加基本的配置即可正常启动Prometheus Server。
1.1. 安装Prometheus Server
下载安装包:
解压:
tar -zxvf prometheus-2.30.0.
$ ll
total 185544
drwxr-xr-x 2 hadoop hadoop        38 Sep 14 10:24 console_libraries
drwxr-xr-x 2 hadoop hadoop      173 Sep 14 10:24 consoles
-rw-r--r-- 1 hadoop hadoop    11357 Sep 14 10:24 LICENSE
-rw-r--r-- 1 hadoop hadoop      3646 Sep 14 10:24 NOTICE
-rwxr-xr-x 1 hadoop hadoop 100343878 Sep 14 09:52 prometheus
-rw-r--r-- 1 hadoop hadoop      934 Sep 14 10:l
-rwxr-xr-x 1 hadoop hadoop  89626644 Sep 14 09:55 promtool
修改配置⽂件l,添加PushGateway与Node Exporter的监控配置:
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["10.0.3.197:9090"]
# add PushGateway monitor config
- job_name: 'pushgateway'
static_configs:
- targets: ["10.0.3.197:9091"]    #此节点为EMR主节点
labels:
instance: pushgateway
# add Node Exporter monitor config
-
job_name: 'node exporter'
static_configs:
- targets: ['10.0.3.197:9100','10.0.3.34:9100']  #分别为EMR主节点与从节点
1. global配置块:控制Prometheus服务器的全局配置
a)      scrape_interval:配置拉取数据时间间隔,默认为1分钟
b)      evaluation_interval:规则验证(Evaluate rules,例如⽣成alert)的时间间隔,默认为1分钟
2. rule_files配置块:规则配置⽂件
3. scrape_config 配置块:配置采集⽬标相关,prometheus监视的⽬标。Prometheus⾃⾝的运⾏信息可以通过HTTP访问,所以Prometheus 可以监控⾃⼰的运⾏数据。
a)      job_name:监控作业的名称
聚光体
b)      static_configs:表⽰静态⽬标配置,就是固定从某个target拉取数据
Prometheus可以在运⾏中⾃动加载配置。启动时需添加:--able-lifecycle
1.2. 安装Pushgateway
Prometheus在正常情况下是采⽤Pull的模式,从产⽣metric的作业或者exporter(例如专门监控主机的NodeExporter)拉取监控数据。但是我们要监控的是Flink on YARN作业,
想要让Prometheus⾃动发现作业的提交、结束以及⾃动拉取数据显然是⽐较困难的。
PushGateway就是⼀个中转组件,通过配置Flink on YARN作业将metric推到PushGateway,Prometheus再从PushGateway拉取即可。
下载 pushgateway:
tar -zxvf pushgateway-1.4.1.
内容很简单,只有⼀个⼆进制⽂件pushgateway:
$ ll
total 16844
-rw-r--r-- 1 hadoop hadoop    11357 May 28 14:37 LICENSE
-rw-r--r-- 1 hadoop hadoop      487 May 28 14:37 NOTICE
-rwxr-xr-x 1 hadoop hadoop 17231089 May 28 14:30 pushgateway
1.3. 安装Alertmanager
tar -zxvf alertmanager-0.23.0.
1.4. 安装Node Exporter
tar -zxvf node_exporter-1.2.2.
在Prometheus的架构设计中,Prometheus Server主要负责数据的收集、存储、并且对外提供数据查询⽀持。⽽实际的监控样本数据的收集则由Exporter完成。
因此,为了能够监控到某些东西,例如主机的CPU使⽤率,我们需要使⽤到Exporter。Prometheus周期性的从Exporter暴露的HTTP地址(通常是/metrics)拉取监控样本数据。
Exporter是⼀个相对开放的概念,其可以是⼀个独⽴运⾏的程序,独⽴于监控⽬标。也可以是直接内
置在监控⽬标中。只要能够向Prometheus提供标准格式的监控样本数据即可。
为了能够采集到主机的指标如CPU、内存、磁盘等信息。我们可以使⽤Node Exporter。Node Exporter 同样采⽤Golang编写,并且不存在任何的第三⽅依赖。只需要下载,解压即可运⾏。
启动:./node_exporter
然后即可在实例9100端⼝访问到node export 获取的当前主机的所有监控数据。例如:
# HELP node_disk_written_bytes_total The total number of bytes written successfully.
# TYPE node_disk_written_bytes_total counter
node_disk_written_bytes_total{device="xvda"} 1.163564544e+09
摩托车化油器结构图
node_disk_written_bytes_total{device="xvdb"} 5.83238272e+10
1.4.1. 将Node Export 安装到所有节点
此步骤可以通过 bootstrap来完成。
aws s3 cp s3://tang-prometheus/packages/node_exporter-1.2.2. .
tar -zxvf node_exporter-1.2.2.
node_exporter-1.2.2.linux-amd64/node_exporter &
修改 Prometheus配置⽂件l,并添加以下配置(前⾯已添加):
# add Node Exporter monitor config
- job_name: 'node exporter'
static_configs:
- targets: ['10.0.3.197','10.0.3.34']  #分别为EMR主节点与从节点
1.4.
2. 设置为开启⾃动启动
$ sudo vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_export
Documentation=github/prometheus/node_exporter
After=network.target
[Service]
Type=simple
User=hadoop
ExecStart=/home/hadoop/prometheus/node_exporter-1.2.2.linux-amd64/node_exporter
现浇梁Restart=on-failure
[Install]
WantedBy=multi-user.target
在所有机器上设置为开机启动:
sudo systemctl enable node_exporter.service
启动服务(所有节点):
sudo systemctl start node_exporter.service
2. 启动Prometheus Server、Pushgateway和Altertmanager 启动Prometheus Server:
nohup ./prometheus --config.l > ./prometheus.log 2>&1 &
启动Pushgateway:
nohup ./pushgateway --web.listen-address :9091 > ./pushgateway.log 2>&1 &
启动Alertmanager:
nohup ./alertmanager --config.l > ./alertmanager.log 2>&1 &
可以看到各个服务都是UP状态。
3. PromQL介绍手机盒
Prometheus通过指标名称(metrics name)以及对应的⼀组标签(labelset)唯⼀定义⼀条时间序列。指标名称反映了监控样本的基本标识,⽽label则在这个基本特征上为采集到的数据提供了多种特征维度。⽤户可以基于这些特征维度过滤、聚合、统计从⽽产⽣新的计算后的⼀条时间序列。
PromQL是Prometheus内置的数据查询语⾔,其提供对时间序列数据丰富的查询、聚合以及逻辑运算能⼒的⽀持。并且被⼴泛应⽤在Prometheus的⽇常应⽤中,包括对数据查询、可视化、告警处理中。
3.1. 基本⽤法
3.1.1. 查询时间序列
当Prometheus通过Exporter采集到相应的监控指标样本数据后,我们就可以通过PromQL对监控样本数据进⾏查询。
当我们直接使⽤监控指标名称查询时,可以查询该指标下的所有时间序列,例如:
prometheus_http_requests_total
其等同于:prometheus_http_requests_total{}
在花括号⾥可以加过滤条件,例如:prometheus_http_requests_total{code="302"}
PromQL还⽀持⽤户根据时间序列的标签匹配模式来对时间序列进⾏过滤,⽬前主要⽀持2种匹配模式:完全匹配和正则匹配。
PromQL⽀持使⽤ = 和 != 两种完全匹配模式:
1. 通过使⽤label=value可以选择那些标签满⾜表达式定义的时间序列
2. 反之使⽤label != value则可以根据标签匹配排除时间序列
例如,我们只需要查询所有prometheus_http_requests_total时间序列种满⾜标签instance为10.0.3.197:9090的时间序列:
prometheus_http_requests_total{instance="10.0.3.197:9090"}
反之使⽤:prometheus_http_requests_total{instance!="10.0.3.197:9090"}
PromQL还⽀持正则表达式作为匹配条件,多个表达式之间使⽤ | 进⾏分离:
1. 使⽤label=~regx 表⽰选择那些标签符合正则表达式定义的时间序列
2. 反之使⽤label!~regx进⾏排除
例如,如果想查询多个环节下的时间序列,可以使⽤如下表达式:
prometheus_http_requests_total{environment =~=~"staging|testing|development",method!="GET"}
排除⽤法:
prometheus_http_requests_total{environment !!~~"staging|testing|develop ment",method!="GET"}
3.1.2. 范围查询
直接通过类似于PromQL表达式httprequesttotal查询时间序列返回时,返回值中只会包含该时间序列中的最新的⼀个样本值,这样的返回结果我们称之为瞬时向量。⽽相应的这样的表达式称之为瞬时向量表达式。
⽽如果我们想查询过去⼀段时间范围内的样本数据时,需要使⽤区间向量表达式。区间向量表达式和瞬时向量表达式之间的差异在于:区间向量表达式中我们需要定义时间选择的范围,时间范围通过时间范围选择器[]进⾏定义。例如,通过以下表达式可以选择最近1分钟内的所有样本数据:
prometheus_http_requests_total{}[1m]
该表达式返回查询到的时间序列中最近1分钟的所有样本数据:
prometheus_http_requests_total{code="200", handler="/-/ready", instance="10.0.3.197:9090", job="prometheus"}
17 @1632465479.097
17 @1632465494.098
17 @1632465509.098
17 @1632465524.098
prometheus_http_requests_total{code="200", handler="/api/v1/alertmanagers", instance="10.0.3.197:9090", job="prometheus"}
1 @1632465479.097
1 @1632465494.098
1 @1632465509.098
1 @1632465524.098
通过区间向量表达式查询得到的结果称为区间向量。除了使⽤m表⽰分钟外,PromQL的时间范围选择器⽀持其他时间单位:无心磨床自动上料机
s – 秒
m – 分钟
h – ⼩时
d – 天
w – 周
ext前端框架y – 年
3.1.3. 时间位移操作
在瞬时向量表达式或者区间向量表达式中,都是以当前的时间为基准:
prometheus_http_requests_total{} # 瞬时向量表达式,选择当前最新的数据
prometheus_http_requests_total{}[1m] # 区间向量表达式,选择以当前时间为基准,1分钟内的数据
若是想查询1分钟前的瞬时样本数据,或者昨天⼀天的区间内的样本数据。此时可以使⽤位移操作,关键字为offset:
prometheus_http_requests_total{} offset 1m
prometheus_http_requests_total{}[1d] offset 1d
3.1.
4. 使⽤聚合操作

本文发布于:2024-09-23 03:25:09,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/1/161426.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:时间   配置   数据   监控   表达式
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议