概述
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等系统,还提供有源码,可编译安装。
下载地址:
1、解压
# tar -xvf go1.13.linux-amd64.tar.gz -C /usr/local/
2、配置环境变量
echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile source /etc/profile
3、测试
验证一下是否成功,用go version 来验证
# go version
二、在监控服务器安装prometheus
1、开始安装prometheus
去官网下载对应系统的版本:
下载地址:
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
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
5、测试访问
访问地址:服务器IP:9090,点击Status–>targets 跳转到监控目标,红框的表示部署的prometheus
觉得有用的朋友多帮忙转发哦!后面会分享更多 devops 和DBA方面的内容,感兴趣的朋友可以关注下~