• Post author:
  • Post category:prometheus
  • Post comments:0评论

一、介绍

  Prometheus 提供了服务发现机制,即通过自动化的机制来检测、分类和识别新的和变更的目标。一般有这几种类型:静态、基于文件、基于API、基于DNS。
  在只有监控几台服务器时,我们将目标配置在 Prometheus 的配置文件中,即静态服务发现:

[root@cp-3 ~]# cat /usr/local/prometheus/prometheus.yml
......
scrape_configs:
  - job_name: 'node'
    static_configs:
    - targets: ['localhost:9100', '10.88.88.11:9100', '10.88.88.12:9100']
......

[root@cp-3 ~]# curl -sL http://localhost:9090//api/v1/targets | jq .data.activeTargets[].scrapeUrl
"http://localhost:9100/metrics"
"http://10.88.88.11:9100/metrics"
"http://10.88.88.12:9100/metrics"
"http://localhost:9090/metrics"

  这样的话,添加删除目标都需要对配置文件进行修改并重启 Prometheus ,而且上了规模这样也不方便去维护。
  以下介绍下基于文件的服务发现和基于 Consul 的服务发现(其实就是基于 API 的方式),当然基于 API 的方式还支持不少平台,官方文档都是有介绍的:https://prometheus.io/docs/prometheus/latest/configuration/configuration/

二、基于文件的服务发现

  基于文件的方式用的配置是 file_sd_configs,它用于读取一组包含零个或多个列表的文件,这些文件可以是 YAML 或 JSON 格式,包含着定义的目标列表。

Prometheus:

[root@cp-3 ~]# vim /usr/local/prometheus/prometheus.yml
......
  - job_name: 'node'
    file_sd_configs:
      - files:
        - targets/*.json
        - targets/*.yaml
        refresh_interval: 10s
......

注:
  refresh_interval:   表示多久加载一次文件,默认5m

格式:

JSON:
    [ { "targets": [ "<host>", ... ], "labels": { "<labelname>": "<labelvalue>", ... } }, ... ]

YAML:
    - targets: [ - '<host>' ] labels: [ <labelname>: <labelvalue> ... ]

JSON:

[root@cp-3 ~]# mkdir /usr/local/prometheus/targets
[root@cp-3 ~]# cd /usr/local/prometheus/targets/
[root@cp-3 targets]# vim node.json
[{
  "targets": [
    "10.88.88.11:9100",
    "10.88.88.12:9100",   
    "localhost:9100"
  ],
  "labels": {
     "role": "test"  
  }
}]

YAML:

[root@cp-3 targets]# vim node.yaml 
- targets:
    - "10.88.88.11:9100"
    - "10.88.88.12:9100"
    - "localhost:9100"
  labels: 
    role: test

三、基于Consul的服务发现

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

安装 Consul:

[root@cp-3 ~]# yum install -y yum-utils
[root@cp-3 ~]# yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
[root@cp-3 ~]# yum -y install consul
[root@cp-3 ~]# vim /etc/consul.d/consul.hcl 
data_dir = "/opt/consul"
client_addr = "0.0.0.0"
ui_config{
  enabled = true
}
server = true
bind_addr = "10.0.0.14"
#advertise_addr = "10.88.88.13"

[root@cp-3 ~]# netstat -ntlp | grep consul
tcp        0      0 10.0.0.14:8300          0.0.0.0:*               LISTEN      30697/consul        
tcp        0      0 10.0.0.14:8301          0.0.0.0:*               LISTEN      30697/consul        
tcp        0      0 10.0.0.14:8302          0.0.0.0:*               LISTEN      30697/consul        
tcp6       0      0 :::8500                 :::*                    LISTEN      30697/consul        
tcp6       0      0 :::8600                 :::*                    LISTEN      30697/consul 

  此时可以访问 consul web界面:http://10.88.88.13:8500
  接下来,我们要注册服务到 Consul 中,可以通过其提供的 API 标准接口来添加。
  先注册一个服务,为本机 node-exporter 服务信息,服务地址及端口为 node-exporter 默认提供指标数据的地址,如下:

# 注册服务
[root@cp-3 consul]# curl -X PUT -d '{"id": "10.88.88.13","name": "node-exporter","address": "10.88.88.13","port": 9100,"tags": ["node-exporter"],"checks": [{"http": "http://10.88.88.13:9100/metrics", "interval": "5s"}]}'  http://10.88.88.13:8500/v1/agent/service/register

# 指定的服务信息
[root@cp-3 consul]# curl http://10.88.88.13:8500/v1/catalog/service/node-exporter

  配置 Prometheus 来使用 Consul 自动服务发现,使用 consul_sd_configs,它允许 Prometheus 从 Consul API 中抓取目标。
  官方文档:https://prometheus.io/docs/prometheus/latest/configuration/configuration/#consul_sd_config

[root@cp-3 ~]# vim /usr/local/prometheus/prometheus.yml
......
  - job_name: 'consul-prometheus'
    consul_sd_configs:
    - server: '10.88.88.13:8500'
      services:
        - node-exporter
      tags: 
      - node-exporter
......
[root@cp-3 ~]# systemctl restart prometheus
[root@cp-3 ~]# curl -sL http://localhost:9090//api/v1/targets | jq .data.activeTargets[].scrapeUrl
"http://10.88.88.13:9100/metrics"
"http://localhost:9090/metrics"

  可以看到,在 Targets 中能够成功的自动发现 Consul 中的 Services 信息,后期需要添加新的 Targets 时,只需要通过 API 往 Consul 中注册服务即可,Prometheus 就能自动发现该服务。

[root@cp-3 ~]# curl -X PUT -d '{"id": "10.88.88.11","name": "node-exporter","address": "10.88.88.11","port": 9100,"tags": ["node-exporter"],"checks": [{"http": "http://10.88.88.11:9100/metrics", "interval": "5s"}]}'  http://10.88.88.13:8500/v1/agent/service/register

[root@cp-3 ~]# curl -X PUT -d '{"id": "10.88.88.12","name": "node-exporter","address": "10.88.88.12","port": 9100,"tags": ["node-exporter"],"checks": [{"http": "http://10.88.88.12:9100/metrics", "interval": "5s"}]}'  http://10.88.88.13:8500/v1/agent/service/register

[root@cp-3 ~]# curl -sL http://localhost:9090//api/v1/targets | jq .data.activeTargets[].scrapeUrl
"http://10.88.88.12:9100/metrics"
"http://10.88.88.11:9100/metrics"
"http://10.88.88.13:9100/metrics"
"http://localhost:9090/metrics"

注,删除服务的接口,请求方式为PUT,格式如下:

curl -X PUT http://server_ip:8500/v1/agent/service/deregister/实例id

参考:

https://prometheus.io/docs
https://blog.csdn.net/aixiaoyang168/article/details/103022342

发表评论

验证码: 88 − 81 =