Consul是什么
Consul是一个服务管理软件。支持多数据中心下,分布式高可用的,服务发现和配置共享。用来保证服务的高可用,(本文章搭建一个单节点consul环境与prometheus环境实现自动注册功能);
二进制包安装,安装方法:
环境准备
1:下载安装包,最新版本根据官方为准;(consul官方下载地址:https://releases.hashicorp.com/consul)
cd /usr/local/ wget https://releases.hashicorp.com/consul/0.8.1/consul_0.8.1_linux_amd64.zip
|
2:解压缩 consul_0.8.1_linux_amd64.zip
unzip consul_0.8.1_linux_amd64.zip && mv consul_0.8.1_linux_amd64 consul |
3:启动consul服务器
./consul agent -dev -client 0.0.0.0 -ui |
4:prometheus与consul结合实现自动发现配置
# grep -v "^#" prometheus.yml global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). remote_write: - url: "http://192.168.152.137:9201/write" remote_read: - url: "http://192.168.152.137:9201/read" alerting: alertmanagers: - static_configs: - targets: ['192.168.152.137:9093'] # - ['localhost:9093'] rule_files: - "/opt/prometheus-2.15.1.linux-amd64/rules/cpu_usage.yml" - "/opt/prometheus-2.15.1.linux-amd64/rules/disk.yml" - "/etc/prometheus/promgen.rule.yml" scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090','192.168.152.180:9100','192.168.152.114:9100','192.168.151.41:9100','192.168.152.106:9100'] - job_name: 'consul' #prometheus与consul配置段 consul_sd_configs: - server: '192.168.152.186:8500' services: [ ] - job_name: 'ping' scrape_interval: 1m metrics_path: /probe params: module: [icmp] static_configs: - targets: ['192.168.152.106','192.168.152.114'] labels: type: '虚拟机' relabel_configs: - source_labels: [__address__] target_label: __param_target - source_labels: [__param_target] target_label: instance - target_label: __address__ replacement: 192.168.152.133:9115 - job_name: pushgateway static_configs: - targets: ['192.168.152.133:9091']
|
客户端通过HTTP API注册服务
注册服务:curl -X PUT -d '{"id": "node-exporter","name": "node-exporter-192.168.152.106","address": "192.168.152.106","port": 9100,"tags": ["test"],"checks": [{"http": "http://192.168.152.106:9100/metrics", "interval": "5s"}]}' http://192.168.152.186:8500/v1/agent/service/register 注销服务:curl -X PUT http://192.168.152.186:8500/v1/agent/service/deregister/node-exporter
|
通过页面查看是否注册成功
http://192.168.152.137:9090/targets |