Prometheus+Consul实现自动服务发现

prometheus+Consul自动服务发现减少修改修改prometheus.yml重启prometheus等操作

Consul 介绍

Consul有多个组件,但总体而言,它是基础架构中的一款服务发现和配置的工具。 它提供了几个关键功能:
服务发现:Consul client 可以提供服务,例如api或mysql,也可以使用Consul client来发现指定服务的提供者。 使用DNS或HTTP,应用程序可以轻松找到他们所依赖的服务。
健康检查:Consul client 可以提供任何数量的健康检查,或者与给定的服务(“Web服务器是否返回200 OK”),或与本地节点(“内存利用率是否低于90%”)相关联。 可以使用此信息来监控集群运行状况,服务发现组件使用此信息将流量从有问题的主机中移除出去。
KV Store:应用程序可以使用Consul的分层键/值存储,包括动态配置,功能标记,协调,leader选举等等。 简单的HTTP API使其易于使用。
多数据中心:Consul支持多个数据中心。 这意味着Consul的用户不必担心构建额外的抽象层以扩展到多个区域。
官方架构图
Consul

端口介绍

1
2
3
4
5
6
8500,客户端http api接口
8600,客户端DNS服务端口
8400,客户端RPC通信端口
8300,集群server RPC通信接口
8301,集群DC内部通信接口
8302,集群DC之间通信接口

prometheus服务发现 Service Discovery

Prometheus支持多种服务发现机制:文件,DNS,Consul,Kubernetes,OpenStack,EC2等等。基于服务发现的过程并不复杂,通过第三方提供的接口,Prometheus查询到需要监控的Target列表,然后轮训这些Target获取监控数据。

本地测试环境的Docker Compose

使用gliderlabs/registrator监听Docker进程,对于暴露了端口的容器,registrator会自动将该容器暴露的服务地址注册到Consul中。
使用NodeExporter采集当前主机数据,cAdvisor采集容器相关数据之prometheus自动发现相关服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
version: '2'

services:
consul:
image: consul
ports:
- 8400:8400
- 8500:8500
- 8600:53/udp
command: agent -server -client=0.0.0.0 -dev -node=node0 -bootstrap-expect=1 -data-dir=/tmp/consul

labels:
SERVICE_IGNORE: 'true'
registrator:
image: gliderlabs/registrator
depends_on:
- consul
volumes:

- /var/run:/tmp:rw
command: consul://consul:8500

prometheus:
image: quay.io/prometheus/prometheus
ports:
- 9090:9090
node_exporter:

image: quay.io/prometheus/node-exporter
pid: "host"
ports:
- 9100:9100
cadvisor:

image: google/cadvisor:latest
ports:
- 8080:8080
volumes:

- /:/rootfs:ro
- /var/run:/var/run:rw
- /var/lib/docker/:/var/lib/docker:ro

promethues.yml配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
global:
scrape_interval: 5s
scrape_timeout: 5s
evaluation_interval: 15s
scrape_configs:
- job_name: consul_sd
metrics_path: /metrics
scheme: http
consul_sd_configs:
- server: consul:8500
scheme: http
services:
- node_exporter
- cadvisor
- prometheus-node

静态注册方式:

使用配置文件注册服务,创建文件夹/etc/consul.d 启动指定配置文件目录-config-dir /etc/consul.d

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 cat consul/nodes-export.json 
{
"service":{
"id": "node",
"name": "prometheus-node",
"address": "172.18.241.194",
"port": 9100,
"tags": ["prometheus-target"],
"checks": [
{
"http": "http://172.18.241.194:9100/metrics",
"interval": "15s"
}
]
}
}

重新加载配置:

1
docker exec -it 7fa7d7e9ead9 consul reload

Consul
Consul

api服务注册方式:

用http的方式,直接调用/v1/agent/service/register接口注册:

1
curl -X PUT -d '{"id": "node","name": "prometheus-node","address": "172.18.241.194","port": 9100,"tags": ["node-exporter"],"checks": [{"http": "http://172.18.241.194:9100/","interval": "5s"}]}'     http://localhost:8500/v1/agent/service/register

修改后的compose文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
version: '2'

services:
consul:
image: consul
ports:
- 8400:8400
- 8500:8500
- 8600:53/udp
command: agent -server -client=0.0.0.0 -dev -node=node0 -bootstrap-expect=1 -config-dir=/consul/consul.d -data-dir=/tmp/consul

labels:
SERVICE_IGNORE: 'true'
volumes:
- ./consul:/consul/consul.d
registrator:

image: gliderlabs/registrator
depends_on:
- consul
volumes:

- /var/run:/tmp:rw
command: consul://consul:8500

prometheus:
image: quay.io/prometheus/prometheus
ports:
- 9090:9090
volumes:

- ./prometheus.yml:/etc/prometheus/prometheus.yml
command: --config.file=/etc/prometheus/prometheus.yml

node_exporter:
image: quay.io/prometheus/node-exporter
pid: "host"
ports:
- 9100:9100
cadvisor:

image: google/cadvisor:latest
ports:
- 8080:8080
volumes:

- /:/rootfs:ro
- /var/run:/var/run:rw
- /var/lib/docker/:/var/lib/docker:ro

http://consul.la/intro/what-is-consul