概述
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/

2、配置環境變量
echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile source /etc/profile

3、測試
驗證一下是否成功,用go version 來驗證
# go version

二、在監控服務器安裝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


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

