Redian新闻
>
Prometheus 服务自动发现机制介绍

Prometheus 服务自动发现机制介绍

公众号新闻

Prometheus的服务发现的几种类型:

  • 基于文件的服务发现;
  • 基于DNS的服务发现;
  • 基于API的服务发现:Kubernetes、Consul、Azure......

Prometheus为什么需要自动发现?

Prometheus Server的数据抓取工作于Pull模型,因而,它必需要事先知道各Target的位置,然后才能从相应的Exporter或Instrumentation中抓取数据, 对于小型系统来说,通过static_configs就可以解决此问题,这也是最简单的配置方法;对于中大型系统环境或具有较强动态性的云计算环境来说,静态配置显然难以适用,因此,Prometheus为此专门设计了一组服务发现机制,以便能够通过服务注册中心自动发现、检测、分类可被检测的各target,以及更新发生了变动的target。

Prometheus指标抓取的生命周期

发现 -> 配置 -> relabel -> 指标数据抓取 -> metrics relabel

  • 在每个scrape_interval期间,Prometheus都会检查执行的作业(Job);

  • 这些作业首先会根据Job上指定的发现配置生成target列表,此即服务发现过程;

  • 服务发现会返回一个Target列表,其中包含一组称为元数据的标签,这些标签都以“__meta_”为前缀;

  • 服务发现还会根据目标配置来设置其它标签,这些标签带有“__”前缀和后缀,包括“__scheme__”、 “__address__”和“__metrics_path__”,分别保存有target支持使用协议(http或https,默认为http)、target的地址及指标的URI路径(默认为/metrics);

  • 若URI路径中存在任何参数,则它们的前缀会设置为“__param_;

  • 配置标签会在抓取的生命周期中被重复利用以生成其他标签,例如,指标上的instance标签的默认值就来自于__address__标签的值;

  • 抓取而来的指标在保存之前,还允许用户对指标重新打标并过滤,在job段metric_relabel_configs配置,通常用来删除不需要的指标、删除敏感或不必要的标签和添加修改标签格式等。

自动发现的几种方式演示

基于文件的自动发现

此种类型也是最简单的服务发现方式,主要是通过Prometheus Server定期从文件中加载target的信息。文件可以是json或者yaml格式,它含有定义的target列表,以及可选的标签信息。

vi prometheus.yml# static config nodes  - job_name: 'nodes'    file_sd_configs:    - files:                                                     - targets/nodes-*.yaml        refresh_interval: 2m     scrape_interval: 15s

然后将所有要发现的target全部放在targets/目录下即可,例如

cat targets/nodes-linux.yaml - targets:  - monitor.example.com:9100  - node.export1.com:9101  - node.export2.com:9101  - node.export3.com:9101  labels:    app: node-exporter    os: aliyunos3
cat targets/nodes-prometheus.yaml - targets: - monitor.example.com:9090 labels: app: prometheus job: prometheus

重新加载Prometheus配置即可:

curl -XPOST monitor.example.com:9090/-/reload

基于consul注册中心自动发现

consul是一款基于golang开发的开源工具,主要面向分布式,服务化的系统提供服务注册、服务发现和配置管理的服务,提供服务注册/发现、健康检查、Key/Value存储、多数据中心和分布式一致性保证等功能。

服务部署

多种部署方式,这里仅是使用consul的功能,并不考虑高可用或其他问题,采用docker-compose方式部署。

vi docker-compose.yml
version: '3.6'
volumes: consul_data: {}
networks: monitoring: driver: bridge
services: consul: image: consul:1.14 volumes: - ./consul_configs:/consul/config - consul_data:/consul/data/ networks: - monitoring ports: - 8500:8500 command: ["consul","agent","-dev","-bootstrap","-config-dir","/consul/config","-data-dir","/consul/data","-ui","-log-level","INFO","-bind","127.0.0.1","-client","0.0.0.0"]
 consul-exporter:    image: prom/consul-exporter:v0.8.0    networks:      - monitoring    ports:      - 9107:9107    command:      - "--consul.server=consul:8500"    depends_on:      - consul

这里顺便把consul-exporter也部署了 直接启动:

# docker-compose up -d 
# docker-compose psNAME IMAGE COMMAND SERVICE CREATED STATUS PORTSconsul-and-exporter-consul-1 consul:1.14 "docker-entrypoint.s…" consul 24 hours ago Up 24 hours 8300-8302/tcp, 8301-8302/udp, 8600/tcp, 8600/udp, 0.0.0.0:8500->8500/tcp, :::8500->8500/tcpconsul-and-exporter-consul-exporter-1 prom/consul-exporter:v0.8.0 "/bin/consul_exporte…" consul-exporter 24 hours ago Up 24 hours 0.0.0.0:9107->9107/tcp, :::9107->9107/tcp

可以通过ip:8500直接访问consul,这里示例并没有设置token,正常生产环境需要token来进行身份验证:

编辑Prometheus.yml

需要注意,使用consul自动发现时,需要在job中通过标签来匹配对应的target,例如:

vi prometheus.yml
# consul_service_discovery - job_name: 'nodes' consul_sd_configs: - server: "monitor.example.com:8500" tags: - "nodes" # 匹配在consul注册的服务中带有nodes标签的service refresh_interval: 2m scrape_interval: 15s
- job_name: 'grafana' consul_sd_configs: - server: "monitor.example.com:8500" tags: - "grafana" # 匹配在consul注册的服务中带有grafana标签的service refresh_interval: 2m scrape_interval: 15s

重新加载:

curl -XPOST monitor.example.com:9090/-/reload

服务注册到consul

服务注册到consul有两种方式,一种是使用consul客户端命令进行操作,另一种是通过api操作。api方式注册演示 准备json文件

vi grafana.json
{ "ID": "grafana", "Name": "grafana", "Tags": ["grafana", "v9"], # 包含的标签 "Address": "monitor.example.com", "Port": 3000, "Meta": { "grafana_version": "9" # 元数据,可自定义 }, "EnableTagOverride": false, "Check": { # 检查健康状态的方法 "http": "http://monitor.example.com:3000/metrics", "interval": "5s", "Timeout": "5s" }, "Weights": { "Passing": 1, "Warning": 1 }}

健康检查方法也可以是执行脚本,例如:

"Check": { "DeregisterCriticalServiceAfter": "90m", "Args": ["/usr/local/bin/check_redis.py"], "Interval": "10s", "Timeout": "5s" },

注册服务:

curl -XPUT --data @grafana.json http://monitor.example.com:8500/v1/agent/service/register

查看状态:

通过consul自动发现的target会有很多__meta_consul开头的标签,我们可以通过relabel来重新利用这些标签,这个下篇笔记总结。常用的 api 指令:

  • 查看当前所有注册的service
curl http://monitor.example.com:8500/v1/agent/services
  • 查看tomcat service的健康状态
curl http://monitor.example.com:8500/v1/agent/health/service/name/tomcat
  • 注册服务,需提前准备好json文件
curl -XPUT --data @grafana.json http://monitor.example.com:8500/v1/agent/service/register
  • 注销服务
curl -XPUT http://monitor.example.com:8500/v1/agent/service/deregister/grafana

官网文档:https://developer.hashicorp.com/consul/api-docs/agent/service

consul命令方式注册演示

准备nodes.json文件,同一类型的target可以写到一个json文件中,便于编辑注册

{  "services": [    {      "id": "node.export1.com",      "name": "node.export1.com",      "address": "node.export1.com",      "port": 9101,      "tags": ["nodes"],      "checks": [{        "http": "http://node.export1.com:9101/metrics",        "interval": "5s"      }]    },    {      "id": "node.export2.com",      "name": "node.export2.com",      "address": "node.export2.com",      "port": 9101,      "tags": ["nodes"],      "checks": [{        "http": "http://node.export2.com:9101/metrics",        "interval": "5s"      }]    },    {      "id": "node.export3.com",      "name": "node.export3.com",      "address": "node.export3.com",      "port": 9101,      "tags": ["nodes"],      "checks": [{        "http": "http://node.export3.com:9101/metrics",        "interval": "5s"      }]    },    {      "id": "monitor.example.com",      "name": "monitor.example.com",      "address": "monitor.example.com",      "port": 9100,      "tags": ["nodes"],      "checks": [{        "http": "http://monitor.example.com:9100/metrics",        "interval": "5s"      }]    }  ]}

将node.json文件放置到consul服务启动的"-data-dir"目录下,此示例为容器内/consul/data

/consul/config # pwd/consul/config/consul/config # lsnodes.json

执行config重新加载

consul reloadConfiguration reload triggered

查看consul及Prometheus状态

至此,Prometheus基于consul的自动发现基本演示完毕。

链接:https://blog.csdn.net/u012516914/article/details/131820812

(版权归原作者所有,侵删)


微信扫码关注该文公众号作者

戳这里提交新闻线索和高质量文章给我们。
相关阅读
“互联网”要写成the Internet还是the internet?Looking to Buy a Home in China? It Might Come With a Gold Ingot.Hurun China Metaverse Companies with the Greatest Potential 2023He Wrote the Book on Rural China. But First, He Wrote a Novel.速递 | Aleta Biotherapeutics 的CAR T 细胞激活剂 ALETA-001 获得 MHRA 临床试验授权Grow and glow, from the inside out——2023届Zier升学结果和去向报告究竟应该说someone faces problems还是problems face someone?咀外文嚼汉字(241)“借金渍”,日本借钱谚语Young Graduates Are Selling Their Knowledge on the Streets每天10句英语口语|Let's go to the sea tomorrowThe Mall at the Center of the Universe西雅图地区高端住宅开发商推荐(三)—— Steven D. Smith Custom HomeRazer AU Kraken Kitty Chroma USB Gaming Headset, QuartzNetizens Go Crazy For Cockroaches’ Northern Expedition《黄浦江》 —— 第6章 玉人垂泪(2)是否可以说hope somebody to do something?How a Chinese Town Made a Fortune From the World’s Lockdown Pets学人习语297:pull the rug from under sb's feet | 学生订阅特惠开箱即用~ 神器 Prometheus 如何深入浅出?全能王加航里程再出力作,全球最佳头等舱/商务舱(ANA The Suite/The Room)一网打尽,助我顺利回国K8s CPU Limits 造成的事故,竟让 Prometheus 轻松解决了?COMPUT METH PROG BIO | 生物学年龄预测模型的构建文革 - 血色,但不浪漫搞懂 Prometheus 这四种指标类型,谁都可能成为监控老司机?每天10句英语口语|The ball rolled down the hillCheckmate HATE: 10th Annual Chess-in-the- ParkFrom Streets to Pages: Inside the Life of a Beijing Courier每天10句英语口语|How's the weather there today?The Private Practice Heirs of China’s Socialist Aid Program歌剧“波希米亚人”观后感冰岛Kirkjufellsfoss,热点风景To Flip the Script on Harassment, Grease is the New Word谷歌新动作,ChromeOS将不再是原来的ChromeOSAmid Uproar, Ctrip Pulls Plug on Women-Only Train CompartmentsClinical Nutrition Heats Up in China’s Pet Health Industry
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。