Prometheus 是一种开源系统,用于监控系统和服务。Consul 是一个分布式的服务发现和配置管理系统。本文将介绍如何使用 Prometheus 抓取 Consul KV(键/值)存储的指标。Consul KV 存储是一个轻量级的键/值存储,用于在 Consul 集群中存储配置和元数据。

要将 Consul KV 存储的指标导入 Prometheus,我们需要使用 Prometheus 的 Consul SD(服务发现)插件。Consul SD 插件可以自动发现 Consul 中的服务,并将其添加到 Prometheus 的目标列表中。在此过程中,我们将使用 Consul KV 存储来存储 Prometheus 的配置信息。

以下是如何使用 Prometheus 抓取 Consul KV 存储的指标的步骤:

  1. 安装和配置 Consul 和 Prometheus

在安装 Consul 和 Prometheus 之前,确保您的系统上已经安装了 Docker 和 Docker Compose。接下来,创建一个名为 prometheus-consul 目录,并在该目录下创建一个名为 docker-compose.yml 的文件。

在 docker-compose.yml 文件中,添加以下内容:

version: '3'
services:
  consul:
    image: consul
    ports:
      - "8500:8500"
  prometheus:
    image: prom/prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      - ./consul_sd_configs:/etc/prometheus/consul_sd_configs
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'

在这个文件中,我们定义了两个服务:Consul 和 Prometheus。Consul 服务将运行一个 Consul 服务器,并将其暴露在 8500 端口上。Prometheus 服务将运行一个 Prometheus 服务器,并将其暴露在 9090 端口上。我们还定义了两个卷:prometheus.yml 和 consul_sd_configs。

prometheus.yml 文件将包含 Prometheus 的配置信息,我们稍后会进行更改。consul_sd_configs 目录将包含我们的 Consul SD 配置文件。

  1. 配置 Prometheus

在 prometheus.yml 文件中,我们将添加以下内容:

global:
  scrape_interval: 15s
  scrape_timeout: 10s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'consul'
    consul_sd_configs:
      - server: 'localhost:8500'
        services:
          - 'consul'
        datacenter: 'dc1'
        token: ''

    relabel_configs:
      - source_labels: [__meta_consul_tags]
        regex: (.*),prometheus(.*)
        action: keep
      - source_labels: [__meta_consul_node]
        target_label: instance
      - source_labels: [__meta_consul_service_address]
        target_label: __address__
      - source_labels: [__meta_consul_service_port]
        target_label: __port__

在这个文件中,我们定义了两个作业:prometheus 和 consul。

prometheus 作业使用了静态配置,将 Prometheus 服务器添加到目标列表中。

consul 作业使用了 Consul SD 配置,将 Consul 服务添加到目标列表中。我们还定义了一些 relabel_configs 规则,以将 Consul KV 存储的指标转换为 Prometheus 格式。这些规则将在下一步中更详细地讨论。

  1. 创建 Consul KV 存储

现在,我们将创建一个 Consul KV 存储,用于存储 Prometheus 的配置信息。在 Consul UI 中,创建一个名为 prometheus/config 的键,将其值设置为以下内容:

global:
  scrape_interval: 15s
  scrape_timeout: 10s

scrape_configs:
  - job_name: 'consul'
    metrics_path: /kv
    static_configs:
      - targets: ['localhost:9200']

在这里,我们定义了与 prometheus.yml 文件中相同的配置信息。我们还指定了 metrics_path 为 /kv,这将允许 Prometheus 从 Consul KV 存储中获取指标。

  1. 启动 Consul 和 Prometheus

现在,我们可以启动 Consul 和 Prometheus。在 prometheus-consul 目录中,运行以下命令:

docker-compose up -d

这将启动两个容器:Consul 和 Prometheus。您可以通过访问 http://localhost:8500 和 http://localhost:9090 来查看 Consul 和 Prometheus 的 UI。

  1. 查看指标

现在,您可以访问 Prometheus UI(http://localhost:9090)并查看 Consul KV 存储的指标。在 Prometheus UI 中,单击“Graph”选项卡,并使用以下查询查看 Consul KV 存储的指标:

consul_kv_key_values{key="prometheus/config"}

这将显示 Consul KV 存储中 prometheus/config 键的所有指标。

总结

本文中,我们讨论了如何使用 Prometheus 抓取 Consul KV 存储的指标。我们使用了 Prometheus 的 Consul SD 插件来自动发现 Consul 服务,并将其添加到 Prometheus 的目标列表中。我们还使用 Consul KV 存储来存储 Prometheus 的配置信息。最后,我们展示了如何在 Prometheus UI 中查看 Consul KV 存储的指标。

Prometheus 抓取 Consul KV 指标:完整指南

原文地址: https://www.cveoy.top/t/topic/lKcV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录