consul+vm 自动发现监控主机

Consul 安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# docker-compose.yaml
version: '3.6'
services:
consul:
image: swr.cn-south-1.myhuaweicloud.com/starsl.cn/consul:latest
hostname: consul
container_name: consul
restart: always
ports:
- "8500:8500"
volumes:
- /opt/consul/data:/consul/data
- /opt/consul/config:/consul/config
- /usr/share/zoneinfo/PRC:/etc/localtime
command: "agent"
networks:
- TenSunS
networks:
TenSunS:
name: TenSunS
driver: bridge
ipam:
driver: default
# TenSunS 为consul 管理界面

WEB UI:http://10.168.140.45:8500/ui/dc1/services 可以直接访问 8500 端口

配置

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
log_level = "error"
data_dir = "/consul/data"
client_addr = "0.0.0.0"
ui_config{
enabled = true
}
ports = {
grpc = -1
https = -1
dns = -1
grpc_tls = -1
serf_wan = -1
}
peering {
enabled = false
}
connect {
enabled = false
}
server = true
bootstrap_expect=1
acl = {
enabled = true
default_policy = "deny"
enable_token_persistence = true
tokens {
initial_management = "9602e8a5-c754-43f0-b0ce-861b0df1b5df"
agent = "9602e8a5-c754-43f0-b0ce-861b0df1b5df"
}
}

默认配置:

https://developer.hashicorp.com/consul/docs/agent/config/config-files

数据注册

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 结构类似于
ConsulData{
ID: sid,
Name: "node_exporter",
Address: ip,
Port: 9100,
Tags: []string{vendor},
Meta: map[string]string{
"vendor": vendor,
"region": region,
"name": name,
"projectChineseName": projectFullName,
"projectShortName": projectShortName,
"instance": exportAddress,
"env": env,
},
Check: map[string]string{
"tcp": exportAddress,
"interval": "60s",
},
}

服务注册文档:

https://developer.hashicorp.com/consul/api-docs/agent/service#register-service

VM 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
scrape_configs:
# Scrape OpenStack instances
- job_name: "Openstack"
consul_sd_configs:
- server: "10.168.140.45:8500"
datacenter: 'dc1'
token: '9602e8a5-c754-43f0-b0ce-861b0df1b5df'
relabel_configs:
- source_labels: [__meta_consul_service]
regex: "consul"
action: drop
- regex: __meta_consul_service_metadata_(.+)
replacement: ${1}
action: labelmap

参考:

https://cloud.tencent.com/developer/article/1611091