文章目錄
- 前言
- 1、Prometheus介紹
-
- 1.1、Prometheus元件說明
- 1.2、Prometheus的特點
- 1.3、Prometheus的架構
- 1.4、Prometheus的使用場景
- 2、安裝Prometheus
-
- 1.下載下傳
- 2.安裝Prometheus
- 3.注冊系統服務
- 4.啟動服務
- 3、配置Prometheus
-
- 3.1配置總體說明
- 3.2global配置
- 3.3 alerting告警配置
- 3.4 rule_files規則檔案配置
- 3.5 scrape_configs拉取配置
- 3.6 remote_read/remote_write遠端讀寫配置
- 3.7 修改prometheus.yml
前言
前面章節的Prometheus是用Docker安裝的,需要依賴Docker且不易于持久化。下面介紹手動安裝。
1、Prometheus介紹
Prometheus是最初在SoundCloud上建構的開源系統監視和警報工具包。自2012年成立以來,許多公司群組織都采用了Prometheus,該項目擁有非常活躍的開發人員和使用者社群。Prometheus 于2016年加入了 Cloud Native Computing Foundation,這是繼Kubernetes之後的第二個托管項目。
官網:https://prometheus.io
最新版本:
2.32.1
1.1、Prometheus元件說明
元件 | 元件說明 |
---|---|
Prometheus | 開源的系統監控和報警架構,靈感源自Google的Borgmon監控系統 |
AlertManager | 處理由用戶端應用程式(如Prometheus server)發送的警報。它負責将重複資料删除,分組和路由到正确的接收者內建,還負責沉默和抑制警報 |
Node_Exporter | 用來監控各節點的資源資訊的exporter,應部署到prometheus監控的所有節點 |
PushGateway | 推送網關,用于接收各節點推送的資料并暴露給Prometheus server |
Exporter是一個采集監控資料并通過Prometheus監控規範對外提供資料的元件,能為Prometheus提供監控的接口。
Exporter将監控資料采集的端點通過HTTP服務的形式暴露給Prometheus Server,Prometheus Server通過通路該Exporter提供的Endpoint端點,即可擷取到需要采集的監控資料。不同的Exporter負責不同的業務。
1.2、Prometheus的特點
- 多元的資料模型(基于時間序列的Key、Value鍵值對)
- 靈活的查詢和聚合語言PromQL
- 提供本地存儲和分布式存儲
- 通過基于HTTP的Pull模型采集時間序列資料
- 可利用Pushgateway(Prometheus的可選中間件)實作Push模式
- 可通過動态服務發現或靜态配置發現目标機器
- 支援多種圖表和資料大盤
1.3、Prometheus的架構
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYTMfhHLlN3XnxCM38FdsYkRGZkRG9lcvx2bjxCNx8VZ6l2cs0TP35WVlVHc1MVZwxUatg2VhVGTjFWW1I0Q2UTQClGVF5UMR9Fd4VGdsATNfd3bkFGazxSUhxGatJGbwhFT1Y0Mk9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLzQWNyMDO3UWM0ADO4YjY5IzNyQjMwMDZyM2Y1QWMhJ2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
1.4、Prometheus的使用場景
prometheus非常适合記錄任何純數字時間序列。它既适合以機器為中心的監視,也适合監視高度動态的面向服務的體系結構。在微服務世界中,它對多元資料收集和查詢的支援是一種特别的優勢。
prometheus的設計旨在提高可靠性,使其成為中斷期間要使用的系統,進而使您能夠快速診斷問題。每個prometheus伺服器都是獨立的,而不依賴于網絡存儲或其他遠端服務,當基礎設施部分出現問題時仍然可以使用它。
2、安裝Prometheus
目前安裝版本為
2.32.1
。
1.下載下傳
下載下傳位址:https://prometheus.io/download/
wget https://github.com/prometheus/prometheus/releases/download/v2.32.1/prometheus-2.32.1.linux-amd64.tar.gz
2.安裝Prometheus
tar zxvf prometheus-2.32.1.linux-amd64.tar.gz
mv prometheus-2.32.1.linux-amd64 /usr/local/Prometheus
mkdir -p /data/Prometheus
3.注冊系統服務
vi /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus
After=network.target
[Service]
Type=simple
Environment="GOMAXPROCS=4"
#User=prometheus
#Group=prometheus
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/data/prometheus \
--storage.tsdb.retention=30d \
--web.console.libraries=/usr/local/prometheus/console_libraries \
--web.console.templates=/usr/local/prometheus/consoles \
--web.listen-address=0.0.0.0:9090 \
--web.read-timeout=5m \
--web.max-connections=10 \
--query.max-concurrency=20 \
--query.timeout=2m \
--web.enable-lifecycle
PrivateTmp=true
PrivateDevices=true
ProtectHome=true
NoNewPrivileges=true
LimitNOFILE=infinity
ReadWriteDirectories=/data/prometheus
ProtectSystem=full
SyslogIdentifier=prometheus
Restart=always
[Install]
WantedBy=multi-user.target
4.啟動服務
systemctl daemon-reload
systemctl enable prometheus
systemctl start prometheus
通路http://172.16.10.171:9090
這樣就啟動成功了
3、配置Prometheus
3.1配置總體說明
配置檔案在
/usr/local/prometheus/prometheus.yml
,主要分以下幾個配置子產品:
global | 全局配置 |
---|---|
alerting | 告警配置 |
rule_files | 規則檔案配置 |
scrape_configs | 拉取配置 |
remote_read、remote_write | 遠端讀寫配置 |
3.2global配置
global指定在所有其他配置上下文中有效的參數。還可用作其他配置部分的預設設定。
global:
# 預設拉取頻率
[ scrape_interval: <duration> | default = 1m ]
# 拉取逾時時間
[ scrape_timeout: <duration> | default = 10s ]
# 執行規則頻率
[ evaluation_interval: <duration> | default = 1m ]
# 通信時添加到任何時間序列或告警的标簽
# external systems (federation, remote storage, Alertmanager).
external_labels:
[ <labelname>: <labelvalue> ... ]
# 記錄PromQL查詢的日志檔案
[ query_log_file: <string> ]
3.3 alerting告警配置
alerting指定與Alertmanager相關的設定。
alerting:
alert_relabel_configs:
[ - <relabel_config> ... ]
alertmanagers:
[ - <alertmanager_config> ... ]
3.4 rule_files規則檔案配置
rule_files指定prometheus加載的任何規則的位置,從所有比對的檔案中讀取規則和告警。
rule_files:
[ - <filepath_glob> ... ]
3.5 scrape_configs拉取配置
scrape_configs指定prometheus監控哪些資源。預設會拉取prometheus本身的時間序列資料,通過http://ip:9090/metrics進行拉取。
一個scrape_config指定一組目标和參數,描述如何拉取它們。在一般情況下,一個拉取配置指定一個作業。在進階配置中,這可能會改變。
可以通過static_configs參數靜态配置目标,也可以使用支援的服務發現機制之一動态發現目标。
此外,relabel_configs在拉取之前,可以對任何目标及其标簽進行修改。
scrape_configs:
job_name: <job_name>
# 拉取頻率
[ scrape_interval: <duration> | default = <global_config.scrape_interval> ]
# 拉取逾時時間
[ scrape_timeout: <duration> | default = <global_config.scrape_timeout> ]
# 拉取的http路徑
[ metrics_path: <path> | default = /metrics ]
# honor_labels 控制prometheus處理已存在于收集資料中的标簽與prometheus将附加在伺服器端的标簽("作業"和"執行個體"标簽、手動配置的目标标簽和由服務發現實作生成的标簽)之間的沖突
# 如果 honor_labels 設定為 "true",則通過保持從拉取資料獲得的标簽值并忽略沖突的伺服器端标簽來解決标簽沖突
# 如果 honor_labels 設定為 "false",則通過将拉取資料中沖突的标簽重命名為"exported_<original-label>"來解決标簽沖突(例如"exported_instance"、"exported_job"),然後附加伺服器端标簽
# 注意,任何全局配置的 "external_labels"都不受此設定的影響。在與外部系統的通信中,隻有當時間序列還沒有給定的标簽時,它們才被應用,否則就會被忽略
[ honor_labels: <boolean> | default = false ]
# honor_timestamps 控制prometheus是否遵守拉取資料中的時間戳
# 如果 honor_timestamps 設定為 "true",将使用目标公開的metrics的時間戳
# 如果 honor_timestamps 設定為 "false",目标公開的metrics的時間戳将被忽略
[ honor_timestamps: <boolean> | default = true ]
# 配置用于請求的協定
[ scheme: <scheme> | default = http ]
# 可選的http url參數
params:
[ <string>: [<string>, ...] ]
# 在每個拉取請求上配置 username 和 password 來設定 Authorization 頭部,password 和 password_file 二選一
basic_auth:
[ username: <string> ]
[ password: <secret> ]
[ password_file: <string> ]
# 在每個拉取請求上配置 bearer token 來設定 Authorization 頭部,bearer_token 和 bearer_token_file 二選一
[ bearer_token: <secret> ]
# 在每個拉取請求上配置 bearer_token_file 來設定 Authorization 頭部,bearer_token_file 和 bearer_token 二選一
[ bearer_token_file: /path/to/bearer/token/file ]
# 配置拉取請求的TLS設定
tls_config:
[ <tls_config> ]
# 可選的代理URL
[ proxy_url: <string> ]
# Azure服務發現配置清單
azure_sd_configs:
[ - <azure_sd_config> ... ]
# Consul服務發現配置清單
consul_sd_configs:
[ - <consul_sd_config> ... ]
# DNS服務發現配置清單
dns_sd_configs:
[ - <dns_sd_config> ... ]
# EC2服務發現配置清單
ec2_sd_configs:
[ - <ec2_sd_config> ... ]
# OpenStack服務發現配置清單
openstack_sd_configs:
[ - <openstack_sd_config> ... ]
# file服務發現配置清單
file_sd_configs:
[ - <file_sd_config> ... ]
# GCE服務發現配置清單
gce_sd_configs:
[ - <gce_sd_config> ... ]
# Kubernetes服務發現配置清單
kubernetes_sd_configs:
[ - <kubernetes_sd_config> ... ]
# Marathon服務發現配置清單
marathon_sd_configs:
[ - <marathon_sd_config> ... ]
# AirBnB's Nerve服務發現配置清單
nerve_sd_configs:
[ - <nerve_sd_config> ... ]
# Zookeeper Serverset服務發現配置清單
serverset_sd_configs:
[ - <serverset_sd_config> ... ]
# Triton服務發現配置清單
triton_sd_configs:
[ - <triton_sd_config> ... ]
# 靜态配置目标清單
static_configs:
[ - <static_config> ... ]
# 目标relabel配置清單
relabel_configs:
[ - <relabel_config> ... ]
# metric relabel配置清單
metric_relabel_configs:
[ - <relabel_config> ... ]
# 每次拉取樣品的數量限制
# metric relabelling之後,如果有超過這個數量的樣品,整個拉取将被視為失效。0表示沒有限制
[ sample_limit: <int> | default = 0 ]
3.6 remote_read/remote_write遠端讀寫配置
remote_read/remote_write将資料源與prometheus分離。
# 與遠端寫功能相關的設定
remote_write:
[ - <remote_write> ... ]
# 與遠端讀功能相關的設定
remote_read:
[ - <remote_read> ... ]
3.7 修改prometheus.yml
将前幾章部署的springboot、mysql、nginx的exporter配置資訊,配置到prometheus.yml中。
vi /usr/local/prometheus/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"]
- job_name: "spring"
scrape_interval: 15s
scrape_timeout: 10s
metrics_path: "/demo/actuator/prometheus"
static_configs:
- targets: ["192.168.100.88:7901"]
- job_name: "mysql8.x"
static_configs:
- targets: ["172.16.10.169:9104", "192.168.100.88:9104"]
- job_name: "nginx"
static_configs:
- targets: ["172.16.10.171:9913"]
修改完後重新開機下prometheus
通路prometheus的targets: http://172.16.10.171:9090/targets
發現除了自身的prometheus,一個springboot項目,兩個mysql,一個nginx,都接入進來了。