Consul简介
◼ 一款基于golang开发的开源工具,主要面向分布式,服务化的系统提供服务注册、服务发现和配置管理
的功能
◼ 提供服务注册/发现、健康检查、Key/Value存储、多数据中心和分布式一致性保证等功能
部署
curl -LO https://releases.hashicorp.com/consul/1.16.2/consul_1.16.2_linux_amd64.zip
unzip consul_1.16.2_linux_amd64.zip
mv consul /usr/local/bin
consul agent -dev -ui -data-dir=/consul/data/ \
-config-dir=/etc/consul/ -client=0.0.0.0
配置service安装
curl -LO https://releases.hashicorp.com/consul/1.16.2/consul_1.16.2_linux_amd64.zip
root@ubuntu20:~# mkdir /usr/local/consul/{data,config}
root@ubuntu20:~# chown consul.consul /usr/local/consul/{data,config}
mkdir -p /usr/local/consul/config/data
unzip consul_1.14.1_linux_amd64.zip -d /usr/local/consul
ln -s /usr/local/consul/consul /usr/bin/consul
useradd -r consul
chown consul.consul /usr/local/consul/{data,config}
vi /usr/lib/systemd/system/consul.service
[Unit]
Description="HashiCorp Consul - A service mesh solution"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target
[Service]
EnvironmentFile=-/etc/consul.d/consul.env
User=consul
Group=consul
ExecStart=/usr/bin/consul agent -dev -bootstrap \
-config-dir /usr/local/consul/config \
-data-dir /usr/local/consul/data \
-ui \
-log-level INFO \
-bind 127.0.0.1 \
-client 0.0.0.0
ExecReload=/bin/kill --signal HUP $MAINPID
KillMode=process
KillSignal=SIGTERM
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
访问
http://120.77.146.92:8500/
列出已经注册的服务:
curl -XGET http://localhost:8500/v1/agent/services
例如,下面定义了一个要注册的tomcat服务示例,它保存于tomcat.json文件中
root@ubuntu20:~# cat tomcat.json
{
"id": "server01",
"name": "server01.luohw.com",
"address": "server01.luohw.com",
"port": 9100,
"tags": ["node_export"],
"checks": [{
"http": "http://server01.luohw.com/metrics",
"interval": "5s"
}]
}
使用命令注册
curl -XPUT --data @tomcat.json http://localhost:8500/v1/agent/service/register
注册方式二
root@ubuntu20:~# cat server02.json
{
"services": [{
"id": "server02",
"name": "server02.luohw.com",
"address": "server02.luohw.com",
"port": 9100,
"tags": ["node_exporter"],
"checks": [{
"http": "http://server02.luohw.com:9100/metrics",
"interval": "5s"
}]
}
]
}
使用命令注册
consul services register server02.json
注销服务,也可以使用consul services deregister命令进行。
consul services deregister -id <SERVICE_ID>
root@ubuntu20:~# consul services deregister -id server02
Deregistered service: server02
基于consnl的服务发现
修改prometheus配置文件
注释file_sd_configs段
添加consul_sd_configs段
- job_name: "node"
metrics_path: '/metrics'
scheme: "http"
consul_sd_configs:
- server: "192.168.1.50:8500" #填写consul的地址
tags:
- "node_exporter" #consul中服务的标签
refresh_interval: 1m
检查Prometheus配置文件
root@consumer:/apps/prometheus# ./promtool check config ./prometheus.ym
命令行重启
root@consumer:/apps/prometheus# curl -XPOST localhost:9090/-/reload
查看prometheus
之前labels的os标签没有了,这个是基于consnl发现的