Prometheus操作文档
<p>[TOC]</p>
<h1>一、Prometheus架构图</h1>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=e5d841a4833f88e8c09ea46da4979671&amp;file=file.png" alt="" /></p>
<p>普罗米修斯是基于pull-push即拉取和推送模型,简而言之就是获取监控指标metrics和发出告警信息alert以及进行可视化。</p>
<p><strong>例如:</strong></p>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=78e299d6dcea6669f9d30da1f4cdde38&amp;file=file.png" alt="" />
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=856e8c7f74cac39b2bb5add7ebab4bd4&amp;file=file.png" alt="" /></p>
<h1>二、监控所需组件</h1>
<h2>Prometheus server 大致流程</h2>
<p>它是prometheus的主程序,本身也是一个时序数据库,负责整个监控集群的数据拉取pull、处理、计算和存储。
完成下面的一些操作之后,prometheus会根据配置时间周期保存数据到本地或者是第三方存储中。</p>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=c3272dca10c7e8507d25ada65ca160da&amp;file=file.png" alt="" /></p>
<h2>Alertmanager</h2>
<p>它是prometheus的告警组件,负责整个集群的告警发送、分组、调度、警告抑制等功能。
<strong>alertmanager本身是不做告警规则计算的</strong>(alertmanager不去计算当前的监控取值是否达到我设定的阈值)。
alertmanager监听prometheus server发来的消息,然后在结合自己的配置,比如<strong>等待周期,重复发送告警时间,路由匹配</strong>等配置项,然后把接收到的消息发送到指定的接收者。同时他还支持多种告警接收方式,常见的如邮件、<strong>企业微信</strong>、钉钉等。</p>
<h2>Exporters</h2>
<p>暴露已有的第三方服务的metrics,等待server拉取。
它可以分为两类:</p>
<ul>
<li>直接采集:直接内置了对Prometheus监控的支持,比如cAdvisor,Kubernetes。都直接内置了用于向Prometheus暴露监控数据的端点。</li>
<li><strong>间接采集</strong>:原有监控目标并不直接支持Prometheus,所以需要通过Prometheus提供的Client Library编写该监控目标的监控采集程序。例如:Mysql Exporter,Redis Exporter等。</li>
</ul>
<h2>Grafana</h2>
<p>把Prometheus采集的数据通过dashboard来进行可视化展示。配置prometheus作为数据源后就能查看。</p>
<h1>三、Client需要做什么?</h1>
<h3>1. 为需要监控的服务生成相应的 metrics 并暴露给 Prometheus Server,Server pull 时直接返回实时状态。</h3>
<h3>2. 对公司公网出口、服务端公网出口、本机公网出口 放行exporter组件的端口</h3>
<h3>3. 向consul注册服务,这一步在服务端通过ansible脚本就可以操作</h3>
<h1>四、操作指引</h1>
<h2>【1】Prometheus</h2>
<h3>> web界面</h3>
<h4>Targets监控项</h4>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=ed44c40267c8b8f09511c8a385fd3fc9&amp;file=file.png" alt="" /></p>
<h4>Alerts告警详情</h4>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=54580947ff8fdbc01bab68a84760d198&amp;file=file.png" alt="" /></p>
<h4>Rules告警规则</h4>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=6f45b29e15248208a3fa3ec3e0d3eb20&amp;file=file.png" alt="" /></p>
<h4>Server Discovery服务发现</h4>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=a7472abc4ac6d927b9911e18bd3dbe17&amp;file=file.png" alt="" /></p>
<h3>> 配置文件prometheus.yml</h3>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=f1b8d757b9e6cc0774632572a4db7a8e&amp;file=file.png" alt="" />
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=22e30d456b1f295cac378f4ec44836f9&amp;file=file.png" alt="" /></p>
<h3>> 配置监控指标(规则),rules/host_sys.yml 自定义创建</h3>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=ace4ed35aa2226479e13c7a9f4e35eaf&amp;file=file.png" alt="" /></p>
<p>> 检查yml文件格式:
cd /usr/local/prometheus
./amtool check-config alertmanager.yml</p>
<h4>解析:</h4>
<p>> <strong>注意:</strong>yml文件严格要求格式对齐,描述内容需要双引号,否则报错!!!
alert:一个alert代表一个告警规则,也代表告警规则的名称
expr:告警指标,基于PromQL表达式告警触发条件,用于计算是否有时间序列满足该条件。
for:可选参数,用于表示只有当触发条件持续一段时间后才发送告警,在等待期间新产生告警的状态为pending。
labels:自定义标签,提示用户
annotations:指定一组附加信息,比如对当前告警的描述信息,产生的实际阈值等。</p>
<h3>> 触发阈值效果</h3>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=0097434c77ea29a12af30c4fa9537556&amp;file=file.png" alt="" /></p>
<h2>【2】Alertmanager</h2>
<h3>> web界面</h3>
<p>无告警
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=c448edeb047ed54a5ec6cf81f504e51a&amp;file=file.png" alt="" />
有告警
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=53d43bbbab4d8fa19ab641b98fdcc22a&amp;file=file.png" alt="" /></p>
<h3>> 配置文件alertmanager.yml</h3>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=2e64c9c5befb22115afcf500a3fc54bc&amp;file=file.png" alt="" /></p>
<h4>解析:</h4>
<p>> resolve_timeout: 这个解释有点绕,简单说就是在报警恢复的时候不是立马发送的,在接下来的这个时间内,如果没有此报警信息触发,才发送报警恢复消息。 默认值为5m。
group_by: [alertname] # 报警分组
group_wait: 10s # 在组内等待所配置的时间,如果同组内,10秒内出现相同报警,在一个组内出现。这个是为了把更多的告警一个批次发出去。
group_interval: 10s # 如果组内内容不变化,合并为一条警报信息,10s后发送。
repeat_interval: 5m # 发送报警间隔,如果指定时间内没有修复,则重新发送报警。
receivers: # 接收者
inhibit_rules: # 抑制器规则配置,合理设置抑制规则可以减少垃圾告警的产生,比如说节点宕机了, 在这个节点上面的各种服务报警都会触发的, 如果都发送不太方便定位问题,还容易带来巨大的压力。可以配置节点宕机压制节点层面的其他报警信息。
source_match # 第一条抑制规则
target_match # 匹配标签
equal # 就是source和target中都包含equal中列出的标签,且他们的值一致时,才会抑制
templates: #告警信息模板</p>
<h3>告警静默</h3>
<p>比如42.192.10.73这个节点持续处于宕机状态,并配置了repeat_interval: 5m,也就是如果故障没有解决,每5m会通知一次,直到故障恢复。但有可能这台服务器硬件损坏,短期内无法恢复,为了避免告警打扰,可以设置临时静默。</p>
<h4>1.查看告警,点击silence</h4>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=199eb81671e766ab3f69997488d76b80&amp;file=file.png" alt="" /></p>
<h4>2.设置静默</h4>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=d8d9cc4e03ff4a2eab477b8c5fabbb03&amp;file=file.png" alt="" /></p>
<h3>小结</h3>
<p>alertmanager集成报警系统的几个重要功能。
分组: 可以通过route书的group_by来进行报警的分组,多条消息一起发送。
抑制: 重要报警抑制低级别报警。
静默: 故障静默,确保在接下来的时间内不会在收到同样报警信息。</p>
<h2>【3】客户端注册流程</h2>
<p>通过consul注册到服务端,Prometheus自动发现</p>
<h3>注册脚本</h3>
<pre><code class="language-yaml">---
- name: exporter_registry
hosts: all
tasks:
- name: exporter_registry
shell: curl --request PUT -d '{&quot;ID&quot;:&quot;{{ ansible_hostname }}-{{ ansible_default_address }}-{{ Prometheus_exporter_tag }}&quot;,&quot;Name&quot;:&quot;{{ ansible_hostname }}-{{ ansible_default_address }}&quot;,&quot;Tags&quot;:[&quot;{{ Prometheus_game_tag }}&quot;],&quot;Address&quot;:&quot;{{ ansible_default_address }}&quot;,&quot;Port&quot;:{{ exporter_port }},&quot;Meta&quot;:{&quot;Name&quot;:&quot;{{ Prometheus_exporter_tag }}&quot;,&quot;group&quot;:&quot;{{ Prometheus_exporter_group_tag }}&quot;},&quot;EnableTagOverride&quot;:false,&quot;Check&quot;:{&quot;HTTP&quot;:&quot;http://{{ ansible_default_address }}:{{ exporter_port }}/metrics&quot;,&quot;Interval&quot;:&quot;10s&quot;},&quot;Weights&quot;:{&quot;Passing&quot;:10,&quot;Warning&quot;:1}}' {{ Prometheus_server_consulIP }}/v1/agent/service/register?replace-existing-checks=1
delegate_to: localhost
become: yes</code></pre>
<h3>执行脚本</h3>
<pre><code class="language-yaml">ansible-playbook -i 42.192.10.73:2020,
-e &quot;Prometheus_exporter_tag=mysqld_exporter&quot;
-e &quot;ansible_default_address=42.192.10.73&quot;
-e &quot;Prometheus_exporter_group_tag=长安幻想&quot;
-e &quot;exporter_port=9104&quot;
-e &quot;Prometheus_server_consulIP=180.184.138.201:8500&quot;
-e &quot;ansible_ssh_user=shiyue&quot;
exporter_register.yml</code></pre>
<h3>consul注册成功界面</h3>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=d6ac87cbb338d5e8a233f7b28e693fed&amp;file=file.png" alt="" />
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=7027e1a71b2ea15723b2203314730b38&amp;file=file.png" alt="" />
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=5caebc1b89780ecc100552524b37a59d&amp;file=file.png" alt="" />
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=98a503b530c82c1540ad43c4e0e4f78b&amp;file=file.png" alt="" /></p>
<h3>取消注册</h3>
<pre><code class="language-yaml">curl --request PUT http://180.184.138.201:8500/v1/agent/service/deregister/实例注册名称</code></pre>
<h2>【4】配置告警路由</h2>
<h3>web服务</h3>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=81b6bdb013caf64d6fb127bcbc04e289&amp;file=file.png" alt="" />
每一个函数对应一个机器人接口</p>
<h3>alertmanager.yml配置</h3>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=28d8659cff556f28e3cc044fad6ac1d3&amp;file=file.png" alt="" /></p>
<h2>【5】告警效果</h2>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=8b5be551cfe609feaadfc07c4ca6147f&amp;file=file.png" alt="" /></p>