56、K8S-监控机制-Prometheus-配置解析、标签管理

发布时间 2023-04-10 15:45:19作者: 小粉优化大师

Kubernetes学习目录

1、配置文件

1.1、配置简介

1.1.1、简介

Prometheus可以通过命令行或者配置文件的方式对服务进行配置。 一般情况下,命令行方式一般用于不可变的系统参数配置,例如存储位置、要保留在磁盘和内存中的数据量等;
配置文件用于定义与数据动态获取相关的配置选项和文件等内容。
1、命令行方式的配置属性可以通过 prometheus
-h 的方式来获取,这些配置属性主要在服务启动时候设置. 2、配置文件方式,我们需要在 /data/server/prometheus/cfg/prometheus.yml 文件中修改配置属性,该配置文件的内容是以YAML格式编写的。

1.2、prometheus配置文件组成

1.2.1、配置文件

prometheus-server ~]# egrep -v '^#|  #|^$' /data/server/prometheus/etc/prometheus.yml 
global:
  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.
alerting:
  alertmanagers:
    - static_configs:
        - targets:
rule_files:
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: "node_exporter"
    static_configs:
    - targets: ['192.168.10.29:9100','192.168.10.30:9100']

1.2.2、配置解析

核心配置:
global        全局配置内容
alerting       触发告警相关的配置,主要是与Alertmanager相关的设置。
rule_files     各种各样的外置规则文件配置,包含了各种数据查询表达式、告警表达式、等
scrape_configs 监控项的配置列表,这是最核心的配置
除了默认的四项配置之外,prometheus还有另外可选的其它配置,效果如下 扩展配置(8项) tls_config、static_config、relabel_config、metric_relabel_configs、 alert_relabel_configs、alertmanager_config、remote_write、remote_read
平台集成配置(12项) azure_sd_config、consul_sd_config、dns_sd_config、ec2_sd_config、 openstack_sd_config、file_sd_config、gce_sd_config、kubernetes_sd_config、 marathon_sd_config、nerve_sd_config、serverset_sd_config、triton_sd_config

1.2.3、scrape_configs解析

   对于scrape_configs来说,这是我们操作最多的一个配置段,它指定了一组监控目标及其细节配置参
数,这些目标和参数描述了如何获取指定主机上的时序数据。

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: "node_exporter"
    static_configs:
    - targets: ['192.168.10.29:9100','192.168.10.30:9100']

结构解析:
   在一般情况下,一个scrape_configs配置需要指定一个或者多个job,根据我们之前对基本概念的了解,每一个job都是
一系列的instance集合,借助job我们可以将目标主机进行分组管理。
对于job内部的每一个instance的配置,都需要借助于static_configs参数获取目标列表,只要在该列表位置的目标,
都可以被Prometheus动态服务自动发现。 static_configs的配置格式如下: scrape_configs:
- job_name: 'prometheus' static_configs: - targets: [ '<host_ip:host_port>', ... ] - labels: [ <labelname>: <labelvalue> ... ] 属性解析: 我们可以借助于 targets 以ip+port 方式发现目标,也可以使用labels以标签方式发现目标。

2、标签管理

2.1、标签简介

我们知道Prometheus对数据的处理流程是:先从各目标节点上获取数据,然后对数据进行本地化处理,然后进行各种数据分析。
这个流程中最核心的就是监控数据的本地化处理,在prometheus的配置选项中,有两个与监控指标密切相关的配置,relabel_config、metric_relabel_configs,他们的作用就是监控目标 上面的监控项进行标签管理和设置,便于在prometheus上设置更灵活的时序数据。

2.2、标签类型

2.2.1、私有标签

私有标签以"__*"样式存在,用于获取监控目标的默认元数据属性,比如__address__用于获取目标的地址,__scheme__用户获取目标的请求协议方法,__metrics_path__获取请求的url地址等

2.2.2、普通标签

对个监控主机节点上的监控指标进行各种灵活的管理操作,常见的操作有,删除不必要的敏感指标,添加、编辑或者修改指标的标签值或者标签格式。

 

2.3、全局性标签配置

global:
...
  external_labels:
    [ <labelname>: <labelvalue> ... ]
...

2.4、relabel_config、metric_relabel_configs配置 

2.4.1、配置语法

scrape_configs:
- job_name: 'prometheus'
  metric_relabel_configs:
  - source_labels: [<labelname> [, ...]]
    regex: '<regex> | default = (.*)'
    replacement: '<string> | default = $1'
    target_label: '<labelname>'
    action: '<relabel_action> | default = replace'

2.4.2、属性解析

属性解析:
source_labels 用于对正则匹配成功的监控项进行标签管理
target_label  在进行标签替换的时候,可以将原来的source_labels替换为我们定制的
label
replacement 替换标签的时候,设定label的值 
regex       通过正则表达式来匹配我们想要获取到的数据
action      对标签进行管理,常见的动作有replace|keep|drop|labelmap|labeldrop等

2.4.3、动作解析

动作解析:
replace:默认,通过regex匹配source_label的值,使用replacement来进行替换
keep:从获取的监控数据中删除regex与连接不匹配的目标 source_labels
drop:从获取的监控数据中删除regex与连接匹配的目标 source_labels
labeldrop:删除regex匹配的标签
labelkeep:删除regex不匹配的标签
labelmap:将regex对所有的标签名进行匹配判定,而后将匹配到的标签的值赋给replacement字段指定的标签名之上;通常用于取出匹配的标签名的一部分生成新标签;

2.4.4、删除示例

metric_relabel_configs:
- source_labels: [__name__]
  regex: 'node_network_receive.*'
  action: drop

2.4.5、替换示例

metric_relabel_configs:
- source_labels: [id]
  regex: '/.*'
  replacement: '4321'
  target_label: replace_id

2.5、relabel_config与metric_relabel_configs区别

2.5.1、执行顺序

relabel_configs用与发现目标前的标签设置,也就是说在scrape_configs前生效,针对的target对象
metric_relabel_configs 作用于scrape_configs生效后,即针对target对象上的监控数据

2.5.2、数据处理

metric_relabel_configs 是 prometheus 在保存数据前的最后一步标签重新编辑,默认情况下,它将监控不需要的数据,直接丢掉,不在prometheus 中保存

2.6、实践-根据默认属性定制新标签

2.6.1、配置prometheus.yml

prometheus-server ~]# egrep -v '^#|^$| #' /data/server/prometheus/etc/prometheus.yml 
global:
alerting:
  alertmanagers:
    - static_configs:
        - targets:
rule_files:
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: "node_exporter"
    static_configs:
    - targets: ['192.168.10.29:9100','192.168.10.30:9100']
# 增加如下配置 relabel_configs:
- source_labels: - __scheme__ - __address__ - __metrics_path__ regex: "(http|https)(.*)" separator: "" target_label: "endpoint" replacement: "${1}://${2}" action: replace

2.6.2、配置解析

    relabel_configs:
    - source_labels:
      - __scheme__
      - __address__
      - __metrics_path__
      regex: "(http|https)(.*)"
      separator: ""
      target_label: "endpoint"
      replacement: "${1}://${2}"
      action: replace

解析:
source_labels # 现有数据的标签,出现多个的时候,由separator指定分割符
regex         # __scheme__+__address__+__metrics_path__ 拼接成字符串,使用这个正式表达则分组匹配
separator     # 标签之间的分割符
target_label  # 产生新的标签名字
replacement   # 分成两组后,替换该表达式
action        # 动作

2.6.3、语法检查

prometheus-server ~]# promtool check config /data/server/prometheus/etc/prometheus.yml 
Checking /data/server/prometheus/etc/prometheus.yml
 SUCCESS: /data/server/prometheus/etc/prometheus.yml is valid prometheus config file syntax

2.6.4、重启prometheus服务

systemctl restart prometheus

2.6.5、web页面查询结果

2.7、实践-基于已存在的标签匹配,然后进行删除

2.7.1、配置prometheus.yml

prometheus-server ~]# egrep -v "^#|^$| #" /data/server/prometheus/etc/prometheus.yml 
global:
alerting:
  alertmanagers:
    - static_configs:
        - targets:
rule_files:
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: "node_exporter"
    static_configs:
    - targets: ['192.168.10.29:9100','192.168.10.30:9100']
    relabel_configs:
    - regex: "(job|app)"
      replacement: ${1}_name
      action: labelmap
    - regex: "(job|app)"
      action: labeldrop

2.7.2、配置解析

    relabel_configs:
    - regex: "(job|app)"
      replacement: ${1}_name
      action: labelmap
    - regex: "(job|app)"
      action: labeldrop

# 备份标签
labelmap

# 删除标签
labeldrop

2.7.3、语法检查

prometheus-server ~]# promtool check config /data/server/prometheus/etc/prometheus.yml 
Checking /data/server/prometheus/etc/prometheus.yml
 SUCCESS: /data/server/prometheus/etc/prometheus.yml is valid prometheus config file syntax

2.7.4、重启prometheus服务

systemctl restart prometheus

2.7.5、web页面查询结果