一、为什么要服务发现(sd:service discovery )
希望服务发现能做什么
- 自动添加 target ,海量的服务添加实在不便;
- 自动配置 lable ,lable 的设计和添加比 target 添加还麻烦;
- 无需重启 prometheus ;
二、prometheus 当前支持的 sd_config 配置
自带配置 | 适配 | 说明 |
---|---|---|
azure_sd_config | 微软 Azure 云 | Azure SD configurations allow retrieving scrape targets from Azure VMs. |
consul_sd_config | ||
digitalocean_sd_config | ||
docker_sd_config | ||
dockerswarm_sd_config | ||
dns_sd_config | ||
ec2_sd_config | ||
openstack_sd_config | ||
ovhcloud_sd_config | ||
puppetdb_sd_config | ||
file_sd_config |
||
gce_sd_config | ||
hetzner_sd_config | ||
http_sd_config |
||
ionos_sd_config | ||
kubernetes_sd_config | ||
kuma_sd_config | ||
lightsail_sd_config | ||
linode_sd_config | ||
marathon_sd_config | ||
nerve_sd_config | ||
nomad_sd_config | ||
serverset_sd_config | ||
triton_sd_config | ||
eureka_sd_config | ||
scaleway_sd_config | ||
uyuni_sd_config | ||
vultr_sd_config |
三、 http_sd_config 和 file_sd_config 对比
Item | File SD | HTTP SD |
---|---|---|
Event Based | Yes, via inotify | No |
Update frequency | Instant, thanks to inotify | Following refresh_interval |
Format | Yaml or JSON | JSON |
Transport | Local file | HTTP/HTTPS |
Security | File-Based security | TLS, Basic auth, Authorization header, OAuth2 |
四、http_sd_config
需要开发部署一个HTTP SD endpoint,prometheus周期性的http访问该endpoint获取targets。如下是返回格式范例:
Example response body:
[
{
"targets": [ "<host>", ... ],
"labels": {
"<labelname>": "<labelvalue>", ...
}
},
...
]
[
{
"targets": ["10.0.10.2:9100", "10.0.10.3:9100", "10.0.10.4:9100", "10.0.10.5:9100"],
"labels": {
"__meta_datacenter": "london",
"__meta_prometheus_job": "node"
}
},
{
"targets": ["10.0.40.2:9100", "10.0.40.3:9100"],
"labels": {
"__meta_datacenter": "london",
"__meta_prometheus_job": "alertmanager"
}
},
{
"targets": ["10.0.40.2:9093", "10.0.40.3:9093"],
"labels": {
"__meta_datacenter": "newyork",
"__meta_prometheus_job": "alertmanager"
}
}
]
五、file_sd_config
prometheus.yml
配置文件增加如下
scrape_configs:
- job_name: 'node'
file_sd_configs:
- files:
- 'targets.json'
编辑 targets.json
[
{
"targets": [
"localhost:9100"
],
"labels": {
"job": "node"
}
},
{
"targets": [
"localhost:9200"
],
"labels": {
"job": "node"
}
}
]
[
{
"labels": {
"job": "logstash1",
"env": "dev"
},
"targets": [
"localhost:9501",
"localhost:9502",
"localhost:9503"
]
}
]
[
{
"targets": ["localhost:9090", "example.org:443"]
}
]
yaml 格式
- targets:
[ - '<host>' ]
labels:
[ <labelname>: <labelvalue> ... ]
- targets: ['localhost:9090', 'example.org:443']
- targets: ['10.98.29.9:9100', '10.98.29.12:9100', '10.98.29.15:9100']
labels:
job: rtms-nacos
env: prd
- targets:
- 10.98.29.9:9100
- 10.98.29.12:9100
- 10.98.29.15:9100
labels:
job: rtms-nacos
env: prd