翼度科技»论坛 云主机 LINUX 查看内容

prometheus + consul 服务注册+报警 最佳实践

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
一、 说明

本文主要将prometheus和consul 结合起来使用,通过conusl 注册服务至prometheus,实现服务统一注册,统一报警管理等。
整个架构思路consul+consul_export+prometheus+alertmanager ,consul通过check检测服务状态,然后通过consul_export暴露给prometheus,prometheus通过alert规则,通知alertmanager,接着邮件通知管理人员。
在各服务器上安装node_expoert,然后将node配置到consul的hcl服务文件中,进行服务注册,当然也可以http api发现。然后在prometheus主yml文件中配置consul_sd_configs对服务进行发现。
单台架构如下。

 
 

本次server服务器有3台,组建consul集群,全部用做server端,然后在每一台都部署node_expoerter作为prometheus node节点。
IP规划如下:
172.19.100.212(prometheus主服务器,理论上可以不部署node_expoerter),   
172.19.100.213
172.19.100.52
二、consul集群搭建

2.1 consul安装

在每台服务器上安装consul。采用yum 安装,这也是我最推荐的方式。配置目录路径在/etc/consul.d。
  1. yum install -y yum-utils
  2. yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
  3. yum -y install consul
复制代码
 
2.2 集群文件配置

212服务器的配置如下。data_dir已经在默认的consul.hcl中有配置,所以注释掉了。其他两台服务器根据如下配置分别配置IP。systemct    start consul启动consul即可。
  1. server = true
  2. bootstrap_expect = 3
  3. #        data_dir = "/tmp/consul"
  4. node_name = "172.19.100.212"
  5. bind_addr = "172.19.100.212"
  6. client_addr = "0.0.0.0"
  7. ui_config = {
  8.   enabled = true
  9. }
  10. enable_script_checks = true
  11. start_join = [
  12.   "172.19.100.213",
  13.   "172.19.100.52"
  14. ]
  15. retry_join = [
  16.   "172.19.100.213",
  17.   "172.19.100.52"
  18. ]
复制代码

 
搭建好的集群如下:
 
2.3 consul_exporter安装

       consul_exporter主要用于将consul的check 报警导出到prometheus ,这样在prometheus就不需要再次配置报警规则了。
下载地址:https://github.com/prometheus/consul_exporter/releases
当然如果需要额外的报警功能,可以通过blackbox_exporter配置,里边有很多报警模块。      
        consul_export 不需要额外的配置,三台直接安装后启动即可,后续在prometheus中配置三台的地址即可。目前我只在212服务器上安装了。
  1. [root@zht-app001 opt]# tar -xzvf consul_exporter-0.11.0.linux-amd64.tar.gz
  2. consul_exporter-0.11.0.linux-amd64/
  3. consul_exporter-0.11.0.linux-amd64/LICENSE
  4. consul_exporter-0.11.0.linux-amd64/consul_exporter
  5. consul_exporter-0.11.0.linux-amd64/NOTICE
  6. [root@zht-app001 opt]# cd consul_exporter-0.11.0.linux-amd64
  7. [root@zht-app001 consul_exporter-0.11.0.linux-amd64]# ls
  8. consul_exporter  LICENSE  NOTICE
  9. [root@zht-app001 consul_exporter-0.11.0.linux-amd64]# nohup ./consul_exporter &
  10. [1] 11749
  11. [root@zht-app001 consul_exporter-0.11.0.linux-amd64]# nohup: ignoring input and appending output to ‘nohup.out’
  12. [root@zht-app001 consul_exporter-0.11.0.linux-amd64]# tail -f nohup.out
  13. ts=2024-03-18T02:07:50.153Z caller=consul_exporter.go:82 level=info msg="Starting consul_exporter" version="(version=0.11.0, branch=HEAD, revision=3e5be6411ce11ba011b9e4fe52029daba8160ee8)"
  14. ts=2024-03-18T02:07:50.153Z caller=consul_exporter.go:83 level=info build_context="(go=go1.21.5, platform=linux/amd64, user=root@34d50aaee2f8, date=20231225-15:29:52, tags=unknown)"
  15. ts=2024-03-18T02:07:50.154Z caller=tls_config.go:274 level=info msg="Listening on" address=[::]:9107
  16. ts=2024-03-18T02:07:50.154Z caller=tls_config.go:277 level=info msg="TLS is disabled." http2=false address=[::]:9107
复制代码

consul_exporter的可选配置如下,如果不配置都是默认的,consul的token我将在最后配置。配置命令如下:
  1. export CONSUL_HTTP_TOKEN=<your_token_here>
复制代码
 
2.4 consul token设置

三、prometheus 搭建

前往https://prometheus.io/download/ 下载最新的prometheus组件
3.1 node_exporter 安装 和注册

       每一个conul (server/client)agent上都需要安装node_exporter,然后分别对node进行注册。
3.1.1 安装

我将文件下载到了/opt目录,然后将node_exporter配置为系统service;文件名为node_exporter.service,放置在/etc/systemd/system目录下:
       配置如下:
  1. [Unit]
  2. Description=Node Exporter for Prometheus
  3. After=network-online.target
  4. [Service]
  5. #User=<非特权用户,例如 node_exporter>
  6. ExecStart=/opt/node_exporter-1.7.0.linux-amd64/node_exporter
  7. #EnvironmentFile=-/etc/default/node_exporter  # 如果有额外环境变量配置,可以在这里引用
  8. Restart=on-failure
  9. RestartSec=5s
  10. [Install]
  11. WantedBy=multi-user.target
复制代码
          操作命令如下
  1. systemctl daemon-reload
  2. systemctl start node_exporter.service
  3. systemctl status node_exporter.service
  4. systemctl enable node_exporter.service
复制代码
 
       状态如下
 

 
3.1.2 注册到consul

       在consul配置文件夹下新建hcl文件即可,配置如下,其余服务器按照一样的操作进行。
  1. [root@izwz92l6wmcnfyprbcxmjkz opt]# cd /etc/consul.d/
  2. [root@izwz92l6wmcnfyprbcxmjkz consul.d]# ls
  3. consul.env  consul.hcl  server.hcl
  4. [root@izwz92l6wmcnfyprbcxmjkz consul.d]# vim node_exporter.hcl
复制代码
 
       Token在后续配置,配置好后,重新启动consul
  1. services {
  2.   name = "node_172.19.100.52"
  3.   address = "172.19.100.52"
  4.   id = "node_172.19.100.52"
  5.   port = 9100
  6.   tags = ["prometheus","172.19.100.52"]
  7.   #token = "d1e6d941-8577-1f27-dd7b-0d8ac9212ba5"
  8.   checks {
  9.     id = "node_172.19.100.52_tcp_check"
  10.     name = "Node Exporter TCP Check"
  11.     interval = "10s"
  12.     timeout = "1s"
  13.     tcp = ":9100"
  14.   }
  15. }
复制代码
 
       可以看到node注册成功,check检查成功,这个结果后续我们要通过consul_exporter传到至prometheus。



 
3.2 安装prometheus

        将下载的tar.gz解下后,将prometheus配置成系统/etc/systemd/system/prometheus.service
  1. [Unit]
  2. Description=Prometheus Monitoring System
  3. Documentation=https://prometheus.io/docs/introduction/overview/
  4. After=network-online.target
  5. [Service]
  6. #User=prometheus
  7. #Group=prometheus
  8. ExecStart=/opt/prometheus-2.50.1.linux-amd64/prometheus  --config.file=/opt/prometheus-2.50.1.linux-amd64/prometheus.yml
  9. Restart=always
  10. RestartSec=10s
  11. LimitNOFILE=4096
  12. [Install]
  13. WantedBy=multi-user.target
复制代码
 
 
  1. sudo systemctl daemon-reload
  2. sudo systemctl start prometheus
  3. sudo systemctl status prometheus # 查看服务运行状态
  4. sudo systemctl enable prometheus # 设置开机启动
复制代码
 
3.3 配置consul_sd_config 和consul_exporter

       在prometheus.yml配置如下,然后重启prometheus 即可
 
  1. - job_name: "consul_export报警"
  2.     static_configs:
  3.       - targets: ["172.19.100.212:9107"] # 这里填写 Consul Exporter 监听的 IP 和端口
  4.     relabel_configs: [] # 根据需要重写标签
  5.   - job_name: "node_export服务发现"
  6.     consul_sd_configs:
  7.       - server: "172.16.100.212:8500"
  8.         #token: "4489cdd3-1952-4ded-4eb9-07a823f5b2ff"
  9.         #token: "3ce3c959-c9e5-7e2c-abe7-1fccbbfe01ae"
  10.         tags:
  11.           - "prometheus"
  12.     relabel_configs:
  13.       - source_labels: [__meta_consul_health]
  14.         target_label: service_health_status
复制代码
 
       界面如图
 

 
       点击consul_exporter报警的endpoint的的界面如下,可以看到服务的check状态。我们可以根据这些状态编写rules进行报警。
 

 
3.4 rules 报警规则配置

需要先在prometheus.yml中加如下配置
  1. rule_files:
  2.   # - "first_rules.yml"
  3.   # - "second_rules.yml"
  4.   - "prometheus.rules.yml"
复制代码
 
在prometheus.rules.yml配置如下,这是针对3.3中node的报警规则,一旦有匹配的expr,则会触发rules中的alert规则。而alert规则会直接传送到alertmanager里边查看默认的route,再决定下一步操作(比如发送邮件报警,或者发送到企业微信)。
  1. groups:
  2. #consu_export报警
  3. - name: consul_health_alerts
  4.   rules:
  5.     - alert: "consul服务报警"
  6.       expr: consul_health_service_status{status="critical"} == 1 # 1 表示critical状态
  7.       for: 1m # 可以根据实际情况调整这个时间窗口,比如持续5分钟未恢复才报警
  8.       labels:
  9.         severity: critical
  10.       annotations:
  11.         summary: "Consul Service {{ $labels.service_name }} is critical"
  12.         description: "Service '{{ $labels.service_name }}' has been in a critical state for more than 1 minutes."
  13.     - alert: "consul服务报警"
  14.       expr: consul_health_service_status{status="warning"} == 1
  15.       for: 1m # 可以根据实际情况调整这个时间窗口,比如持续5分钟未恢复才报警
  16.       labels:
  17.         severity: warning
  18.       annotations:
  19.         summary: "Consul Service {{ $labels.service_name }} is warning"
  20.         description: "Service '{{ $labels.service_name }}' has been in
  21. a warning  state for more than 1 minutes."
复制代码
 
这是prometheus的alert页面,显示的就是我上边配置的报警规则
 

3.5 alertmanager 配置

       需要现在prometheus.yml中加入以下配置
  1. # Alertmanager configuration
  2. alerting:
  3.   alertmanagers:
  4.     - static_configs:
  5.         - targets:
  6.             - localhost:9093
复制代码
 
 
将tar.gz解压到/opt/,然后配置系统服务/etc/systemd/system/ alertmanager.service
  1. [Unit]
  2. Description=Prometheus Alertmanager System
  3. After=network-online.target
  4. [Service]
  5. ExecStart=/opt/alertmanager-0.27.0.linux-amd64/alertmanager  --config.file=/opt/alertmanager-0.27.0.linux-amd64/alertmanager.yml
  6. Restart=on-failure
  7. RestartSec=5s
  8. [Install]
  9. WantedBy=multi-user.target
复制代码
 
 
  1. systemctl daemon-reload
  2. systemctl start alertmanager.service
  3. systemctl status alertmanager.service
  4. systemctl enable alertmanager.service
复制代码
 
alertmanager.yml配置如下,我通过QQ邮件进行报警。
  1. global:
  2.   resolve_timeout: 5m
  3. route:
  4.   receiver: "email-receiver"
  5. receivers:
  6.   - name: "email-receiver"
  7.     email_configs:
  8.       - send_resolved: true # 是否发送解决通知
  9.         to: "XXXXX" # 接收报警邮件的地址
  10.         from: "XXXXX " # 发送报警邮件的地址
  11.         smarthost: "smtp.qq.com:587" # SMTP服务器地址及端口
  12.         auth_username: "XXXXX" # SMTP用户名
  13.         auth_password: "XXXXX" # SMTP密码(如果是明文,推荐使用加密存储)
  14.         require_tls: true # 如果SMTP服务器支持TLS,则启用
  15. inhibit_rules: [] # 可选,抑制规则配置
复制代码
 
这是alertmanager页面,目前还没有报警,所以是空白的,下边我们来将其中一个node_export节点停掉,来观察一下报警。
 

 
3.6 报警演示

手动关闭52服务器上的node_exporter 服务 Systemctl stop node_exporter
1、consul最先check出报警

 

 
2、prometheus alert进入pending状态

如果1分钟内,状态没有改变,则变为firing
 

 
3、prometheus  alert进入firing状态

 

 
4、alertmanager平台触发报警

将通过alertmanager.yml中route规则发送邮件。
 

 
5、接收到报警邮件


 
 

6、报警解除,收到解除邮件

Systemctl start node_exporter后报警消失,发送接触报警邮件,至此,报警链条完美闭环。

 
 


来源:https://www.cnblogs.com/simendavid/p/18094978
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具