【Prometheus】Prometheus安装部署流程详解,配置参数webUI使用方法解析说明

前端 0

在这里插入图片描述

✨✨ 欢迎大家来到景天科技苑✨✨

🎈🎈 养成好习惯,先赞后看哦~🎈🎈

🏆 作者简介:景天科技苑
🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。
🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi,flask等框架,云原生k8s,Prometheus监控,linux,shell脚本等实操经验,网站搭建,数据库等分享。

所属的专栏:Prometheus监控系统零基础到进阶
景天的主页:景天科技苑

在这里插入图片描述

文章目录

  • Prometheus安装配置
    • 1、Prometheus安装前准备
      • 1.1 地址规划
      • 1.2 配置Host解析
      • 1.3 防火墙放行9090端口(如果防火墙开启)
    • 2、Prometheus安装配置
      • 2.1 下载Prometheus
      • 2.2 配置Prometheus
      • 2.3 system启动Prometheus
        • 1、配置system管理Prometheus启动和停⽌
        • 2、在启动 Prometheus 服务时参数详解
        • 3、启动Prometheus服务
      • 2.4 访问Prometheus
      • 2.5 Prometheus的WebUI界面详解

Prometheus安装配置

1、Prometheus安装前准备

Prometheus⽀持多种安装⽅式,为了便于理解,我们先使⽤⼆进制⽅式进⾏部署,后期我们再使⽤Kubernetes⽅式进⾏部署。

1.1 地址规划

在这里插入图片描述

1.2 配置Host解析

1、配置hosts解析

[root@jingtian01 ~ ]#vim /etc/hosts127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4::1         localhost localhost.localdomain localhost6 localhost6.localdomain610.10.0.20 jingtian0110.10.0.21 jingtian0210.10.0.22 jingtian0310.10.0.23 jingtian04

2、推送hosts解析⽂件,确保每个节点都能正常通过主机名进⾏访问

for i in {21..23};do scp /etc/hosts 10.10.0.$i:/etc/; done

在这里插入图片描述

测试下是否解析成功
在这里插入图片描述

1.3 防火墙放行9090端口(如果防火墙开启)

为访问 Prometheus 的默认端口 9090,你需要在防火墙中开放该端口。在 CentOS 7 上,你可以使用以下命令来开放端口:

sudo firewall-cmd --permanent --add-port=9090/tcp  sudo firewall-cmd --reload

2、Prometheus安装配置

2.1 下载Prometheus

1、访问Prometheus官⽹ https://prometheus.io/download/ 下载
Prometheus
LTS版本是比较稳定的版本
在这里插入图片描述

我们安装的是Linux版本,右键复制链接地址
在这里插入图片描述

然后在jingtian01上面去下载安装

wget https://github.com/prometheus/prometheus/releases/download/v2.53.2/prometheus-2.53.2.linux-amd64.tar.gz

在这里插入图片描述

查看下载的文件
在这里插入图片描述

如果直接下载不下来,也可以添加个加速地址:

 wget https://mirror.ghproxy.com/https://github.com/prometheus/prometheus/releases/download/v2.53.2/prometheus-2.53.2.linux-amd64.tar.gz

2、解压Prometheus⾄指定⽬录

tar xf prometheus-2.53.2.linux-amd64.tar.gz -C /etc/

在这里插入图片描述

做个软连接,方便访问

ln -s /etc/prometheus-2.53.2.linux-amd64/ /etc/prometheus

查看解压后的文件
其实我们什么都不配置,就可以启动,默认加载prometheus.yml这个文件。
在这里插入图片描述

3、执⾏ prometheus --version 查看命令是否正常

/etc/prometheus/prometheus --version

在这里插入图片描述

2.2 配置Prometheus

1、在启动 Prometheus 之前,我们需要准备⼀个Prometheus的配置⽂件,监控⽬标服务。

由于 Prometheus 服务本身对外暴露了Metrics指标接⼝,所以我们可以配置Prometheus监控⾃身,保存⼀个名为 prometheus.yml 的⽂件,替换掉默认的配置⽂件

先将默认的prometheus.yml备份

cp /etc/prometheus/prometheus.yml /etc/prometheus/prometheus.yml_bak

Prometheus 启动的时候,可以加载运行参数 -config.file 指定配置文件,默认为prometheus.yml。

Prometheus的配置文件是YAML格式。Prometheus的解压包里自带了一个默认的配置文件prometheus.yml。让我们来看一下:

[root@jingtian01 prometheus ]#cat prometheus.yml_bak # my global configglobal:  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.  # scrape_timeout is set to the global default (10s).# Alertmanager configurationalerting:  alertmanagers:    - static_configs:        - targets:          # - alertmanager:9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files:  # - "first_rules.yml"  # - "second_rules.yml"# 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: ["localhost:9090"]

2、配置文件参数详解:
在这个缺省的配置文件里定义了4个单元:global、alerting、rule_files和scrape_configs。
在配置文件中我们可以指定 global, alerting, rule_files, scrape_configs, remote_write, remote_read 等属性。

global:配置全局的信息,如抓取监控数据的间隔,抓取业务数据接口的超时时间,告警规则执行周期等alerting:配置告警发送到的alermanager的地址rule_files:告警规则文件,数据聚合配置scrape_configs:配置抓取业务监控数据的相关信息,如url,拉取时间间隔,拉取的超时时间等。定义了 Prometheus 如何抓取目标应用或设备的指标数据。每个抓取配置可以有不同的 job_name,这是为了区分不同的数据源。在 static_configs 中可以指定具体的目标地址和端口。	metrics_path 和 scheme 分别定义了从目标抓取数据时使用的路径和协议。默认情况下,路径为 /metrics,协议为 http。remote_write:将数据投递到远程地址,如聚合数据投递到hubble-adapterremote_read:主要用于可读远程存储配置,主要包含以下参数:	url: 访问地址	remote_timeout: 请求超时时间

global

global 属于全局的默认配置,它主要包含 4 个属性,scrape_interval: 拉取 targets 的默认时间间隔,即拉取业务监控数据的间隔时间。scrape_timeout: 拉取一个 target 的超时时间,即拉取业务监控数据接口的超时时间。evaluation_interval: 执行 rules 的时间间隔。即多久遍历一次告警规则列表,判断每个规则是否触发告警。和rule_files的加载没关系external_labels: 额外的属性,会添加到拉取的数据并存到数据库中。

alerting
通常我们可以使用运行参数 -alertmanager.xxx 来配置 Alertmanager, 但是这样不够灵活,没有办法做到动态更新加载,以及动态定义告警属性。
所以 alerting 配置主要用来解决这个问题,它能够更好的管理 Alertmanager, 主要包含 2 个参数:alert_relabel_configs 和 alertmanagers

(1)alertmanagers
用于动态发现 Alertmanager 的地址。
如下配置:alertmanager.prom-alert.svc:9093,通过K8S自动发现机制找到本集群内的alertmanager的地址并将告警发送过去。
目前,我们所有prometheus集群都是部署在徐水(另外有个武汉不用管)的K8S集群,Alertmanager也部署在这个集群,所以用自动发现的方式配置alertmanager地址即可。
如下,系统中指定了Alertmanager路径,因为最终需要投递告警到这个服务,如下图:是通过静态配置
在这里插入图片描述

(2)alert_relabel_configs
作用:在告警发生时,动态修改标签内容,一般作用是在告警产生时修改标签,如保留哪些标签(labelkeep),删除哪些标签(labeldrop)。具体的有哪些属性,请参考:Configuration | Prometheus
下面着重说明两个属性:action和regex

action基于正则表达式匹配执行的操作。包括移除标签,保留标签等,具体可参考:Configuration | Prometheusaction枚举:replace: 将正则表达式与串联的source_labels匹配。然后,将target_label设置为replace,用替换中的匹配组引用(${1}, ${2}, ...)替换为其值。 如果正则表达式不匹配,则不会进行替换。keep: 删除其正则表达式与串联的source_labels不匹配的目标。drop: 删除其正则表达式与串联的source_labels匹配的目标。hashmod: 将target_label设置为串联的source_labels的哈希的模数。labelmap: 将正则表达式与所有标签名称匹配。 然后,将匹配标签的值复制到通过替换为它们的值替换的匹配组引用(${1}, ${2}, ...)给出的标签名称。labeldrop: 将正则表达式与所有标签名称匹配。 任何匹配的标签将从标签集中删除。labelkeep: 将正则表达式与所有标签名称匹配。 任何不匹配的标签将从标签集中删除。regex作用是匹配标签的正则表达式。

rule_files
1、介绍
作用:获取所有规则文件中的规则,包括记录规则(recording rules)与告警规则(alerting rule)。

2、记录规则(recording rules)
record规则其实就是数据处理的规则,如数据聚合,如下:
我们可以单独定义数据聚合规则文件,也可以和告警规则文件放一起,但是一般分开放好理解
在这里插入图片描述

注意:一定是规则文件,不包含配置文件,如果指定的文件中包含配置文件内容,则会报错。
按照配置的目录,找了下rancher上对应武汉集群的prometheus项目下的prometheus服务,进入控制台:
我们的配置文件是这样的:

rule_files:- /etc/prometheus/rules/*rules.yaml

所以进入此目录下发现只有一个alert-rules.yaml,恰好就是我们的告警的配置。

scrape_configs

scrape_configs 主要用于配置拉取数据节点,每一个拉取配置主要包含以下参数:job_name:任务名称honor_labels: 用于解决拉取数据标签有冲突,当设置为 true, 以拉取数据为准,否则以服务配置为准params:数据拉取访问时带的请求参数scrape_interval: 拉取时间间隔scrape_timeout: 拉取超时时间metrics_path: 拉取节点的 metric 路径static_configs:配置访问路径前缀,如ip+port,或者域名地址,或者通过服务发现,类似alertmanager.prom-alert.svc:9093scheme: 拉取数据访问协议,如httpsample_limit: 存储的数据标签个数限制,如果超过限制,该数据将被忽略,不入存储;默认值为0,表示没有限制relabel_configs: 拉取数据重置标签配置metric_relabel_configs:metric 重置标签配置

我们先写个简易的,监控prometheus本机

[root@jingtian01 prometheus ]#cat prometheus.yml# my global configglobal:  scrape_interval: 15s # 抓取指标数据的间隔 Set the scrape interval to every 15 seconds. Default is every 1 minute.  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.  # scrape_timeout is set to the global default (10s).# Alertmanager configurationalerting:  alertmanagers:    - static_configs:        - targets:          # - alertmanager:9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files:  # - "first_rules.yml"  # - "second_rules.yml"# 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: "/metrics"    # metrics_path defaults to '/metrics'    # scheme defaults to 'http'.    static_configs:      # prometheus默认暴露9090端口的/metrics      # targets里面只能写ip:端口      # 如果默认的路径不是/metrics,可以在上面写metrics_path      - targets: ["localhost:9090"]

3、启动可以直接通过命令启动
我们先看下prometheus启动参数:
./prometheus --help
–config.file= 指定配置文件
–web.listen-address=“0.0.0.0:9090” 默认监听的是9090端口
在这里插入图片描述

–web.enable-lifecycle #开启热加载,不用重新启动prometheus,通过http请求的方式,即可加载最新的配置文件
http请求为:

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

在这里插入图片描述

执行该命令后,prometheus重新加载配置文件
在这里插入图片描述

prometheus默认保存数据到tsdb数据库存储15天
存储路径是当前目录下的data目录
在这里插入图片描述

本地运行:

./prometheus

默认找的就是当前目录下的prometheus.yml
在这里插入图片描述

通过ip和端口访问
10.10.0.20:9090
在这里插入图片描述

status–Runtime&& Build Infomation可以查看prometheus保存数据时长
在这里插入图片描述

默认是15天
在这里插入图片描述

查看下/metrics
10.10.0.20:9090/metrics
在这里插入图片描述

2.3 system启动Prometheus

但直接这么启动不是很⽅便,因此我们可以准备⼀个专属的启动⽂件,我们通过system来启动

1、配置system管理Prometheus启动和停⽌
[root@jingtian01 ~ ]#cat /usr/lib/systemd/system/prometheus.service [Unit]Description=Prometheus serverDocumentation=https://prometheus.io/ [Service]User=rootExecStart=/etc/prometheus/prometheus / --config.file=/etc/prometheus/prometheus.yml / --storage.tsdb.path=/etc/prometheus/data / --storage.tsdb.retention.time=60d / --web.enable-lifecycleExecReload=/bin/kill -HUP $MAINPIDRestart=on-failureRestartSec=20TimeoutStopSec=20SendSIGKILL=noLimitNOFILE=8192[Install]WantedBy=multi-user.target
2、在启动 Prometheus 服务时参数详解

它们定义了Prometheus 服务的⼀些关键⾏为。

--config.file=/etc/prometheus/prometheus.yml # 指定 Prometheus 的配置⽂件路径。--storage.tsdb.path=/etc/prometheus/data # 设置 Prometheus 存储时间序列数据库(TSDB)的路径。 ⽤于存放Prometheus抓取到的指标数据。--storage.tsdb.retention.time=60d # 配置 Prometheus 的数据保留期限。超过这个时间范围的旧数据将被删除。这个可以根据存储资源和监控需求进⾏调整。--web.enable-lifecycle # 允许通过 HTTP 请求的⽅式来更新Prometheus的配置。
3、启动Prometheus服务

启动prometheus

[root@jingtian01 ~ ]#systemctl daemon-reload [root@jingtian01 ~ ]#systemctl start prometheus.service 

查看状态

systemctl status prometheus.service 

在这里插入图片描述
查看端口监听
在这里插入图片描述

2.4 访问Prometheus

1、通过访问对应服务的IP:9090端⼝,访问Prometheus的UI界⾯
在这里插入图片描述

2、看下Status Targets
在这里插入图片描述

可以看到job名称,state,labels
在这里插入图片描述

目前我们检测的该节点是正常的,我们再添加节点监控

[root@jingtian01 prometheus ]#vim prometheus.yml      - targets: ["localhost:9090","jingtian02:9090"]

在这里插入图片描述

热加载下配置文件

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

可以看到该节点不正常
在这里插入图片描述

在这里插入图片描述

2.5 Prometheus的WebUI界面详解

Prometheus 的 Web UI提供了多个⻚⾯,以便⽤户可以查询指标、查看配置和状态,以及管理告警等。以下是 Prometheus Web UI 中常⻅的⼏个⻚⾯及其描述:
Graph: 允许⽤户通过 Prometheus 的查询语⾔ PromQL 来查询数据,并将
结果以图形的形式展示。
Alerts:允许⽤户查看当前配置的告警规则以及每个告警的状态。
Status:⻚⾯包含了⼏个⼦⻚⾯,它们提供了关于 Prometheus 服务器本身的各种信息:
Command-Line Flags: 显示了 Prometheus 启动时使⽤的命令⾏参数。这有助于了解 Prometheus 的配置和运⾏状态。

Configuration: 展示了 Prometheus 当前加载的配置⽂件内容,可以⽤来确认 Prometheus 正在使⽤的配置,有助于调试问题。
Targets: 显示了 Prometheus 监控的采集⽬标列表,包括每个⽬标的健康状态。如果某个⽬标⽆法抓取,这⾥也会显示相关的错误信息。
Rules: 显示所有配置的告警和记录规则。
Service Discovery: 展示了 Prometheus 的服务发现状态,这包括了Prometheus 如何发现抓取⽬标,以及它们的当前状态和元数据信息。

在主页面输入框,我们可以查询一些数据
在这里插入图片描述

根据我们输入的指标名称,也会有对应的提示,可以查询哪些内容
在这里插入图片描述

当我们查询某个指标,可以看到该指标对应的值
还能看到查询用了多少时间,返回多少条目
在这里插入图片描述

也可以看图形的展示页面
在这里插入图片描述

也许您对下面的内容还感兴趣: