设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间07 现在时间07 缓存数据 给自己一个目标,给自己一个希望,给自己一份爱、一份温暖,只为今天快乐,不为昨天烦恼,自己照顾好自己,我的朋友。

给自己一个目标,给自己一个希望,给自己一份爱、一份温暖,只为今天快乐,不为昨天烦恼,自己照顾好自己,我的朋友。

查看: 1523|回复: 1

Prometheus配置解析小结

[复制链接]

  离线 

TA的专栏

  • 打卡等级:热心大叔
  • 打卡总天数:228
  • 打卡月天数:0
  • 打卡总奖励:3489
  • 最近打卡:2025-04-12 13:54:36
等级头衔

等級:晓枫资讯-上等兵

在线时间
0 小时

积分成就
威望
0
贡献
388
主题
357
精华
0
金钱
4671
积分
805
注册时间
2023-1-5
最后登录
2025-4-12

发表于 2024-6-2 23:05:37 | 显示全部楼层 |阅读模式
目录


  • 1.Prometheus整体架构图
  • 2.配置文件
  • 3.基于consul的服务发现
  • 4.prometheus配置详解

1.Prometheus整体架构图

1.png


2.配置文件
  1. # 全局配置
  2. global:
  3.   scrape_interval:     15s   # 多久 收集 一次数据
  4.   evaluation_interval: 30s   # 多久评估一次 规则
  5.   scrape_timeout:      10s   # 每次 收集数据的 超时时间
  6.   
  7.   # 当Prometheus和外部系统(联邦, 远程存储, Alertmanager)通信的时候,添加标签到任意的时间序列或者报警
  8.   external_labels:
  9.     monitor: codelab
  10.     foo:     bar
  11.   
  12. # 规则文件, 可以使用通配符
  13. rule_files:
  14. - "first.rules"
  15. - "my/*.rules"
  16.   
  17. # 远程写入功能相关的设置
  18. remote_write:
  19.   - url: http://remote1/push
  20.     write_relabel_configs:
  21.     - source_labels: [__name__]
  22.       regex:         expensive.*
  23.       action:        drop
  24.   - url: http://remote2/push
  25.   
  26. # 远程读取相关功能的设置
  27. remote_read:
  28.   - url: http://remote1/read
  29.     read_recent: true
  30.   - url: http://remote3/read
  31.     read_recent: false
  32.     required_matchers:
  33.       job: special
  34.   
  35. # 收集数据 配置 列表
  36. scrape_configs:
  37. - job_name: prometheus  # 必须配置, 自动附加的job labels, 必须唯一
  38.   
  39.   honor_labels: true   # 标签冲突, true 为以抓取的数据为准 并 忽略 服务器中的, false 为 通过重命名来解决冲突
  40.   # scrape_interval is defined by the configured global (15s).
  41.   # scrape_timeout is defined by the global default (10s).
  42.   
  43.   metrics_path:     '/metrics'
  44.   # scheme defaults to 'http'.
  45.   
  46.   
  47.   # 文件服务发现配置 列表
  48.   file_sd_configs:
  49.     - files:  # 从这些文件中提取目标
  50.       - foo/*.slow.json
  51.       - foo/*.slow.yml
  52.       - single/file.yml
  53.       refresh_interval: 10m  # 刷新文件的 时间间隔
  54.     - files:
  55.       - bar/*.yaml
  56.   
  57.   
  58.   # 使用job名作为label的 静态配置目录 的 列表
  59.   static_configs:
  60.   - targets: ['localhost:9090', 'localhost:9191']
  61.     labels:
  62.       my:   label
  63.       your: label
  64.   
  65.   
  66.   # 目标节点 重新打标签 的配置 列表.  重新标记是一个功能强大的工具,可以在抓取目标之前动态重写目标的标签集。 可以配置多个,按照先后顺序应用
  67.   relabel_configs:
  68.   - source_labels: [job, __meta_dns_name]   # 从现有的标签中选择源标签, 最后会被 替换, 保持, 丢弃
  69.     regex:         (.*)some-[regex]  # 正则表达式, 将会提取source_labels中匹配的值
  70.     target_label:  job   # 在替换动作中将结果值写入的标签.
  71.     replacement:   foo-${1}  # 如果正则表达匹配, 那么替换值. 可以使用正则表达中的 捕获组
  72.     # action defaults to 'replace'
  73.   - source_labels: [abc]  # 将abc标签的内容复制到cde标签中
  74.     target_label:  cde
  75.   - replacement:   static
  76.     target_label:  abc
  77.   - regex:
  78.     replacement:   static
  79.     target_label:  abc
  80.   
  81.   bearer_token_file: valid_token_file  # 可选的, bearer token 文件的信息
  82.   
  83.   
  84. - job_name: service-x
  85.   
  86.   # HTTP basic 认证信息
  87.   basic_auth:
  88.     username: admin_name
  89.     password: "multiline\nmysecret\ntest"
  90.   
  91.   scrape_interval: 50s  # 对于该job, 多久收集一次数据
  92.   scrape_timeout:  5s
  93.   
  94.   sample_limit: 1000  # 每次 收集 样本数据的限制. 0 为不限制
  95.   
  96.   metrics_path: /my_path  # 从目标 获取数据的 HTTP 路径
  97.   scheme: https  # 配置用于请求的协议方案
  98.   
  99.   
  100.   # DNS 服务发现 配置列表
  101.   dns_sd_configs:
  102.   - refresh_interval: 15s
  103.     names:  # 要查询的DNS域名列表
  104.     - first.dns.address.domain.com
  105.     - second.dns.address.domain.com
  106.   - names:
  107.     - first.dns.address.domain.com
  108.     # refresh_interval defaults to 30s.
  109.   
  110.   
  111.   # 目标节点 重新打标签 的配置 列表
  112.   relabel_configs:
  113.   - source_labels: [job]
  114.     regex:         (.*)some-[regex]
  115.     action:        drop
  116.   - source_labels: [__address__]
  117.     modulus:       8
  118.     target_label:  __tmp_hash
  119.     action:        hashmod
  120.   - source_labels: [__tmp_hash]
  121.     regex:         1
  122.     action:        keep
  123.   - action:        labelmap
  124.     regex:         1
  125.   - action:        labeldrop
  126.     regex:         d
  127.   - action:        labelkeep
  128.     regex:         k
  129.   
  130.   
  131.   # metric 重新打标签的 配置列表
  132.   metric_relabel_configs:
  133.   - source_labels: [__name__]
  134.     regex:         expensive_metric.*
  135.     action:        drop
  136.   
  137.   
  138. - job_name: service-y
  139.   
  140.   # consul 服务发现 配置列表
  141.   consul_sd_configs:
  142.   - server: 'localhost:1234'  # consul API 地址
  143.     token: mysecret
  144.     services: ['nginx', 'cache', 'mysql']  # 被检索目标的 服务 列表. 如果不定义那么 所有 服务 都会被 收集
  145.     scheme: https
  146.     tls_config:
  147.       ca_file: valid_ca_file
  148.       cert_file: valid_cert_file
  149.       key_file:  valid_key_file
  150.       insecure_skip_verify: false
  151.   
  152.   relabel_configs:
  153.   - source_labels: [__meta_sd_consul_tags]
  154.     separator:     ','
  155.     regex:         label:([^=]+)=([^,]+)
  156.     target_label:  ${1}
  157.     replacement:   ${2}
  158.   
  159. - job_name: service-z
  160.   
  161.   # 收集 数据的 TLS 设置
  162.   tls_config:
  163.     cert_file: valid_cert_file
  164.     key_file: valid_key_file
  165.   
  166.   bearer_token: mysecret
  167.   
  168. - job_name: service-kubernetes
  169.   
  170.   # kubernetes 服务 发现 列表
  171.   kubernetes_sd_configs:
  172.   - role: endpoints   # 必须写, 必须是endpoints, service, pod, node, 或者 ingress
  173.     api_server: 'https://localhost:1234'
  174.   
  175.     basic_auth:  # HTTP basic 认证信息
  176.       username: 'myusername'
  177.       password: 'mysecret'
  178.   
  179. - job_name: service-kubernetes-namespaces
  180.   
  181.   kubernetes_sd_configs:
  182.   - role: endpoints  # 应该被发现的 kubernetes 对象 实体
  183.     api_server: 'https://localhost:1234'  # API Server的地址
  184.     namespaces:  # 可选的命名空间发现, 如果省略 那么所有的命名空间都会被使用
  185.       names:
  186.         - default
  187.   
  188. - job_name: service-marathon
  189.   # Marathon 服务发现 列表
  190.   marathon_sd_configs:
  191.   - servers:
  192.     - 'https://marathon.example.com:443'
  193.   
  194.     tls_config:
  195.       cert_file: valid_cert_file
  196.       key_file: valid_key_file
  197.   
  198. - job_name: service-ec2
  199.   ec2_sd_configs:
  200.     - region: us-east-1
  201.       access_key: access
  202.       secret_key: mysecret
  203.       profile: profile
  204.   
  205. - job_name: service-azure
  206.   azure_sd_configs:
  207.     - subscription_id: 11AAAA11-A11A-111A-A111-1111A1111A11
  208.       tenant_id: BBBB222B-B2B2-2B22-B222-2BB2222BB2B2
  209.       client_id: 333333CC-3C33-3333-CCC3-33C3CCCCC33C
  210.       client_secret: mysecret
  211.       port: 9100
  212.   
  213. - job_name: service-nerve
  214.   nerve_sd_configs:
  215.     - servers:
  216.       - localhost
  217.       paths:
  218.       - /monitoring
  219.   
  220. - job_name: 0123service-xxx
  221.   metrics_path: /metrics
  222.   static_configs:
  223.     - targets:
  224.       - localhost:9090
  225.   
  226. - job_name: 測試
  227.   metrics_path: /metrics
  228.   static_configs:
  229.     - targets:
  230.       - localhost:9090
  231.   
  232. - job_name: service-triton
  233.   triton_sd_configs:
  234.   - account: 'testAccount'
  235.     dns_suffix: 'triton.example.com'
  236.     endpoint: 'triton.example.com'
  237.     port: 9163
  238.     refresh_interval: 1m
  239.     version: 1
  240.     tls_config:
  241.       cert_file: testdata/valid_cert_file
  242.       key_file: testdata/valid_key_file
  243.   
  244. # Alertmanager相关的配置
  245. alerting:
  246.   alertmanagers:
  247.   - scheme: https
  248.     static_configs:
  249.     - targets:
  250.       - "1.2.3.4:9093"
  251.       - "1.2.3.5:9093"
  252.       - "1.2.3.6:9093"
复制代码
3.基于consul的服务发现

注意: Prometheus的
  1. consul_sd_config
复制代码
使用的是catalog的API。
使用http接口注册consul
  1. # curl -X PUT -d '{"ID": "node_exporter", "Name": "node_exporter", "Address": "10.6.28.37", "Port": 9100, "Tags": ["lock"], "EnableTagOverride": false}' http://10.6.28.37:8500/v1/agent/service/register
复制代码
  1. # curl -s http://10.6.28.37:8500/v1/agent/services|jq
  2. {
  3.   "node_exporter": {
  4.     "ID": "node_exporter",
  5.     "Service": "node_exporter",
  6.     "Tags": [
  7.       "lock"
  8.     ],
  9.     "Address": "10.6.28.37",
  10.     "Port": 9100,
  11.     "EnableTagOverride": false,
  12.     "CreateIndex": 0,
  13.     "ModifyIndex": 0
  14.   }
  15. }
复制代码
4.prometheus配置详解

当查询的时候存在十分复杂的表达式,这样会降低prometheus的性能可以使用Recording rules
允许您预先计算经常需要的或计算上昂贵的表达式,并将其结果保存为新的 time series。这对于仪表板尤其有用,每次刷新时它都需要重复查询相同的表达式。 将复杂的计算后台计算 放到一个新的时序里
二元算术运算符 应用于 应用于 即时向量/即时向量 时,运算符将应用于左侧向量中的元素及其在右侧向量中的匹配到的元素.
运算结果被传播到结果向量中,并且度量名称被丢弃.那些在右侧向量中没有匹配条目的条目 不是结果的一部分。
例如:
  1. employee_age_bucket_bucket{le=~"20|30|40"} + employee_age_bucket_bucket{le=~"30|40|50"}
复制代码
返回的结果是:
  1. {instance="10.0.86.71:8080",job="prometheus",le="30"} 6000{instance="10.0.86.71:8080",job="prometheus",le="40"} 8000
复制代码
到此这篇关于Prometheus配置解析小姐的文章就介绍到这了,更多相关Prometheus配置内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
12
积分
4
注册时间
2023-9-17
最后登录
2023-9-17

发表于 2024-9-20 02:35:11 | 显示全部楼层
感谢楼主分享。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1楼
2楼

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表