概述
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=` 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 中的配置詳解
: 布爾值,true 或 false : 持續時間,格式符合正則表達式 [0-9]+(ms|[smhdwy]) : 標籤名,格式符合正則表達式 [a-zA-Z_][a-zA-Z0-9_]* : 標籤值,可以包含任意 unicode 字符 : 文件名,任意有效的文件路徑 : 主機,可以是主機名或 IP,後面可跟埠號 : URL 路徑 : 協議,http 或 https : 字符串 : 密鑰,比如密碼 : 模板字符串,裡面包含需要展開的變量
4、啟動prometheus
./prometheus
5、測試訪問
訪問地址:伺服器IP:9090,點擊Status-->targets 跳轉到監控目標,紅框的表示部署的prometheus
覺得有用的朋友多幫忙轉發哦!後面會分享更多devops和DBA方面的內容,感興趣的朋友可以關注下~