关于consul:prometheus结合consulconfd实现动态注册服务和动态更新配置告警规则

5次阅读

共计 4129 个字符,预计需要花费 11 分钟才能阅读完成。

一、prometheus 装置应用

1. 创立配置文件

mkdir /opt/prometheus    
cd /opt/prometheus/
vim prometheus.yml
  • 内容如下:
global:
  scrape_interval:     60s #抓取数据的工夫距离
  evaluation_interval: 60s #触发告警检测的工夫
scrape_configs:
  - job_name: prometheus  #工作名称
    static_configs:  #动态配置节点
      - targets: ['localhost:9090']
        labels:
          instance: prometheus  #实例名称

2. 装置 prometheus

docker run  -d \
  -p 9090:9090 \
  -v /opt/prometheus/prometheus1.yml:/etc/prometheus/prometheus.yml \
  -v /opt/prometheus/data:/prometheus \
  prom/prometheus \

3. 拜访地址:http://localhost:9090/graph

二、consul 装置应用

1. 装置 consul

docker run \ 
--name consul \ 
-p 8500:8500 \ 
-v /data/consul/conf/:/consul/conf/ \ 
-v /data/consul/data/:/consul/data/ \ 
-d consul

拜访地址:http://ip:8500/graph(ip 为你装置的服务器地址)

2. 应用 consul

  • 首先理解 consul+prometheus 的大抵流程

(1) 在服务器上装一个 node 节点(exporter 节点),启动节点,例如 mysql-exporter 端口为 9104

(2) 将该服务器注册到 consul 的 server 中, 在装置 consul 的服务器执行命令

curl -X PUT -d '{"id":"test-exporter","name":"test-exporter","address":"ip","port": 9104,"tags": ["jvm-exporter"],"checks": [{"http":"http://ip:9104/metrics","interval":"5s"}]}'  http://ip:8500/v1/agent/service/register
 
name:consul 的 service 注册名称
id:consul 的实例名称
address: 监控地址 ip
port: 监控的端口号
tags: 标签名
checks: 查看的节点的门路

(3) 在 prometheus 中配置服务主动发现,去读取 consul 中配置的服务器,只有 consul 有新的服务注册,prometheus 也会更新节点,在 prometheus.yml 中批改为

- job_name: 'consul-node-exporter'
    consul_sd_configs:
    - server: 'ip:8500' #consul 的地址

(4) 配置不同分类的 job,对增加的标签进行正则表达式过滤,合乎则增加到相应 job 分类

如上配置的毛病是,所有的注册节点都会归类到 consul-node-exporter 这个工作类别上面,有时候想对注册的服务进行分类,比方 mysql 为一类,oracle 为一类,就要批改配置为:

- job_name: 'consul-node-exporter'
    consul_sd_configs:
    - server: 'ip:8500'
    relabel_configs: #通过标签设置
      - source_labels: [__meta_consul_tags] #源标签
        regex: .*node.*  #正则表达式,只有__meta_consul_tags 蕴含 node 就归为这个类别下
        action: keep #抛弃 source_labels 的值中没有匹配到正则表达式内容的 Target 实例,用 drop 则相同

以上配置表名对标签进行顾虑,符合要求的会布局为该类,不合乎则抛弃。

(5) 如果要在 consul 删除服务,解绑服务,应用命令:

curl -v -X PUT http://ip:8500/v1/agent/service/deregister/ 服务名称

到这里,咱们就能够通过 consul 注册服务并且同步归类到 prometheus 页面了,在
ip:9090/targets 能够查看

三、confd 装置应用

接下来就该配置告警了
confd 流程:以键值对模式注册规定到 consul–写好须要的模板文件–配置 confd 生成规定配置文件 yml–prometheus 读取规定配置文件
而后,只有将规定通过接口或者其它形式注册到 consul,confd 通过配置读取 consul 中的值并依据模板生成文件 (stage_file), 生成的文件和 prometheus 的配置文件(dest_file) 不一样时,便会同步文件实现动静刷新配置(如果是第一次读取会将 stage_file 作为配置文件到相应目录)。

1. 装置 confd

# Download the binary
wget https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64
# 重命名二进制文件,并挪动到 PATH 的目录下
mv confd-0.16.0-linux-amd64 / 本人的 prometheus 目录 /confd
chmod +x / 本人的 prometheus 目录 /confd
# 验证是否装置胜利
confd --help

2. 创立配置文件目录

在 prometheus 目录(上方放 confd 的 prometheus 目录)下创立

mkdir  conf.d
mkdir templates
mkdir rules

当前目录应如图所示,便于前面配置以及启动命令的文件门路配置,所有的文件夹和启动二进制程序应在 prometheus(自定义)目录下

conf.d:confd 的配置文件,次要蕴含配置的生成逻辑,例如模板源,后端存储对应的 keys,命令执行等。
templates: 配置模板 Template,即基于不同组件的配置,批改为合乎 Golang text templates 的模板文件。
模板源配置文件是 TOML 格局的文件,次要蕴含配置的生成逻辑,例如模板源,后端存储对应的 keys,命令执行等。默认目录在 conf.d,在这里次要是要配置告警规定,所以咱们配置 prometheus 告警规定模板文件。

(1) 用键值对模式注册告警到 consul(这里咱们采纳的 json 格局,因为前期提取生成文件也用 json, 也能够用 yml 配置文件)

通过接口向 consul 注册规定键值对(用命令行其实也能够,不过其中有单引号目前还没找到办法本义)

{
    "alert": "节点状态",
    "expr": "up{job='consul-node-exporter'} == 0",
    "for": "15s",
    "labels": {
        "severity": "1",
        "team": "node"
    },
    "annotations": {"summary": "'{{ $labels.instance}} 节点已进行运行'",
        "description": "'{{$labels.instance}} 检测到异样进行, 请重点关注'"
    }
}

查看 consul 服务中心:

(2) 配置模板文件 templates/my.conf.tmpl

groups:
- name: node-alerts
  rules:{{range gets "/prometheus/rules/*"}}{{$data := json .Value}} #循环获取 consul 中 /prometheus/rules/ 键上面所有的值,因为告警规定不止一个,所以采纳循环形式,最初用 json 获取
  - alert: {{$data.alert}}
    expr: {{$data.expr}}
    for: {{$data.for}}
    labels:
      severity: {{$data.labels.severity}}
    annotations:
      summary: {{$data.annotations.summary}}
      description: {{$data.annotations.description}}
{{end}}

(3) 配置 conf.d/config.toml,读取模板文件,生成配置文件,vi conf.d/config.toml

[template]
src = "/my.conf.tmpl"  #模板文件门路
dest = "/ 本人目录 /prometheus/rules/node_alerts.yml" #生成的指标文件门路
keys = ["/prometheus/rules",]
check_cmd = "touch /tmp/check" 
reload_cmd = "docker restart 382b64f17a28" #重启 prometheus,此处用的容器,如用命令,批改为命令

(4) 在 prometheus 目录下启动 confd

nohup ./confd -interval 20 -log-level info -confdir ./ -config-file conf.d/config.toml -backend consul -node ip(consul 的 ip):8500 &

以上整个流程就完结了,规定配置文件曾经生成在相应目录(/ 本人目录 /prometheus/rules/node_alerts.yml),此时还须要在 prometheus 中配置读取规定配置文件,增加:

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  - "/ 本人目录 /prometheus/rules/*.yml"  #读取该目录下所有 yml 配置文件

最初重启 prometheus,整个配置就实现了,通过 consul 能够动静增加服务和更新规定配置文件。(以上在 consul 中注册多个 key/value 规定只会生成一个 yml 规定文件)

正文完
 0