1. 程式人生 > 其它 >Prometheus 服務發現

Prometheus 服務發現

為什麼要有服務發現?

Prometheus Server的資料抓取工作於Pull模型,因而,它必需要事先知道各Target的位置,然後才能從相應的Exporter或Instrumentation中抓取資料
  • 對於小型的系統環境來說,通過static_configs指定各Target便能解決問題,這也是最簡單的配置方法;
    • 每個Targets用一個網路端點(ip:port)進行標識;
對於中大型的系統環境或具有較強動態性的雲端計算環境來說,靜態配置顯然難以適用; 因此,Prometheus為此專門設計了一組服務發現機制,以便於能夠基於服務註冊中心(服務匯流排)自動發現、檢測、分類可被監控的各Target,以及更新發生了變動的Target;

可整合的服務發現機制

不用場景中, 服務註冊中心的指標也會有不同.

  1. 共有云或私有云 LassS 雲自身儲存有平臺上的所有資源資訊, 其API Server 便可作為Prometheus 的服務發現媒介.

  2. Prometheus 也可以整合到多種不同的開源服務發現工具上, 以動態發現需要監控的目標. 例如: Consul, Eureka, Zookeeper, Serverset等

  3. Prometheus 也可以很好的整合 Kubernetes 平臺上, 通過其 API Server 動態發現各類被監控的 Pod , Service, Endpoint, Ingress 和 Node物件.

  4. Prometheus 還支援基於DNS 或者檔案的動態發現機制.

基於檔案的服務發現

  基於檔案的服務發現是僅僅略優於靜態配置的服務發現方式, 他不僅依賴於任何平臺和第三方服務, 因而也是最為簡單和通用的實現方式.

  Prometheus Server 定義從檔案中載入各個 Target 資訊,

Prometheus 配置檔案

[root@ops prometheus]# cat prometheus.yml

scrape_configs:
  - job_name: "prometheus"
    honor_timestamps: true
    scrape_interval: 15s
    scrape_timeout: 10s
    metrics_path: 
/metrics scheme: http file_sd_configs: - files: - targets/prometheus-*.yml refresh_interval: 1m # 重新整理間隔時間 - job_name: "nodes" honor_timestamps: true scrape_interval: 15s scrape_timeout: 10s metrics_path: /metrics scheme: http file_sd_configs: - files: - targets/nodes-*.yml refresh_interval: 1m

Target 配置檔案

[root@ops prometheus]# mkdir targets
[root@ops prometheus]# cd targets/
[root@ops targets]# cat prometheus-server.yml 
- targets:
  - 127.0.0.1:9090
  labels:
    app: prometheus-server

[root@ops targets]# cat nodes-linux.yml 
- targets:
  - 192.168.168.106:9100
  - 192.168.168.105:9100
  labels:
    app: node_exporter

基於Consul的服務發現

Prometheus的Consul服務發現機制將通過Consul的Catalog API來發現target;

  官網地址:https://prometheus.io/docs/prometheus/latest/configuration/configuration/#consul_sd_config

meta標籤

  • __meta_consul_address: # 目標地址
  • __meta_consul_dc: # 目標的資料中心名稱
  • __meta_consul_health: # 服務的健康狀態
  • __meta_consul_metadata_<key>: # 目標的每個節點元資料鍵值
  • __meta_consul_node: # 為目標定義的節點名稱
  • __meta_consul_service_address: # 目標的服務地址
  • __meta_consul_service_id: # 目標的服務ID
  • __meta_consul_service_metadata_<key>:# 目標的每個服務元資料鍵值
  • __meta_consul_service_port: # 目標的服務埠
  • __meta_consul_service: # 目標所屬的服務名稱
  • __meta_consul_tagged_address_<key>: # 每個節點標記目標的地址鍵值
  • __meta_consul_tags: # 由標籤分隔符連線的目標標籤列表

consul部署

xx

prometheus配置檔案

  # All nodes
  - job_name: 'nodes'
    consul_sd_configs:
    - server: "192.168.168.105:8500"
      tags:
      - "nodes"
      refresh_interval: 1m

consul配置檔案

[root@ops prometheus]# cat /etc/consul/nodes.json 
{
    "services": [
        {
            "id": "node_exporter-node01",
            "name": "node01",
            "address": "192.168.168.105",
            "port": 9100,
            "tags": ["nodes"],
            "checks": [{
                "http": "http://192.168.168.105:9100/metrics",
                "interval": "5s"    
            }]
        },
        {
            "id": "node_exporter-node02",
            "name": "node02",
            "address": "192.168.168.102",
            "port": 9100,
            "tags": ["nodes"],
            "checks": [{
                "http": "http://192.168.168.102:9100/metrics",
                "interval": "5s"
            }]
        },
        {
            "id": "node_exporter-node06",
            "name": "node06",
            "address": "192.168.168.106",
            "port": 9100,
            "tags": ["nodes"],
            "checks": [{
                "http": "http://192.168.168.106:9100/metrics",
                "interval": "5s"
            }]
        },
        {
            "id": "node_exporter-node07",
            "name": "node07",
            "address": "192.168.168.107",
            "port": 9100,
            "tags": ["nodes"],
            "checks": [{
                "http": "http://192.168.168.107:9100/metrics",
                "interval": "5s"
            }]
        },
        {
            "id": "node_exporter-mysql07",
            "name": "node055",
            "address": "192.168.168.105",
            "port": 9104,
            "tags": ["nodes"],
            "checks": [{
                "http": "http://192.168.168.105:9104/metrics",
                "interval": "5s"
            }]
        },
        {
            "id": "node_exporter-process",
            "name": "node066",
            "address": "192.168.168.105",
            "port": 9256,
            "tags": ["nodes"],
            "checks": [{
                "http": "http://192.168.168.105:9256/metrics",
                "interval": "5s"
            }]
        }

    ]
}

作者:閆世成

出處:http://cnblogs.com/yanshicheng

聯絡:[email protected]

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,如有問題或建議,請多多賜教,非常感謝。