Prometheus 通過 consul 實現自動服務發現


整體框架

 

 

1、Consul 介紹

Consul 是基於 GO 語言開發的開源工具,主要面向分布式,服務化的系統提供服務注冊、服務發現和配置管理的功能。Consul 提供服務注冊/發現、健康檢查、Key/Value存儲、多數據中心和分布式一致性保證等功能。之前我們通過 Prometheus 實現監控,當新增一個 Target 時,需要變更服務器上的配置文件,即使使用 file_sd_configs 配置,也需要登錄服務器修改對應 Json 文件,會非常麻煩。不過 Prometheus 官方支持多種自動服務發現的類型,其中就支持 Consul。

2、環境、軟件准備

本次演示環境,是在Centos7 系統來執行操作,以下是安裝的軟件及版本:

  • System: CentOS Linux release 7.4.1708 (Core)
  • Docker: Docker version 20.10.3, build 48d30b5
  • Prometheus: Version 2.25.2
  • Consul: 1.6.1

注意:方便啟動 Prometheus、Consul服務,我使用 Docker 方式啟動,,這里忽略 Docker 的安裝過程。其中 Prometheus 安裝配置,可以參照之前文章 基於Centos7.4搭建prometheus+grafana+altertManger監控Spring Boot微服務(docker版),這里着重介紹一下如何啟動並配置 Consul 並配置 Prometheus 基於 Consul 實現自動服務發現。

3、Consul 安裝配置

使用 Docker 啟動 Consul 單節點服務,直接獲取最新版官方鏡像 consul:latest 命令如下:

[root@pro-gra-alt prometheus]# docker run --name consul -d -p 8500:8500 consul

啟動完畢后,瀏覽器訪問 http://ip:8500 地址(ip為宿主機地址),即可打開 Consul Web 管理頁面。可以看到默認只有 consul 一個 Service,后期我們注冊到 Consul 的 Service 都可以從頁面上看到,非常直觀。

4、API 注冊服務到 Consul

接下來,我們要注冊服務到 Consul 中,可以通過其提供的 API 標准接口來添加。那么先注冊一個測試服務,該測試數據為spring boot微服務,服務地址及端口為 spring boot 提供指標數據的地址,執行如下命令:

curl -X PUT -d '{"id": "springboot-anops","name": "anops-10.10.10.10:80","address": "10.10.10.10","port": 80,"tags": ["prometheus-target"],"checks": [{"http": "http://10.10.10.10:80/actuator/prometheus", "interval": "5s"}]}'  http://10.10.10.2:8500/v1/agent/service/register

腳本批量執行:

[root@monitor consul]# cat regdit.sh
#!/bin/bash

proj_array=(agent:10.10.10.9:8080 anops:10.10.10.8:8765)

for((i=0;i<${#proj_array[*]};i++))
do
        proj_name=`echo ${proj_array[i]} |awk -F':' '{print $1}'`
        ip_a=`echo ${proj_array[i]} |awk -F':' '{print $2}'`
        port=`echo ${proj_array[i]} |awk -F':' '{print $3}'`
        if [ "${proj_name}" == "agent" ];then
                curl -X PUT -d '{"id": "springboot-'${proj_name}'","name": "'${proj_name}'-'${ip_a}':'${port}'","address": "'${ip_a}'","port": '${port}',"tags": ["prometheus-target"],"checks": [{"http": "http://'${ip_a}':'${port}'/agent/actuator/prometheus", "interval": "5s"}]}'  http://10.10.10.2:8500/v1/agent/service/register
        else
                curl -X PUT -d '{"id": "springboot-'${proj_name}'","name": "'${proj_name}'-'${ip_a}':'${port}'","address": "'${ip_a}'","port": '${port}',"tags": ["prometheus-target"],"checks": [{"http": "http://'${ip_a}':'${port}'/actuator/prometheus", "interval": "5s"}]}'  http://10.10.10.2:8500/v1/agent/service/register
        fi
done
[root@monitor consul]#

  

執行完畢后,刷新一下 Consul Web 控制台頁面,可以看到成功注冊到 Consul 中。

 

 

 如果要注銷掉某個服務,可以通過如下 API 命令操作,例如注銷上邊添加的 spring boot微服務

curl -X PUT http://10.10.10.2:8500/v1/agent/service/deregister/springboot-anops

 

springboot-anops為實例id

 

 

 

 

5、配置 Prometheus 實現自動服務發現

現在 Consul 服務已經啟動完畢,並成功注冊了一個服務,接下來,我們需要配置 Prometheus 來使用 Consul 自動服務發現,目的就是能夠將上邊添加的服務自動發現到 Prometheus 的 Targets 中,增加 prometheus.yml 配置如下:

[root@pro-gra-alt prometheus]# cat prometheus.yml
global:
  scrape_interval:     60s
  evaluation_interval: 60s

scrape_configs:
  - job_name: prometheus
    static_configs:
      - targets: ['10.10.10.2:9090']
        labels:
          instance: prometheus

  - job_name: 'consul-prometheus'
    consul_sd_configs:
    - server: '10.10.10.2:8500'

    relabel_configs:
    - source_labels: [__metrics_path__]
      regex: /metrics
      target_label: __metrics_path__
      replacement: /actuator/prometheus
      action: replace
    - source_labels: [__meta_consul_service_id]
      regex: springboot-agent
      target_label: __metrics_path__
      replacement: /agent/actuator/prometheus
      action: replace
    - source_labels: [__meta_consul_service]
      separator: ;
      regex: (.*)
      target_label: application
      replacement: $1
      action: replace
    - source_labels: [__address__]
      separator: ":"
      regex: (127.0.0.1):(.*)
      target_label: __address__
      replacement: 10.10.10.2:${2}
      action: replace
    - source_labels: ['__meta_consul_tags']
      regex: '^.*,prometheus-target,.*$'
      action: keep

alerting:
  alertmanagers:
  - static_configs:
    - targets:
       - 10.10.10.2:9093

rule_files:
  - "/usr/local/prometheus/rules/*.rules"
[root@pro-gra-alt prometheus]#

其中 :

  • consul_sd_configs指定 Consul 的地址
  • relabel_configs 指定配置標簽覆蓋規則
  • __meta_consul_service
    - source_labels: [__meta_consul_service]
      separator: ;
      regex: (.*)
      target_label: application
      replacement: $1
      action: replace

這個配置是將 __meta_consul_service 重新映射為 application字段,方便 Prometheus 查詢  

  • __metrics_path__
    - source_labels: [__metrics_path__]
      regex: /metrics
      target_label: __metrics_path__
      replacement: /actuator/prometheus
      action: replace

這個配置是為了將 Prometheus 默認的拉取數據 /metrics改成 /actuator/prometheus,方便從 Spring拉取,如果有多種不同的應用,數據路徑不一樣,建議設置多個job,以使用不同的規則

 

    - source_labels: [__meta_consul_service_id]
      regex: springboot-agent
      target_label: __metrics_path__
      replacement: /agent/actuator/prometheus
      action: replace

這個配置是匹配到__meta_consul_service_id 為 sprintboot-agent 時,將__metrics_path__的值替換為/agent/actuator/prometheus (因為該微服務比其他微服務多了/agent,因此單獨過濾)

 

  • __meta_consul_tags
    - source_labels: ['__meta_consul_tags']
      regex: '^.*,prometheus-target,.*$'
      action: keep

這個配置是為了篩選指定tag的應用,只有有這個tag的應用才會拉取監控數據,這里是 prometheus-target,是在 Spring Boot的配置文件中配置的,這樣就可以避免拉取不相關的數據的應用(如 Consul自己的數據,替換路徑為/actuator/prometheus后無法獲取到監控數據)

配置完成后重新創建 Prometheus容器,配置文件掛載到宿主機

docker run -d -p 9090:9090 --name prometheus -v /etc/localtime:/etc/localtime -v /jws/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -v /jws/prometheus/rules/node-up.rules:/usr/local/prometheus/rules/node-up.rules prom/prometheus --config.file=/etc/prometheus/prometheus.yml --web.enable-lifecycle

重新創建容器后查看Prometheus UI 頁面的 Targets 或者Service Discovery是否配置成功

 

 

參考:https://cloud.tencent.com/developer/article/1536967

參考:https://hellowoodes.blog.csdn.net/article/details/106159160

參考:https://www.prometheus.wang/promql/prometheus-promql-functions.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM