一、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:监控地址ipport:监控的端口号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 binarywget 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目录/confdchmod +x /本人的prometheus目录/confd# 验证是否装置胜利confd --help
2. 创立配置文件目录
在prometheus目录(上方放confd的prometheus目录)下创立
mkdir conf.dmkdir templatesmkdir 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规定文件)