consul_template: https://www.hi-linux.com/posts/36431.html
1、下载安装包 并解压consul 得到consul二进制文件
https://releases.hashicorp.com/consul/1.3.1/consul_1.3.1_linux_amd64.zip
mkdir /application && mv consul /application
2、启动脚本启动consul_server
#!/bin/bash
bin_path=/application/
data_path="$bin_path"/data
nodename=`hostname`
idc=dc1
bindip=`/sbin/ifconfig eth0 |grep '\binet \b' |sed -nr 's#[^0-9]*([0-9.]*).*#\1#gp'`
bootstrap_expect=1
$bin_path/consul agent -server \
-bind=$bindip \
-ui \
-client=0.0.0.0 \
-bootstrap-expect=$bootstrap_expect \
-data-dir $data_path \
-node $nodename \
-datacenter=$idc \
-log-level=warn \
-pid-file=$bin_path/consul.pid
3、consul配合consul_template自动修改prometheus配置
参考博客:https://www.hi-linux.com/posts/36431.html
1、consul指定添加单个key
./consul kv put prometheus/kubernetes-apiserver/address 10.0.0.31
2、consul指定获取单个key
./consul kv get prometheus/kubernetes-apiserver/address
3、consul指定删除单个key
./consul kv delete prometheus/kubernetes-apiserver/address
4、consul_template安装
$ wget https://releases.hashicorp.com/consul-template/0.18.3/consul-template_0.18.3_linux_amd64.zip
$ unzip consul-template_0.18.3_linux_amd64.zip
$ mv consul-template /usr/local/bin/
5、prometheus配置文件来设置tcp端口检测机制:
- job_name: "tcp_port_discorvery"
scrape_interval: 5s
metrics_path: /probe
params:
module: [tcp_connect]
file_sd_configs:
- files:
- /home/work/software/prometheus/tcp_port.json
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: xxx.xx.xx.xx:31659
配置consul_template模板来进行配置渲染
vim /prometheus/template.json
[{{ range tree "prometheus/linux" }}
{
"targets": ["{{ .Value }}"],
"labels": {
"process_name": "{{ .Key }}"
}
},
{{ end }}
{
"targets": ["xxx.xx.xx.xx:80"],
"labels": {
"process_name": "tengine"
}
}
]
vim /application/prometheus.ops
consul {
address = "xxx.xx.xx.xx:8500"
}
template {
source = "/prometheus/template.json"
destination = "/home/work/software/prometheus/tcp_port.json"
command = "curl -X POST http://127.0.0.1:9090/-/reload"
}
测试模板生成文件 看到文件内容为空
consul-template -consul-addr xxx.xxx.xx.xx:8500 -template="complate.json:result" -once -dry
将要获取的key推到consul中注册
./consul kv put prometheus/tcp_port/k8smaster-apiserver 1xx.xx.xx.xx:6443
测试无误后启动consul-template
consul-template -config "/application/prometheus.ops" &
也可以添加到systemctl服务管理程序设置开机自启动