文章目录

  • 一、基础
  • 二、安装consul
  • 下载地址
  • 启动consul
  • 访问consul
  • 三、编写服务发现文件nodes.json
  • 四、prometheus配置consul发现
  • 修改prometheus.yml
  • 重启Prometheus
  • 参考



一、基础

prometheus基于consul的服务发现_prometheus

二、安装consul

下载地址

https://developer.hashicorp.com/consul/installprometheus基于consul的服务发现_服务发现_02

启动consul

mkdir -p /app/consul/{data,etc,bin}
unzip /home/devops/consul_1.17.2_linux_amd64.zip -d /app/consul/bin/
ln -sv /app/consul/bin/consul /usr/local/bin/consul
consul -v
cd /app/consul/
nohup consul agent -dev -ui -data-dir=/app/consul/data/ -config-dir=/app/consul/etc/ -client=0.0.0.0 &

访问consul

http://IP:8500/ui/prometheus基于consul的服务发现_服务发现_03

三、编写服务发现文件nodes.json

vim /app/consul/etc/nodes.json

{
  "services":[
    {
      "id":"node exporter-node01",
      "name": "node01",
      "address":"192.168.1.31",
      "port": 10050,
      "tags": ["nodes"] ,
      "checks": [{
        "http":"http://192.168.1.31:10050/metrics",
        "interval":"5s"
      }]
    },
    {
      "id":"node exporter-node02",
      "name": "node02",
      "address":"192.168.1.34",
      "port": 10050,
      "tags": ["nodes"] ,
      "checks": [{
        "http":"http://192.168.1.34:10050/metrics",
        "interval":"5s"
      }]
    },
    {
      "id":"node exporter-node03",
      "name": "node03",
      "address":"192.168.1.36",
      "port": 10050,
      "tags": ["nodes"] ,
      "checks": [{
        "http":"http://192.168.1.36:10050/metrics",
        "interval":"5s"
      }]
    }
  ]
}

consul reload #加载配置文件

prometheus基于consul的服务发现_consul_04

四、prometheus配置consul发现

修改prometheus.yml

增加如下配置

- job_name: "nodes"
  consul_sd_configs:
  - server: 192.168.1.34:8500
    tags:
      - "nodes"
    refresh_interval: 2m

prometheus基于consul的服务发现_prometheus_05

重启Prometheus

systemctl restart prometheus

可以发现现在获取的Targets里面有consul的字段

prometheus基于consul的服务发现_json_06

参考

https://www.bilibili.com/video/BV1PT4y1P7bX/?from=search&seid=851756632097160928