首页 技术 正文
技术 2022年11月11日
0 收藏 583 点赞 4,982 浏览 2976 个字

 

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自动发现监控目标大致流程做个介绍。

  1. 通过在consul注册服务或注销服务(监控targets)
  2. 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

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:

Prometheus基于consul自动发现监控对象   https://www.iloxp.com/archive/11/

Prometheus基于consul自动发现监控对象   https://www.iloxp.com/archive/11/

这是注册我本机node_exporter的示例,到grafana里面看下是否能获取,上面图片黑色部分能看到consul与prometheus relabel各个配置的对应关系。

按理说是能看到监控数据有中断的图片,由于我刚刚重新注册、注销consul服务的时间间隔太短了,所以看不出来,在grafana下的效果如下:

Prometheus基于consul自动发现监控对象   https://www.iloxp.com/archive/11/

从consul中注销刚刚注册的node_exporter prometheus 监控目标:

curl -X PUT http://127.0.0.1:8500/v1/agent/service/deregister/node-10-135-13-136

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,118
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,590
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,435
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,206
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,842
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,927