企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 基于Consul的服务发现 ## 一、Consul的安装和配置 基于Consul实现的服务发现,一定程度上可以解决基于fie文件的服务发现的弊端。 ### 1.1 docker安装 centos1上,此处使用docker方式安装启动Consu单节点服务,直接获取官方镜像consul:1.14.5 命令如下 孝拉取镜像 docker pull consul:1.14.5 #运行consul容器,占据8500端口 docker run.name consul -d -p 8500:8500 consul:1.14.5 #查看正在运行的consu1容器 docker ps -a|grep 8500 ### 1.2 访问Consul http://192.168.100.87:8500/ ### 1.3 api方式服务注册 ![](https://img.kancloud.cn/d6/4c/d64c32d70951abfbb8b9cbc6e85eb2fe_595x274.png) * 命令行方式注册node1 centos1上运行 curl -X PUT -d '{ "id":"node1","name":"node_exporter","address":"node_exporter","port":9100,"tags":["exporter"],"meta":{"job":"node_exporter","instance":"Prometheus服务器所在Linux监控"},"checks":[{"http":"http://192.168.100.87:9100/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #id字段,不允许重复 #name字段 服务得名称 #address 服务地址 #port 服务端口 #meta 元数据,可以理解是这个待监测服务的一些辅助参数 #checks 这个待监测服务的健康检查的地址信息 * json方式注册node2 centos1上创建json文件 mkdir /data/consul cd /data/consul #创建json文件 cat >node2_exporter.json<<"EOF" { "id": "node2", "name": "node_exporter", "tags":[ "exporter" ], "address":"192.168.100.141", "port": 9100, "meta":{ "job": "node_exporter", "instance":"centos2股务器Linux" }, "check":{ "http": "http://192.168.100.141:9100/metrics", "interval":"10s" } } EOF 执行json文件,注册node2 curl --request PUT --data @node2_exporter.json http://localhost:8500/v1/agent/service/register 刷新8500的web界面,可看到注册的两个node1/node2成功 ![](https://img.kancloud.cn/cf/b6/cfb638e0327f1226b259ded173122356_1581x522.png) 备注:除了我们自己注册的两个Lnux操作系统服务node1/node2之外,Consul的agent还会将自己注册为一个名为consul的服务。 ### 1.4 api方式注销某个服务 如果要注销掉某个服务,可以通过如下 API命令操作,例如注销上边添加的 node2服务 curl -X PUT http://localhost:8500/v1/agent/service/deregister/node2 #说明一下这里最后的node2是consul中exporter的id. ## 二、配置Prometheus实现自动服务发现 现在 ConsuI服务已经启动完毕,并成功注册了两个服务,接下来,我们需要配置Prometheus 来使用 Consu自动服务发现,目的就是能够将上边添加的服务自动发现到Prometheus的Targets中,首先备份源配置文件 #将上一章节的基于文件的服务发现配置文件备份一下 cd /data/docker-prometheus/prometheus cp -a prometheus.yml prometheus.yml.filebak ll *.filebak ### 2.1增加consul_sd_configs的配置 增加 prometheus.yml配置如下 vi prometheus.yml - job_name: "consul_exporter" consul_sd_configs: - server: 192.168.100.87:8500 services: [] relabel_configs: - source_labels: [__meta_consul_tags] regex: .*exporter.* action: keep - regex: __meta_consul_service_metadata_(.+) action: labelmap &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;保存后输入命令更新: curl -XPOST http://localhost:9090/-/reload &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;成功监听 ![](https://img.kancloud.cn/23/81/2381ee9bfd56e8f28c547ec28a9cb8c5_1796x248.png) ### 2.2 验证自动发现 * 增加node3 curl -X PUT -d '{ "id":"node3","name":"node_exporter","address":"node_exporter","port":9100,"tags":["exporter"],"meta":{"job":"node_exporter","instance":"Prometheus服务器所在Linux监控"},"checks":[{"http":"http://192.168.100.87:9100/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register * 制除node3 #注销剽除node3的id curl -X PUT http://localhost:8500/v1/agent/service/deregister/node3 ## 三、将其他的服务加入Consul自动发现 ### 3.1 创建添加脚本 centos1上执行 cat>/data/consul/api.sh <<"EOF" #nginx curl -X PUT -d '{ "id":"nginx1","name":"nginx_exporter","address":"192.168.100.141","port":9113,"tags":["exporter"],"meta":{"job":"nginx_exporter","instance":"centos2服务器所在nginx监控"},"checks":[{"http":"http://192.168.100.141:9113/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #rabbitmq curl -X PUT -d '{ "id":"rabbitmq1","name":"rabbitmq_exporter","address":"192.168.100.141","port":9419,"tags":["exporter"],"meta":{"job":"rabbitmq_exporter","instance":"centos2服务器所在rabbitmq监控"},"checks":[{"http":"http://192.168.100.141:9419/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #redis curl -X PUT -d '{ "id":"redis1","name":"redis_exporter","address":"192.168.100.141","port":9121,"tags":["exporter"],"meta":{"job":"redis_exporter","instance":"centos2服务器所在redis监控"},"checks":[{"http":"http://192.168.100.141:9121/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #mongo curl -X PUT -d '{ "id":"mongodb1","name":"mongodb_exporter","address":"192.168.100.141","port":9216,"tags":["exporter"],"meta":{"job":"mongodb_exporter","instance":"centos2服务器所在mongodb监控"},"checks":[{"http":"http://192.168.100.141:9216/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #mysqld curl -X PUT -d '{"id":"mysqld1","name":"mysqld_exporter","address":"192.168.100.141","port":9104,"tags":["exporter"],"meta":{"job":"mysqld_exporter","instance":"centos2服务器所在mysql监控"},"checks":[{"http":"http://192.168.100.141:9104/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #cadvisor curl -X PUT -d '{"id":"cadvisor2","name":"cadvisor","address":"192.168.100.141","port":8079,"tags":["exporter"],"meta":{"job":"cadvisor","instance":"centos2服务器所在docker监控"},"checks":[{"http":"http://192.168.100.141:8079/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register curl -X PUT -d '{"id":"cadvisor1","name":"cadvisor","address":"192.168.100.87","port":8079,"tags":["exporter"],"meta":{"job":"cadvisor","instance":"centos1服务器所在docker监控"},"checks":[{"http":"http://192.168.100.87:8079/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #process curl -X PUT -d '{"id":"process1","name":"process_exporter","address":"192.168.100.141","port":9256,"tags":["exporter"],"meta":{"job":"process_exporter","instance":"centos2服务器所在进程监控"},"checks":[{"http":"http://192.168.100.141:9256/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #golang自定义程序 curl -X PUT -d '{"id":"golang_exporter1","name":"golang_exporter","address":"192.168.100.58","port":8182,"tags":["exporter"],"meta":{"job":"golang_exporter","instance":"go自定义程序监控"},"checks":[{"http":"http://192.168.100.58:8182/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #prometheus curl -X PUT -d '{"id":"prometheus1","name":"prometheus","address":"192.168.100.87","port":9090,"tags":["exporter"],"meta":{"job":"prometheus","instance":"Prometheus服务器 Prometheus Server"},"checks":[{"http":"http://192.168.100.87:9090/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #domain curl -X PUT -d '{"id":"domain1","name":"domain_exporter","address":"baidu.com","port":9222,"tags":["domain"],"meta":{"job":"domain_exporter","instance":"百度域名监控"},"checks":[{"http":"http://192.168.100.141:9222/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register curl -X PUT -d '{"id":"domain2","name":"domain_exporter","address":"qq.com","port":9222,"tags":["domain"],"meta":{"job":"domain_exporter","instance":"qq域名监控"},"checks":[{"http":"http://192.168.100.141:9222/metrics","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #snmp curl -X PUT -d '{"id":"snmp1","name":"snmp_exporter","address":"192.168.100.141","port":9116,"tags":["snmp"],"meta":{"job":"snmp_exporter","instance":"centos2服务器所在snmp监控"},"checks":[{"http":"http://192.168.100.141:9116","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #blackbox_http curl -X PUT -d '{"id":"blackbox_http1","name":"blackbox_http_exporter","address":"https://www.baidu.com","port":9115,"tags":["blackbox_http"],"meta":{"job":"blackbox_http_exporter","instance":"centos1服务器所在黑盒http监控"},"checks":[{"http":"http://192.168.100.87:9115","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register curl -X PUT -d '{"id":"blackbox_http2","name":"blackbox_http_exporter","address":"https://wy.ldw520.top","port":9115,"tags":["blackbox_http"],"meta":{"job":"blackbox_http_exporter","instance":"centos1服务器所在黑盒http监控"},"checks":[{"http":"http://192.168.100.87:9115","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #blackbox_tcp curl -X PUT -d '{"id":"blackbox_tcp1","name":"blackbox_http_exporter","address":"192.168.100.87:9090","port":9115,"tags":["blackbox_tcp"],"meta":{"job":"blackbox_http_exporter","instance":"centos1服务器所在黑盒tcp监控"},"checks":[{"http":"http://192.168.100.87:9115","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register curl -X PUT -d '{"id":"blackbox_tcp2","name":"blackbox_tcp_exporter","address":"192.168.100.141:9100","port":9115,"tags":["blackbox_tcp"],"meta":{"job":"blackbox_tcp_exporter","instance":"centos1服务器所在黑盒tcp监控"},"checks":[{"http":"http://192.168.100.87:9115","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register #blackbox_icmp curl -X PUT -d '{"id":"blackbox_icmp1","name":"blackbox_icmp_exporter","address":"192.168.100.87","port":9115,"tags":["blackbox_icmp"],"meta":{"job":"blackbox_icmp_exporter","instance":"centos1服务器所在黑盒icmp监控"},"checks":[{"http":"http://192.168.100.87:9115","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register curl -X PUT -d '{"id":"blackbox_icmp2","name":"blackbox_icmp_exporter","address":"192.168.100.141","port":9115,"tags":["blackbox_icmp"],"meta":{"job":"blackbox_icmp_exporter","instance":"centos1服务器所在黑盒icmp监控"},"checks":[{"http":"http://192.168.100.87:9115","interval":"10s"}]}' http://localhost:8500/v1/agent/service/register EOF 增加 prometheus.yml配置如下 vi prometheus.yml - job_name: "consul_snmp-exporter" scrape_interval: 30s scrape_timeout: 30s metrics_path: snmp params: module: [if_mib] consul_sd_configs: - server: '192.168.100.87:8500' services: [ ] relabel_configs: - source_labels: [__meta_consul_tags] regex: .*snmp.* action: keep - regex: __meta_consul_service_metadata_(.+) action: labelmap - source_labels: [ '__meta_consul_service_address' ] target_label: __param_target - source_labels: [ __param_target ] target_label: instance - target_label: __address__ replacement: 192.168.100.141:9116 - job_name: "consul_blackbox_http" metrics_path: probe params: module: [ http_2xx ] consul_sd_configs: - server: '192.168.100.87:8500' services: [ ] relabel_configs: - source_labels: [ __meta_consul_tags ] regex: .*blackbox_http.* action: keep - regex: __meta_consul_service_metadata_(.+) action: labelmap - source_labels: [ '__meta_consul_service_address' ] target_label: __param_target - source_labels: [ __param_target ] target_label: instance - target_label: __address__ replacement: blackbox_exporter:9115 - job_name: "consul_blackbox_tcp" metrics_path: probe params: module: [ tcp_connect ] consul_sd_configs: - server: '192.168.100.87:8500' services: [ ] relabel_configs: - source_labels: [ __meta_consul_tags ] regex: .*blackbox_tcp.* action: keep - regex: __meta_consul_service_metadata_(.+) action: labelmap - source_labels: [ '__meta_consul_service_address' ] target_label: __param_target - source_labels: [ __param_target ] target_label: instance - target_label: __address__ replacement: blackbox_exporter:9115 - job_name: "consul_blackbox_icmp" metrics_path: probe params: module: [ icmp ] consul_sd_configs: - server: '192.168.100.87:8500' services: [ ] relabel_configs: - source_labels: [ __meta_consul_tags ] regex: .*blackbox_icmp.* action: keep - regex: __meta_consul_service_metadata_(.+) action: labelmap - source_labels: [ '__meta_consul_service_address' ] target_label: __param_target - source_labels: [ __param_target ] target_label: instance - target_label: __address__ replacement: blackbox_exporter:9115 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;保存后输入命令更新: curl -XPOST http://localhost:9090/-/reload &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;成功监听 ![](https://img.kancloud.cn/00/e7/00e752110ba465f8a07285a2ac31550c_1800x809.png) <hr> ## 四、基于Consul与基于文件两种服务发现模式的比较 | 基于Consul | 基于文件 优点 | Consul是独立服务,可以做高可用提升配置可靠性 | 实现简单,去中心化 缺点 | 增加了一层服务去管理配置 | 配置文件与Prometheus服务器耦合 ,配置文件的备份与恢复麻烦 ## 五、ConsulManager图形界面管理 ### 4.1 ConsulManager是什么 传统的Consul项目的配置管理是基于命令行模式或者json配置模式,管理效率低、不直观。可以借助于ConsulManager开源项日实现图形界面的管理,增强管理的易用性 ### 4.2安装配置ConsulManager * 优先完成Consul配置1.14.5 下载ConsulManager的docker-compose.yml进入centos1的服务器 mkdir /data/consulmanager -p cd /data/consulmanager wget https://starsl.cn/static/img/docker-compose.yml * 修改dockercompose.yml #修改3个环境交量 #cosul token:consul的登录token:当前测试环境可以不填token,这样consul使用无密码 #consul_url:consul的 URL(http开头:/v1要保留) #admin passwd:录ConsulManager web的admin密码 cd /data/consulmanager dacker-compose pull docker-compose up -d docker ps -a lgrep consul 访问地址 http://192.168.100.87:1026 ![](https://img.kancloud.cn/fc/c7/fcc70e6abb9d0d685ad8312fad717983_1826x874.png) ![](https://img.kancloud.cn/64/63/6463764a483752119b4c44af769dd529_1885x989.png) ### 4.3 使用ConsulManager管理服务发现 * 添加待监控targets ![](https://img.kancloud.cn/35/9f/359f06fb2232e9c490479362fae936f4_800x901.png) * 删除监控targets ![](https://img.kancloud.cn/e1/e2/e1e2f782cded33ea52a9c9efb22a4b8c_1783x478.png)