prometheus-2.26.0.linux-amd64.tar.gz
下載地址:https://github.com/prometheus/prometheus/releases/download/v2.26.0/prometheus-2.26.0.linux-amd64.tar.gz
prometheus-2.54.1.linux-amd64.tar.gz
下載地址:https://github.com/prometheus/prometheus/releases/download/v2.26.0/prometheus-2.26.0.linux-amd64.tar.gz
CentOS 7.9
下載並執行Prometheus
#
wget https://github.com/prometheus/prometheus/releases/download/v2.26.0/prometheus-2.26.0.linux-amd64.tar.gz
#
tar xvzf prometheus-2.26.0.linux-amd64.tar.gz
# cd
prometheus-2.26.0.linux-amd64
# ls
console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool
配置Prometheus自身監控
prometheus.yml
prometheus.yml
global:
scrape_interval:15s# 預設,每15秒取樣一次目標
# 與其它外部系統(比如federation, remote storage, Alertmanager)互動時,會附加這些標籤到時序資料或者報警
external_labels:
monitor:'codelab-monitor'
# 一份取樣配置僅包含一個 endpoint 來做取樣
# 下面是 Prometheus 本身的endpoint:
scrape_configs:
# job_name 將被被當作一個標籤 `job=<job_name>`新增到該配置的任意時序取樣.
-job_name:'prometheus'
# 覆蓋全域性預設值,從該job每5秒對目標取樣一次
scrape_interval:5s
static_configs:
# 如果需要遠端訪問, localhost 也可以替換為具體IP,比如10.118.71.170
-targets:
[
'localhost:9090'
]
啟動Prometheus
#
啟動 Prometheus.
#
預設地, Prometheus 在 ./data 路徑下儲存其資料庫 (flag --storage.tsdb.path).
#
./prometheus --config.file=prometheus.yml
localhost:9000
來瀏覽狀態頁。等待幾秒讓他從自己的 HTTP metric endpoint 來收集資料。開放防火牆埠
#
firewall-cmd --permanent --zone=public --add-port=9090/tcp
success
#
firewall-cmd --reload
success
使用expressin browser
localhost:9090/graph
,選擇 Graph
導航選單下的 Table
tab頁 (Classic UI
下為Console
tab頁)。localhost:9090/metrics
頁面內容可知,Prometheus 匯出了關於其自身的一個名為 prometheus_target_interval_length_seconds
指標(目標取樣之間的實際間隔)。將其作為搜尋表示式,輸入到表示式搜尋框中,點選 Execute
按鈕,如下,將返回多個不同的時間序列(以及每個時間序列的最新值),所有時間序列的 metric 名稱均為 prometheus_target_interval_length_seconds
,但具有不同的標籤。這些標籤具有不同的延遲百分比
和目標組間隔(target group intervals)
。
prometheus_target_interval_length_seconds
{quantile
="0.99"
}
count
(prometheus_target_interval_length_seconds)
使用繪圖介面
rate
(prometheus_tsdb_head_chunks_created_total[
1
m])

啟動一些取樣目標
Node Exporter
作為取樣目標,多關於它的使用請查閱#
wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
#
tar -xvzf node_exporter-1.1.2.linux-amd64.tar.gz
#
./node_exporter --web.listen-address 127.0.0.1:8001
#
./node_exporter --web.listen-address 127.0.0.1:8002
#
./node_exporter --web.listen-address 127.0.0.1:8003
http://localhost:8080/metrics, http://localhost:8081/metrics 和http://localhost:8082/metrics
的示例目標配置 Prometheus 來監控示例目標
group=“ production”
標籤新增到第一個目標組,同時將 group=“ canary”
新增到第二個目標。prometheus.yml
中的 scrape_configs
部分,然後重新啟動 Prometheus 例項。修改後的 prometheus.yml
內容如下global:
scrape_interval:15s# 預設,每15秒取樣一次目標
# 與其它外部系統(比如federation, remote storage, Alertmanager)互動時,會附加這些標籤到時序資料或者報警
external_labels:
monitor:'codelab-monitor'
# 一份取樣配置僅包含一個 endpoint 來做取樣
# 下面是 Prometheus 本身的endpoint:
scrape_configs:
# job_name 將被被當作一個標籤 `job=<job_name>`新增到該配置的任意時序取樣.
-job_name:'prometheus'
# 覆蓋全域性預設值,從該job每5秒對目標取樣一次
scrape_interval:5s
static_configs:
-targets:
[
'10.118.71.170:9090'
]
-job_name:'node'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval:5s
static_configs:
-targets:
[
'localhost:8001'
,
'localhost:8002'
]
labels:
group:'production'
-targets:
[
'localhost:8003'
]
labels:
group:'canary'
Targets
(Status
-> Targets
)
Graph
查詢
配置規則以將取樣的資料聚合到新的時間序列
node_cpu_seconds_total
,保留 Job,instance,和mode 維度))。我們可以這樣寫:
avg
by
(
job, instance,
mode) (rate(node_cpu_seconds_total[
5
m]))
Graph
中執行查詢,結果如下
job_instance_mode:node_cpu_seconds:avg_rate5m
的新指標,使用以下記錄規則建立檔案並將其儲存 prometheus.rules.yml
groups:
-name:cpu-node
rules:
-record:job_instance_mode:node_cpu_seconds:avg_rate5m
expr:avgby(job,instance,mode)(rate(node_cpu_seconds_total[5m]))
prometheus.yml
中新增 rule_files
語句,以便 Prometheus 選擇此新規則。現在,prometheus.yml
配置應如下所示:global:
scrape_interval:15s# 預設,每15秒取樣一次目標
# 與其它外部系統(比如federation, remote storage, Alertmanager)互動時,會附加這些標籤到時序資料或者報警
external_labels:
monitor:'codelab-monitor'
rule_files:
-'prometheus.rules.yml'
# 一份取樣配置僅包含一個 endpoint 來做取樣
# 下面是 Prometheus 本身的endpoint:
scrape_configs:
# job_name 將被被當作一個標籤 `job=<job_name>`新增到該配置的任意時序取樣.
-job_name:'prometheus'
# 覆蓋全域性預設值,從該job每5秒對目標取樣一次
scrape_interval:5s
static_configs:
-targets:
[
'10.118.71.170:9090'
]
-job_name:'node'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval:5s
static_configs:
-targets:
[
'localhost:8001'
,
'localhost:8002'
]
labels:
group:'production'
-targets:
[
'localhost:8003'
]
labels:
group:'canary'
expression brower
查詢 job_instance_mode:node_cpu_seconds:avg_rate5m
,結果如下

文末福利

即將步入2025年,不少小夥伴在考慮來年的工作方向。
僅目前來說,傳統運維衝擊年薪30W+的轉型方向就是SRE&DevOps崗位。









