常见几种服务发现模式
静态服务发现

1
2
3
- job_name: 'test'
static_configs:
- targets: ['10.1.74.109:9104']

文件服务发现
周期性自动检查特定文件内容读取加载到配置,不需要重启

1
2
3
4
5
6
- job_name: 'file_sd_test'
file_sd_configs:
- refresh_interval: 15s
files:
- /data/prometheus/*.yml
- /data/prometheus/*.json

yml格式:

1
2
3
4
5
- targets: ['10.1.74.109:9100']
labels:
app: 'ceshi'
env: 'dev'
region: 'dev-1'

consul
面向分布式,提供服务注册、服务发现和配置管理。
通过exporter注册到consul服务,然后将数据汇聚到prometheus进行收集,exporter–>prometheus–>grafana,注册简单,批量监控

docker方式运行:http://$ip:8500访问正常即可

1
2
3
4
5
6
7
8
9
10
11
version: "3.5"
services:
consul:
image: harbor.od.com/consul:latest
container_name: consul
volumes:
- "/data/consul/data:/consul/data"
- "/data/consul/config:/consul/config"
ports:
- "8500:8500"
restart: always

安装测试3节点,以下使用ansible同步操作步骤,运行exporter:

1
2
3
4
5
6
[root@k8s-master ~]# ansible all -m ping -o 
10.1.74.21 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
10.1.74.22 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
10.1.74.20 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

[root@k8s-master ~]# ansible all -m shell -a "docker run -itd --name node-exporter -p 9100:9100 --restart always harbor.od.com/prometheus/node-exporter:v1.3.1"

注册consul:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@k8s-master ~]# cat consul.sh 
#!/bin/bash

for i in {20..22};do
curl -X PUT -d '{
"id": "node'$i'",
"name": "node_exporter",
"address": "10.1.74.'$i'",
"port": 9100,
"tags": ["prometheus"],
"checks": [{"http": "http://10.1.74.'$i':9100/metrics","interval": "15s"}]}' \
http://10.1.74.109:8500/v1/agent/service/register
done
[root@k8s-master ~]# sh consul.sh

upload successful

配置prometheus

增加consul配置项完成后重启prometheus,查看已自动发现

1
2
3
4
5
[root@ prometheus]# vi prometheus.yml
...
- job_name: 'consul'
consul_sd_configs:
- server: '10.1.74.109:8500'

upload successful

删除服务

1
[root@ prometheus]# curl -X PUT http://{cousul_ip}:8500/v1/agent/service/deregister/{ID号}