Prometheus基于集群模式Consul自动发现(consul_sd_configs)

1. 安装配置 Consul 集群模式

Consul 集群模式支持高可用性,多个节点协同工作。以下是安装和配置步骤:

1.1 安装 Consul集群

  1. 下载并解压 Consul 二进制文件:

    1
    2
    3
    wget https://releases.hashicorp.com/consul/1.15.2/consul_1.15.2_linux_amd64.zip
    unzip consul_1.15.2_linux_amd64.zip
    sudo mv consul /usr/local/bin/
  2. 创建 Consul 数据目录:

    1
    sudo mkdir -p /opt/consul/data

1.2 配置 Consul 服务器节点

在每个服务器节点上创建 Consul 配置文件(如 /etc/consul.d/consul.json):

1
2
3
4
5
6
7
8
9
10
11
12
13
# 每个服务器节点的配置相似,唯一不同的是 node_name 和 bind_addr 的 IP 地址,还有retry_join的ip地址需要根据bind_addr去指定。
{
"datacenter": "dc1",
"node_name": "consul-server-1",
"server": true,
"bootstrap_expect": 3,
"bind_addr": "192.168.1.101",
"client_addr": "0.0.0.0",
"retry_join": ["192.168.1.102", "192.168.1.103"],
"data_dir": "/opt/consul/data",
"log_level": "INFO",
"ui": true
}

配置说明:

  • bootstrap_expect:期望的 Consul 服务器节点数量(如上面配置当中的3,表示会有3个节点)。
  • retry_join:加入集群的其他节点 IP。
  • bind_addr:当前节点的 IP 地址。

启动 Consul 服务器节点(集群模式需要在不同的服务器节点上进行启动):

1
consul agent -server -config-file=/etc/consul.d/consul.json

1.3 配置 Consul 客户端节点

客户端节点配置文件示例(位于 /etc/consul.d/consul.json):

1
2
3
4
5
6
7
8
9
10
11
# 客户端地址也有一个bind_addr监听客户端地址
{
"datacenter": "dc1",
"node_name": "consul-client-1",
"bind_addr": "192.168.1.104",
"client_addr": "0.0.0.0",
"retry_join": ["192.168.1.101", "192.168.1.102", "192.168.1.103"],
"data_dir": "/opt/consul/data",
"log_level": "INFO",
"ui": true
}

启动客户端节点:

1
consul agent -config-file=/etc/consul.d/consul.json

1.4 验证 Consul 集群

通过 Web UI 验证集群状态,访问 http://<任意服务器IP>:8500/ui


2. 将应用注册到 Consul 集群

Consul 支持 手动服务注册自动服务注册

2.1 手动注册服务

可以创建服务定义文件,如 /etc/consul.d/my-web-app.json,注册服务:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"service": {
"name": "my-web-app",
"tags": ["web", "v1"],
"port": 8080,
"check": {
"id": "my-web-app-health",
"name": "HTTP Health Check",
"http": "http://localhost:8080/health",
"interval": "10s",
"timeout": "1s"
}
}
}

然后通过以下命令重新加载 Consul 配置:

1
consul reload

2.2 自动注册服务

服务可以通过调用 Consul API 自动注册。例如,使用 curl 命令进行注册:

1
2
3
4
5
6
7
8
9
10
11
12
curl --request PUT --data \
'{
"ID": "my-web-app",
"Name": "my-web-app",
"Tags": ["web", "v1"],
"Address": "localhost",
"Port": 8080,
"Check": {
"HTTP": "http://localhost:8080/health",
"Interval": "10s"
}
}' http://localhost:8500/v1/agent/service/register

3. 配置 Prometheus 动态发现 Consul 注册的服务

3.1 修改 Prometheus 配置文件

在 Prometheus 配置文件 prometheus.yml 中添加 Consul 服务发现配置

1
2
3
4
5
6
7
8
9
10
scrape_configs:
- job_name: 'consul-services'
consul_sd_configs:
- server: 'localhost:8500'
services:
- 'my-web-app'
relabel_configs:
- source_labels: [__meta_consul_service]
regex: 'my-web-app'
action: keep

3.2 重新启动 Prometheus

更新配置后,重新启动 Prometheus:

1
./prometheus --config.file=prometheus.yml

3.3 验证服务发现

访问 Prometheus Web UI (http://localhost:9090),进入 Targets 页面,检查 my-web-app 是否已被动态发现并抓取。


总结

  • Consul 集群模式 提供高可用服务发现。
  • 应用可以通过手动或自动方式注册到 Consul。
  • Prometheus 可以通过 Consul 服务发现 动态抓取已注册的服务指标。

Prometheus基于集群模式Consul自动发现(consul_sd_configs)
https://blog.t-ao.cn/2025/03/19/Prometheus基于集群模式Consul自动发现(consul-sd-configs)/
作者
TAO
发布于
2025年3月19日
许可协议