一文看懂开源监控神器--prometheus部署教程,值得收藏
概述
Prometheus支持多种语言(Go,JAVA,Python,ruby官方提供客户端,其他语言有第三方开源客户端)。我们可以通过客户端方面的对核心业务进行埋点。
Prometheus的基本原理是通过HTTP协议周期性抓取被监控组件的状态,任意组件只要提供对应的HTTP接口就可以接入监控。不需要任何SDK或者其他的集成过程。这样做非常适合做虚拟化环境监控系统,比如VM、Docker、Kubernetes等。输出被监控组件信息的HTTP接口被叫做exporter 。
部署思路:
1、安装Go语言环境
2、在监控服务器上安装prometheus
3、在被监控环境上安装export
4、安装grafana
5、安装alertmanager
以下基于centos7系统进行演示。
一、安装go语言环境
由于Prometheus 是用golang开发的,所以首先安装一个go环境,Go语言是跨平台,支持windows、linux、mac OS X等系统,还提供有源码,可编译安装。
下载地址:https://studygolang.com/dl
1、解压
# tar -xvf go1.13.linux-amd64.tar.gz -C /usr/local/
![一文看懂开源监控神器--prometheus部署教程,值得收藏](http://www.isolves.com/d/file/p/2019/09-24/d24b2d7545400ea70e6ae2e184edd988.jpg)
2、配置环境变量
echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile source /etc/profile
![一文看懂开源监控神器--prometheus部署教程,值得收藏](http://www.isolves.com/d/file/p/2019/09-24/10b98ae7267f572520fe65361b8b686a.jpg)
3、测试
验证一下是否成功,用go version 来验证
# go version
![一文看懂开源监控神器--prometheus部署教程,值得收藏](http://www.isolves.com/d/file/p/2019/09-24/705d5d4202a89842481a8b6178fa9327.jpg)
二、在监控服务器安装prometheus
1、开始安装prometheus
去官网下载对应系统的版本:https://prometheus.io/download/
下载地址:https://github.com/prometheus/prometheus/releases/download/v2.12.0/prometheus-2.12.0.linux-amd64.tar.gz
![一文看懂开源监控神器--prometheus部署教程,值得收藏](http://www.isolves.com/d/file/p/2019/09-24/1513f7e628cf0061cc9e4253c9ab0e83.jpg)
![一文看懂开源监控神器--prometheus部署教程,值得收藏](http://www.isolves.com/d/file/p/2019/09-24/e10abd22356dce2b7b41c2d2900e1244.jpg)
2、上传到监控服务器并解压
# tar -xvf prometheus-2.12.0.linux-amd64.tar.gz -C /usr/local/ # ln -sv /usr/local/prometheus-2.12.0.linux-amd64/ /usr/local/Prometheus
![一文看懂开源监控神器--prometheus部署教程,值得收藏](http://www.isolves.com/d/file/p/2019/09-24/db393e89a7ce2c96d006f4e4b55c63ab.jpg)
3、监控端配置文件
prometheus.yml默认配置如下:
# 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']
prometheus.yml 中的配置详解
- <boolean>: 布尔值,true 或 false
- <duration>: 持续时间,格式符合正则表达式 [0-9]+(ms|[smhdwy])
- <labelname>: 标签名,格式符合正则表达式 [a-zA-Z_][a-zA-Z0-9_]*
- <labelvalue>: 标签值,可以包含任意 unicode 字符
- <filename>: 文件名,任意有效的文件路径
- <host>: 主机,可以是主机名或 IP,后面可跟端口号
- <path>: URL 路径
- <scheme>: 协议,http 或 https
- <string>: 字符串
- <secret>: 密钥,比如密码
- <tmpl_string>: 模板字符串,里面包含需要展开的变量
4、启动prometheus
./prometheus
![一文看懂开源监控神器--prometheus部署教程,值得收藏](http://www.isolves.com/d/file/p/2019/09-24/d4631e87056f0c784d669f8e2f10ac41.jpg)
5、测试访问
访问地址:服务器IP:9090,点击Status-->targets 跳转到监控目标,红框的表示部署的prometheus
![一文看懂开源监控神器--prometheus部署教程,值得收藏](http://www.isolves.com/d/file/p/2019/09-24/c1fd5f92688582c2ee312f871c2d8039.jpg)
![一文看懂开源监控神器--prometheus部署教程,值得收藏](http://www.isolves.com/d/file/p/2019/09-24/7e1dfff4660ac813510093e41235344e.jpg)