第一步:需求分析
根据业务需求,原Psutil采集工具不满足实际工作需求,决定更换采集工具。 听朋友介绍就决定选择使用 Prometheus+Grafana 来搭建性能测试使用的监控平台,而且经过上级领导同意,效果满意,则扩大到全环境监控。
首先我们需要了解一下
Prometheus 是什么?
Prometheus是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的。随着发展,越来越多公司和组织接受采用Prometheus,社区也十分活跃,他们便将它独立成开源项目,并且有公司来运作。google SRE的书内也曾提到跟他们BorgMon监控系统相似的实现是Prometheus。现在最常见的Kubernetes容器管理系统中,通常会搭配Prometheus进行监控。
Prometheus 的优点
- 非常少的外部依赖,安装使用超简单
- 已经有非常多的系统集成 例如:docker HAProxy Nginx JMX等等
- 服务自动化发现
- 直接集成到代码
- 设计思想是按照分布式、微服务架构来实现的
Prometheus 的特性
- 自定义多维度的数据模型
- 非常高效的存储 平均一个采样数据占 ~3.5 bytes左右,320万的时间序列,每30秒采样,保持60天,消耗磁盘大概228G。
- 强大的查询语句
- 轻松实现数据可视化
等等
相对于Graphite这种产品,还是有不少优点的。最让我觉得不错的是非常优秀的写性能和读取性能,它数据结构实现和OpenTSDB是有相似之处,有兴趣可以看看这个文档。解密OpenTSDB的表存储优,这里只简单描述一下Prometheus的特性,感兴趣的同学可以自己百科。
Prometheus 的系统架构
第二步:技术设计
由于我们公司是spring 后端,集成多个微服务,所以在进行性能测试过程中,需要监控所有微服务的资源使用情况,来定位具体性能瓶颈。另外由于是多多节点多服务,所以在现在性能监控平台的时候,需要选择一款部署块,监控数据多,而且需要指出跨平台以及支持快速扩充的一款产品,因此就决定使用Prometheus来做这个事情。
首先我们需要技术设计,来决定具体实现效果,请参考下图。
![](https://img-blog.csdnimg.cn/20200616201229300.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM1NDUxOTM5,size_16,color_FFFFFF,t_70)
第三步:环境准备
首先每台linux 机器均需要安装 golang ,Prometheus基于go语言来进行数据统计的。
其次分别安装上图所描述的工具,另外如果是多节点监控,只需要一台机器安装grafana及Prometheus 就可以了,采集节点需要安装 node_exporter 、process-exporter即可。
一般都是从官网下载二进制压缩包,自行解压完成。
https://prometheus.io/download/
https://github.com/ncabatoff/process-exporter/releases
下载后 使用rz 命令上传至linux 使用解压命令 进行解压。
首先是 prometheus 然后是 node_exporter 最后是 *.exporter:
tar -zxvf prometheus-2.7.1.linux-amd64.tar.gz
tar -zxvf node_exporter-0.18.0.linux-amd64.tar.gz
tar -zxvf process-exporter-0.4.0.linux-amd64.tar.gz
node_exporter
首先启动node_exporter,node_exporter解压完成后不需要任何操作,可以直接启动。
建议使用 nohup 方式启动。
nohup ./node_exporter >/dev/null &
验证node_exporter : 浏览器输入 http://ip:9100/metrics,如果打开如下图则证明启动成功。
process_exporter
然后是process_exporter,进入process_exporter目录文件,设置配置文件,process_exporter解压完后默认是没有配置文件的,需要我们自己写一个,启动时通过-config.path 指定配置文件。读配置文件的本质就是,读里面配置的服务名,通过服务名称去进行pid的查找和分析,以下为范例<这里使用{{.Matches}}模板>匹配 指定系统微服务 :如下
$ vim process.yaml
process_names:
- name: "{{.Matches}}"
cmdline:
- '*.jar' #需要监控的进程名称
- name: "{{.Matches}}"
cmdline:
- '*.jar' #需要监控的进程名称
启动方式:nohup ./process-exporter -config.path process.yaml >/dev/null &
验证process_exporter: 浏览器输入 http://ip:9256/metrics ,如果打开如下图则证明启动成功。
prometheus
进行Prometheus配置,以下是基本的配置,完整文档介绍可以看 > 官方文档:https://prometheus.io/docs/prometheus/2.13/configuration/configuration/
# my global config
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).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
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']
如果后面配置了exporter组件,可以在scrape_configs里添加job,例如:
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'] #博主的prometheus 安装在本地所以直接用localhost
- job_name: 'node' #脚本名称,随意看心情,自己能认识就行
static_configs:
- targets: ['localhost:9100']
- job_name: 'process'
static_configs:
- targets: ['localhost:9256']
- job_name: 'process-150'
static_configs:
- targets: ['192.168.64.150:9256']
- job_name: 'node-150'
static_configs:
- targets: ['192.168.64.150:9100']
- job_name: 'process-152'
static_configs:
- targets: ['192.168.64.152:9256']
- job_name: 'node-152'
static_configs:
启动:
# 默认情况下,Prometheus会将其数据库存储在./data (flag—storage.tsdb.path)中
$ cd prometheus-2.13.1.linux-amd64/
$ nohup ./prometheus --config.file=prometheus.yml >/dev/null &
验证:可以通过http://ip:9090访问浏览器,或者http://ip:9090/metrics看是否能提供关于自身的各项指标
至此前期环境准备已经完成,下一篇将介绍如何进行Prometheus和Grafana 集成。
技术交流群:1045153571