文章目录
0.系列目录
服务器监控-prometheus使用(1):简介篇
服务器监控-prometheus使用(2):promethues搭建篇
服务器监控-prometheus使用(3):Grafana搭建篇
服务器监控-prometheus使用(4):收集器搭建篇
服务器监控-prometheus使用(5):告警器搭建篇
1.前言
本文开始介绍如何在windows上搭建整套prometheus服务。当然,对于收集器exporter是根据不同环境来搭建的。系列的第四篇章会单独进行叙述,此篇章不做赘述。
2.搭建
2.1 windows搭建
2.1.1 下载地址
https://prometheus.io/download/
进入页面后,可以看到如下图所示,可根据需求进行下载:
windows的话,下载zip格式的。
2.1.2 启动运行
下载后解压,结果如图所示:
此处最重要的是prometheus.yml文件(具体配置见3.配置),当配置完prometheus.yml后,即可双击prometheus.exe启动。启动后会弹出黑窗口。访问“ip地址:9090”端口,就可访问到prometheus的页面。
出现以下页面,则配置、启动成功。
2.2 linux搭建
2.2.1 下载地址
2.2.2 启动运行
linux版本,下载tar后,进行解压,解压后的结果如图所示
使用如下命令启动即可,其中&符号为挂在后台使用。其中–config.file为读取到对应的prometheus的yml配置文件。
./prometheus --web.enable-lifecycle --config.file=prometheus.yml &
启动成功后,可以通过ip+端口号访问地址,prometheus默认的端口号为9090。出现如下页面,则部署、启动成功。
3.配置
-- 官方配置文档
https://prometheus.io/docs/prometheus/latest/getting_started/
对于普罗米修斯,最重要的就是prometheus.yml和rules.yml文件。
3.1 prometheus.yml配置
当你解压后,文件中自带的prometheus.yml配置文件如下所示:
这里最基础的,主要分了四大块:
global: 整体参数配置
alerting: 告警通知配置(需要配合alertmanager的服务进行使用。)
rule_files: 规则文件配置
scrape_configs: 监控节点配置。(当然节点配置有很多中类型)
当然还有其他很多的配置,如图所示:
因配置太多,俺也没研究的太彻底,就不一一介绍,写到哪算哪。
3.1.1 global:全局配置变量
global:
# How frequently to scrape targets by default.
# 抓取收集的默认时间间隔频率。
[ scrape_interval: <duration> | default = 1m ]
# How long until a scrape request times out.
# 抓取的时候最长超时时间。估计是为了防止因为网络或者其他异常,导致prometheus一直在向采集器请求数据导致网络一直占用的问题,默认应该就行。
[ scrape_timeout: <duration> | default = 10s ]
# How frequently to evaluate rules.
# 扫描规则的时间间隔
[ evaluation_interval: <duration> | default = 1m ]
# The labels to add to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
# 外部系统的一些数据集合,基础不用配置
external_labels:
[ <labelname>: <labelvalue> ... ]
# File to which PromQL queries are logged.
# Reloading the configuration will reopen the file.
[ query_log_file: <string> ]
3.1.2 alert:告警配置
这个是配置告警服务的服务地址,如果没有服务,则不用配置。告警模块是当你配置了某些规则,比如服务器cpn超过90,然后普罗米修斯会进行告警,普罗米修斯可以通过告警模块发送给告警服务,从而发送给邮件、钉钉等方式通知运维人员。
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
- 127.0.0.1:9093
3.1.3 rule_files:规则文件配置
配置普罗米修斯的规则文件的文件位置,比如:
rule_files:
- "first_rules.yml"
也可以使用*号做匹配,比如:
rule_files:
- "rules/*_rules.yml"
3.1.4 scrape_configs:节点配置
相当于去截取各待采集的服务的基础配置。此配置方式有很多中形式,比如在yml里面配置,比如使用json,比如使用接口读取。此处简单介绍几种。
3.1.4.1 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"]
3.1.4.2 yml外关联文件的方式配置
比如在job_name中,增加file_sd_configs,如下所示:
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']
- job_name: 'windows'
file_sd_configs:
- files: ['sd_config/windows.yml']
refresh_interval: 60s
对应的windows.yml的配置为:
- targets:
- 192.168.163.xx1:9277
labels:
service: windows
group: 测试服务器组
system: 测试服务器1
- targets:
- 192.168.163.xx2:9277
labels:
service: windows
group: 测试服务器组
system: 测试服务器2
3.1.4.3 通过接口调用的方式获取
但是当前接口,必须按照规定的格式进行返回json或yml。会面会专门开一章,对接口调用方式获取来进行详细的讲解。
scrape_configs:
- job_name: "prometheus"
http_sd_configs:
- url: "http://localhost:8080/prom/config"
basic_auth:
username: admin
password: 123456
3.1.4.4 重加载
当重新修改配置,又没到global里面配置的时间,可以调用一下参数来进行加载:
http://普罗米修斯的ip:9090/-/reload
3.1.5 其他
当然还有很多配置,此处没有一一列举,比如oauth2的配置,tls的配置,docker的配置。如有时间,俺会再一一研究,提供配置思路方式。