2.5 动态配置采集目标
配置采集目标 targets:
static_configs
: 直接在配置文件中指定 targetsfile_sd_configs
: 通过 json 文件指定 targetsconsul_sd_configs
: 通过 consul 服务指定 targets
基于文件
通过 targets.json
文件定义所有的监控目标:
[
{
"targets": [ "localhost:8080"],
"labels": {
"env": "localhost",
"job": "cadvisor"
}
},
{
"targets": [ "localhost:9104" ],
"labels": {
"env": "prod",
"job": "mysqld"
}
},
{
"targets": [ "localhost:9100"],
"labels": {
"env": "prod",
"job": "node"
}
}
]
创建 prometheus 配置文件:
global:
scrape_interval: 15s
scrape_timeout: 10s
evaluation_interval: 15s
scrape_configs:
- job_name: 'file_ds'
file_sd_configs:
- refresh_interval: 1m
files:
- targets.json
prometheus 周期性读取文件中的内容,当文件中定义的内容发生变化时,不需要对 prometheus 进行重启
Consul
下载(https://www.consul.io/downloads.html)
$ wget https://releases.hashicorp.com/consul/1.4.4/consul_1.4.4_linux_amd64.zip
$ unzip consul_1.4.4_linux_amd64.zip
启动(开发者模式,单节点)
$ ./consul agent -dev
查看集群节点:
$ ./consul members
$ curl localhost:8500/v1/catalog/nodes
添加服务:
$ echo '{"service": {"name": "node_exporter", "tags": ["exporter"], "port": 9100}}' \
| sudo tee /etc/consul.d/node_exporter.json
$ consul agent -dev -config-dir=/etc/consul.d
$ curl http://localhost:8500/v1/catalog/service/node_exporter
通过 put 请求添加服务:
$ curl http://localhost:8900/v1/agent/service/register -X PUT -i -H "Content-Type:application/json" -d '{
"ID": "kafkaNode3",
"Name": "kafkaNode",
"Tags": [
"node"
],
"Address": "10.9.145.115",
"Port": 5556,
"EnableTagOverride": false,
"Check": {
"DeregisterCriticalServiceAfter": "90m",
"HTTP": "http://10.9.164.4:65431?Action=CheckHealth",
"Interval": "10s"
}
}'
$ curl http://localhost:8500/v1/catalog/services
$ curl http://localhost:8500/v1/catalog/service/kafkaNode
在consul_sd_configs定义当中通过server定义了Consul服务的访问地址,services则定义了当前需要发现哪些类型服务实例的信息,这里限定了只获取node_exporter的服务实例信息。
- job_name: node_exporter
metrics_path: /metrics
scheme: http
consul_sd_configs:
- server: localhost:8500
services:
- node_exporter