0
1 2
# 使用yum部署consul
yum install -y yum-utils
yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
yum -y install consul
# 或者直接下RPM包安装
wget https://rpm.releases.hashicorp.com/RHEL/7/x86_64/stable/consul-1.11.1-1.x86_64.rpm
rpm -ivh ./consul-1.11.1-1.x86_64.rpm
vi /etc/consul.d/consul.hcl
data_dir = "/opt/consul"
client_addr = "0.0.0.0"
ui_config{
enabled = true
}
server = true
bootstrap = true
acl = {
enabled = true
default_policy = "deny"
enable_token_persistence = true
}
systemctl enable consul.service
systemctl start consul.service
# 获取登录密码
consul acl bootstrap
# 记录 SecretID
module
,company
,project
,env
,name
,instance
编辑yaml文件,传入3个环境变量:
docker-compose up -d
http://{IP}:1026
cat docker-compose.yml
version: "3.8"
services:
flask-consul:
image: registry.cn-shenzhen.aliyuncs.com/starsl/flask-consul:latest
container_name: flask-consul
hostname: flask-consul
restart: always
volumes:
- /usr/share/zoneinfo/PRC:/etc/localtime
environment:
consul_token: xxxxx-xxxxx-xxxxx
consul_url: http://x.x.x.x:8500/v1
admin_passwd: xxxxxxxx
nginx-consul:
image: registry.cn-shenzhen.aliyuncs.com/starsl/nginx-consul:latest
container_name: nginx-consul
hostname: nginx-consul
restart: always
ports:
- "1026:1026"
volumes:
- /usr/share/zoneinfo/PRC:/etc/localtime
vi prometheus.yml
#####blackbox_exporter#####
- job_name: 'http_2xx'
metrics_path: /probe
params:
module: [http_2xx]
consul_sd_configs:
- server: 'x.x.x.x:8500'
token: 'xxx-xxx-xxx-xxx'
services: ['blackbox_exporter']
tags: ['http_2xx']
relabel_configs:
- source_labels: ["__meta_consul_service_metadata_instance"]
target_label: __param_target
- source_labels: ["__meta_consul_service_metadata_company"]
target_label: company
- source_labels: ["__meta_consul_service_metadata_env"]
target_label: env
- source_labels: ["__meta_consul_service_metadata_name"]
target_label: name
- source_labels: ["__meta_consul_service_metadata_project"]
target_label: project
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115
- job_name: 'tcp_connect'
metrics_path: /probe
params:
module: [tcp_connect]
consul_sd_configs:
- server: 'x.x.x.x:8500'
token: 'xxx-xxx-xxx-xxx'
services: ['blackbox_exporter']
tags: ['tcp_connect']
relabel_configs:
- source_labels: ["__meta_consul_service_metadata_instance"]
target_label: __param_target
- source_labels: ["__meta_consul_service_metadata_company"]
target_label: company
- source_labels: ["__meta_consul_service_metadata_env"]
target_label: env
- source_labels: ["__meta_consul_service_metadata_name"]
target_label: name
- source_labels: ["__meta_consul_service_metadata_project"]
target_label: project
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115
- job_name: 'http200igssl'
metrics_path: /probe
params:
module: [http200igssl]
consul_sd_configs:
- server: 'x.x.x.x:8500'
token: 'xxx-xxx-xxx-xxx'
services: ['blackbox_exporter']
tags: ['http200igssl']
relabel_configs:
- source_labels: ["__meta_consul_service_metadata_instance"]
target_label: __param_target
- source_labels: ["__meta_consul_service_metadata_company"]
target_label: company
- source_labels: ["__meta_consul_service_metadata_env"]
target_label: env
- source_labels: ["__meta_consul_service_metadata_name"]
target_label: name
- source_labels: ["__meta_consul_service_metadata_project"]
target_label: project
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115
- job_name: 'http4xx'
metrics_path: /probe
params:
module: [http4xx]
consul_sd_configs:
- server: 'x.x.x.x:8500'
token: 'xxx-xxx-xxx-xxx'
services: ['blackbox_exporter']
tags: ['http4xx']
relabel_configs:
- source_labels: ["__meta_consul_service_metadata_instance"]
target_label: __param_target
- source_labels: ["__meta_consul_service_metadata_company"]
target_label: company
- source_labels: ["__meta_consul_service_metadata_env"]
target_label: env
- source_labels: ["__meta_consul_service_metadata_name"]
target_label: name
- source_labels: ["__meta_consul_service_metadata_project"]
target_label: project
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:9115
主要是默认的配置,增加了4xx和忽略ssl的模块。
cat blackbox.yml
modules:
http_2xx:
prober: http
http:
valid_status_codes:
- 200
- 301
- 302
- 303
no_follow_redirects: true
http_post_2xx:
prober: http
http:
method: POST
tcp_connect:
prober: tcp
ssh_banner:
prober: tcp
tcp:
query_response:
- expect: "^SSH-2.0-"
- send: "SSH-2.0-blackbox-ssh-check"
icmp:
prober: icmp
http4xx:
prober: http
http:
valid_status_codes:
- 401
- 403
- 404
http200igssl:
prober: http
http:
valid_status_codes:
- 200
tls_config:
insecure_skip_verify: true
在文本文件中写入监控目标的信息:JOB名称,公司/部门,项目,环境,名称,实例url,每行一个,空格分隔。
执行导入脚本,即可导入所有监控目标到Consul,并符合Prometheus的自动发现配置。
修改脚本中的consul_token和consul_url,即可导入。
#!/usr/bin/python3
import requests,json
consul_token = 'xxxxxxxxxx'
consul_url = 'http://x.x.x.x:8500/v1'
with open('instance.list', 'r') as file:
lines = file.readlines()
for line in lines:
module,company,project,env,name,instance = line.split()
headers = {'X-Consul-Token': consul_token}
data = {"id": f"{module}/{company}/{project}/{env}@{name}",
"name": 'blackbox_exporter',
"tags": [module],
"Meta": {'module':module,'company':company,'project':project,'env':env,'name':name,'instance':instance}
}
reg = requests.put(f"{consul_url}/agent/service/register", headers=headers, data=json.dumps(data))
if reg.status_code == 200:
print({"code": 20000,"data": "增加成功!"})
else:
print({"code": 50000,"data": f'{reg.status_code}:{reg.text}'})
导入ID:9965
详细URL:https://grafana.com/grafana/dashboards/9965
所有代码都在里面,抛砖引玉。
https://github.com/starsliao/Prometheus/
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8