
1. 問題描述
工作環(huán)境中有三個獨立的 Ceph 集群,分別負(fù)責(zé)對象存儲、塊存儲和文件存儲。搭建這幾個 Ceph 集群時,我對 Ceph 重命名 Cluster name 的難度沒有足夠的了解,所以使用的都是默認(rèn)的 cluster name:ceph,不巧的是 Prometheus 的 ceph_exporter 就是用 cluster name 來區(qū)分不同集群,結(jié)果是 Grafana 中各個集群的數(shù)據(jù)無法區(qū)分,所有的集群數(shù)據(jù)都繪制在了一個圖標(biāo)中,非常亂不說,而且部分?jǐn)?shù)據(jù)還無法正常顯示。
也許大家會說,那就改 Ceph cluster name 不就好了。問題是 Ceph 修改 Cluster name 沒那么簡單,ceph 文件存儲目錄都是和 Cluster name 有對應(yīng)關(guān)系的,所以很多配置文件和數(shù)據(jù)都需要修改目錄才能生效,對于已經(jīng)開始正式使用的 Ceph 集群,這么做風(fēng)險有點大。當(dāng)然如果給每個 Ceph 集群單獨搭建一個 Prometheus 和 Grafana 環(huán)境的話,問題也能解決,但這種方式顯得太沒技術(shù)含量了,不到萬不得已,實在不想采用。
我最開始想到的解決方式是修改 ceph_exporter,既然 cluster name 不行,那加上 Ceph 的 fsid 總能區(qū)分出來了吧,就像這樣:

不過 fsid 這個變量很難直觀看出來代表的是哪個 Ceph 集群,也不是一個好的方案。
最后多虧 neurodrone,才了解到 Prometheus 的 relabel 功能,可以完美的解決這個問題。
2. relabel 配置
Relabel 的本意其實修改導(dǎo)出 metrics 信息的 label 字段,可以對 metrics 做過濾,刪除某些不必要的 metrics,label 重命名等,而且也支持對 label 的值作出修改。
舉一個例子,三個集群的 ceph_pool_write_total 的 label cluster 取值都為 ceph。但在 Prometheus 的配置中,他們分別是分屬于不通 job 的,我們可以通過對 job 進(jìn)行 relabel 來修改 cluster label 的指,來完成區(qū)分。
# cluster1's metric
ceph_pool_write_total{cluster="ceph",pool=".rgw.root"} 4
# cluster2's metric
ceph_pool_write_total{cluster="ceph",pool=".rgw.root"} 10
# cluster3's metric
ceph_pool_write_total{cluster="ceph",pool=".rgw.root"} 7
具體的配置如下,cluster label 的值就改為了 ceph*,并且導(dǎo)出到了新 label clusters 中。
scrape_configs:
- job_name: 'ceph1'
relabel_configs:
- source_labels: ["cluster"]
replacement: "ceph1"
action: replace
target_label: "clusters"
static_configs:
- targets: ['ceph1:9128']
labels:
alias: ceph1
- job_name: 'ceph2'
relabel_configs:
- source_labels: ["cluster"]
replacement: "ceph2"
action: replace
target_label: "clusters"
static_configs:
- targets: ['ceph2:9128']
labels:
alias: ceph2
- job_name: 'ceph3'
relabel_configs:
- source_labels: ["cluster"]
replacement: "ceph3"
action: replace
target_label: "clusters"
static_configs:
- targets: ['ceph3:9128']
labels:
alias: ceph3
修改后的 metric 信息變成這個樣子,這樣我們就可以區(qū)分出不同的 Ceph 集群的數(shù)據(jù)了。
# cluster1's metric
ceph_pool_write_total{clusters="ceph1",pool=".rgw.root"} 4
# cluster2's metric
ceph_pool_write_total{clusters="ceph2",pool=".rgw.root"} 10
# cluster3's metric
ceph_pool_write_total{clusters="ceph3",pool=".rgw.root"} 7
3. Grafana dashboard 調(diào)整
光是修改 Prometheus 的配置還不夠,畢竟我們還要在界面上能體現(xiàn)出來,Grafana 的 dashboard 也要做對應(yīng)的修改,本文使用的 dashboard 是 Ceph - Cluster。
首先是要 dashboard 添加 clusters 變量,在界面上操作即可。
先點擊 dashboard 的 "settings" 按鈕(顯示齒輪圖標(biāo)的就是)

如下圖所示添加 clusters variable,最后保存。

我們已經(jīng)可以在 dashboard 上看到新加的 variable 了:

接下來每個圖表的查詢語句也要做對應(yīng)的修改:

最終改好的 dashboard json 文件可從如下鏈接下載到:
ceph-cluster.json