Prometheus基于consul自动发现监控对象 https://www.iloxp.com/archive/11/
Prometheus 监控目标为什么要自动发现
频繁对Prometheus配置文件进行修改,无疑给运维人员带来很大的负担,还有可能直接变成一个“配置小王子”,即使是配置小王子也会存在人为失误的情况。
Prometheus支持的多种服务发现机制
Prometheus数据源的配置主要分为静态配置和动态发现, 常用的为以下几类:
- static_configs: 静态服务发现
- file_sd_configs: 文件服务发现
- dns_sd_configs: DNS 服务发现
- kubernetes_sd_configs: Kubernetes 服务发现
- consul_sd_configs: Consul 服务发现
- ...
kubernetes 频繁更新的pod,svc,等等资源配置应该是最能体现Prometheus监控目标自动发现服务的好处。
Prometheus基于consul自动发现监控对象
Consul是一个分布式k/v数据库,是当前比较流行的服务注册组件,下面对consul+prometheus自动发现监控目标大致流程做个介绍。
- 通过在consul注册服务或注销服务(监控targets)
- Prometheus 一直监视(watch)consul服务,当发现consul中符合要求的服务有新变化是更新Prometheus监控对象
Prometheus主要配置prometheus.yml中的scrape_configs以及consul_sd_configs如下:
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']
- job_name: 'consul'
consul_sd_configs:
- server: 'localhost:8500'
relabel_configs:
- source_labels: [__meta_consul_tags]
regex: .*,prome,.*
action: keep
- source_labels: [__meta_consul_service]
target_label: job
- scrape_configs 配置数据源
- consul_sd_configs 基于consul服务发现的配置
- relabel_configs 重新标记
consul_sd_configs的相关relabel通过下面实例来说明。Prometheus安装这边不说明了,到prometheus目录启动即可:
./prometheus --config.file="prometheus.yml" --web.listen-address="0.0.0.0:9090" --web.enable-admin-api --web.enable-lifecycle &
--web.enable-admin-api 运行通过web方式管理prometheus(删除清空数据等操作)
--web.enable-lifecycle 运行通过web方式重新加载prometheus配置(相当于reload)
我这里懒得配置腾讯云安全组规则了,直接nginx反向代理暴露一个80端口出来,配置如下:
server {
listen 10.135.13.136:80;
server_name p.iloxp.com www.p.iloxp.com;
resolver 8.8.8.8 8.8.4.4 valid=300s;
location / {
limit_req zone=allips burst=5 nodelay;
proxy_pass http://127.0.0.1:9090;
}
include agent_deny.conf;
location ~ /\.ht {
deny all;
}
}
启动一个consul服务
consul安装比较简单,下载二进制放在/usr/local/bin/目录下即可使用consul命令启动一个agent,consul 集群需至少启动一个server agent,下面是我单机启动consul server的一个示例:
consul agent -server -bootstrap-expect 1 -data-dir=/data/consul -node=server1 -bind=127.0.0.1 -client=127.0.0.1 -ui &
consul将默认侦听TCP 8500端口,-ui表示启用web。
我这里懒得配置腾讯云安全组规则了,直接nginx反向代理暴露一个80端口出来,配置如下:
server {
listen 10.135.13.136:80;
server_name c.iloxp.com www.c.iloxp.com;
resolver 8.8.8.8 8.8.4.4 valid=300s;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8500;
}
include agent_deny.conf;
location ~ /\.ht {
deny all;
}
}
测试整个流程
向consul注册一个服务作为prometheus的监控target
curl -X PUT -d '{"id": "node-10-135-13-136_test","name": "node_exporter","address": "10.135.13.136","port": 9100,"tags": ["prod","prome","node"],"checks": [{"http": "http://10.135.13.136:9100/metrics","interval": "35s"}]}' http://localhost:8500/v1/agent/service/register
检查consul和prometheus是否已有对应target:


这是注册我本机node_exporter的示例,到grafana里面看下是否能获取,上面图片黑色部分能看到consul与prometheus relabel各个配置的对应关系。
按理说是能看到监控数据有中断的图片,由于我刚刚重新注册、注销consul服务的时间间隔太短了,所以看不出来,在grafana下的效果如下:

从consul中注销刚刚注册的node_exporter prometheus 监控目标:
curl -X PUT http://127.0.0.1:8500/v1/agent/service/deregister/node-10-135-13-136
Prometheus基于consul自动发现监控对象 https://www.iloxp.com/archive/11/的更多相关文章
- zabbix使用自动发现监控esxi的磁盘存储storage
zabbix使用自动发现监控esxi的磁盘存储storage 在任意一台可以访问vcenter的zabbix-agent服务器上添加exsi的磁盘监控模板即可 创建模板过程: custom.esxi. ...
- zabbix自动发现监控url
1.在监控客户机上 web_site_code_status.sh: #!/bin/bash UrlFile="/opt/scripts/WEB.txt" IFS=$'\n' we ...
- zabbix自动发现监控mysql
一. 数据库给只读权限 1.1 grant usage on *.* to 'zabbix'@'127.0.0.1' identified by 'zabbix'; flush privileges; ...
- Prometheus + Consul 自动发现服务监控
一.Prometheus支持的多种服务发现机制(常用如下) static_configs: 静态服务发现 file_sd_configs: 文件服务发现 dns_sd_configs: DNS 服务发 ...
- 使用lld自动发现监控多实例redis
zabbix 可以通过常见的手段监控到各种服务,通过编写脚本来获取返回值并将获取到的值通过图形来展现出来,包括(系统.服务.业务)层面.可是有些时候在一些不固定的场合监控一些不固定的服务就比较麻烦.例 ...
- 使用 zabbix 自动发现监控 MySQL
介绍 使用 zabbix 的 low-level 自动发现功能完成单主机多端口的监控, 详见low_level_discovery, 整体上监控类似 percona 的 zabbix 监控插件, 不过 ...
- zabbix监控自动发现监控tomcat(V1)
背景说明: 由于zabbix监控使用自带的模版,只能监控主机上只有1个tomcat的场景适合,虽然网上很多朋友都是在每个监控项上面添加一个空格来解决问题.但是个人感觉这种方法还是蛮麻烦的,所以写一篇使 ...
- Zabbix自动发现监控Tomcat进程
1.编辑自动发现脚本 自动发现脚本只支持JSON格式 #!/usr/bin/env python # -*- coding:utf-8 -*- import commands import psuti ...
- Sping Cloud hystrix.stream 自动发现-监控
相关组件安装脚本 [root@java_gateway4 java_tps]# cat cront_install.sh #!/bin/bashyum install jq -ymkdir /home ...
随机推荐
- 自制php操作mysql工具类(DB.class.php)
DB.class.php <?php class DB{ //主机地址 var $host; //用户名 var $username; //密码 var $password; //数据库名 va ...
- mysql数据库备份,主从复制及半同步复制
1.使用mysqldump备份数据库并通过备份及二进制日志还原数据(备份完后再写入数据,然后再删库) mysqldump -A --single-transaction -F --master-dat ...
- MySQL增加行号rownum
select * from ( select @rownum:=@rownum+1 AS rownum, app_t.* from ( select * from app_custom where 1 ...
- Please provide compiled classes of your project with sonar.java.binaries property
是因为一个jar包版本的原因,sonar-java-plugin-5.1.0.13090.jar 需要降级 https://repo.maven.apache.org/maven2/org/sonar ...
- java获取本机外网ip
public static String getV4IP(){ String ip = ""; String chinaz = "http://ip.chinaz.com ...
- WebSocketServer
@Component @ServerEndpoint(value = "/endpoint/ws") public class WebSocketServer { private ...
- [LeetCode] 0752. Open the Lock 打开转盘锁
题目 You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', ' ...
- DT开发笔记-Cookie作用域的设置
当网站任意一个模块绑定了二级域名或者会员公司主页开启了二级域名时,必须设置cookie作用域,否则会导致二级域名站点不能显示正确的登录状态,js权限错误等问题(例如评论框显示不完全的现象). 进入网站 ...
- postgresql —— 数组类型
创建数组 CREATE TABLE sal_emp ( name text, pay_by_quarter integer[] --还可以定义为integer[4]或integer ARRAY[4] ...
- 2019红帽杯部分wp
xx 程序首先取输入的前4个字符作为xxtea加密的密钥之后进行xxtea加密.接着进行位置置换操作,然后又进行了以3个为一组的异或 首先逆向解出xxtea加密之后的结果 #include<st ...