天天看點

齊活了,Grafana 釋出大規模持續性能分析開源資料庫 - Phlare

Grafana Phlare 是一個用于聚合 continuous profiling(持續分析)資料的開源軟體項目。Grafana Phlare 可以和 Grafana 完全內建,允許你與其他可觀察信号相關聯。

齊活了,Grafana 釋出大規模持續性能分析開源資料庫 - Phlare

什麼是 continuous profiling?

這個概念很有價值:Profiling 可以幫助你了解程式的資源使用情況,進而幫助你優化其性能和成本。然而,向分布式雲原生架構的轉變使這變得更加複雜,進而産生了對持續分析的需求,其中有關資源使用情況的資訊會在整個計算基礎設施中定期自動收集,然後壓縮并存儲為時間序列資料,這使你可以可視化随時間的變化并放大與感興趣的時間段相比對的 profile 檔案 — 例如,CPU 時間在其最高使用率期間所花費的時間。

就其帶來的價值而言,持續分析被稱為可觀測的第四大支柱(在metrics、logging 和 tracing 之後)。

在 Grafana Labs,我們開始研究使用持續分析來了解我們用于支援 Grafana Cloud 的軟體的性能,包括 Grafana Loki、Grafana Mimir、Grafana Tempo 和 Grafana。例如,如果我們對 Mimir 中的慢查詢進行分頁,我們可能會使用分析來了解該查詢在 Mimir 代碼庫中的哪個位置花費的時間最多。如果我們看到 Grafana 由于記憶體不足錯誤而反複崩潰,我們将檢視記憶體配置檔案以檢視崩潰前哪個對象消耗的記憶體最多。

齊活了,Grafana 釋出大規模持續性能分析開源資料庫 - Phlare

雖然有用于存儲和查詢持續分析資料的開源項目,但經過一些調查,我們努力找到一個滿足支援 Grafana Labs 所需級别的持續分析所需的可擴充性、可靠性和性能要求的項目。在全公司範圍的黑客馬拉松期間,一組工程師上司了該項目,該項目展示了與名額、日志和追蹤連接配接時分析資料的價值,進一步增加了我們在所有環境中推出連續分析的渴望。

是以,我們決定着手建立一個用于持續分析遙測的資料庫,基于使我們的其他開源可觀察性後端 Loki、Tempo 和 Mimir 如此成功的設計原則:水準可擴充架構和對象存儲的使用。

核心功能

Grafana Phlare 提供水準可擴充、高可用性、長期存儲和分析資料查詢。就像 Prometheus 一樣,隻需一個二進制檔案即可輕松安裝,無需其他依賴項。因為 Phlare 使用對象存儲,你可以存儲你需要的所有曆史記錄,而不會花很多錢。其原生多租戶和隔離功能集可以為多個獨立團隊或業務部門運作一個資料庫。Grafana Phlare 的核心功能如下所示:

  • 易于安裝:使用其單體模式,隻需一個二進制檔案即可啟動并運作 Grafana Phlare,無需其他依賴項。在 Kubernetes 上,可以使用 Helm Chart 方式進行不同模式的部署。
  • 水準可擴充性:可以在多台機器上運作 Grafana Phlare,可以輕松擴充資料庫以處理工作負載生成的分析量。
  • 高可用性:Grafana Phlare 複制傳入的 profiles 檔案,確定在機器發生故障時不會丢失資料。這意味着你可以在不中斷 profiles 檔案攝取和分析的情況下進行 rollout。
  • 廉價、耐用的 profile 檔案存儲:Grafana Phlare 使用對象存儲進行長期資料存儲,使其能夠利用這種無處不在、經濟高效、高耐用性的技術。它相容多種對象存儲實作,包括 AWS S3、谷歌雲存儲、Azure Blob 存儲、OpenStack Swift,以及任何與 S3 相容的對象存儲。
  • 原生多租戶:Grafana Phlare 的多租戶架構使你能夠将資料和查詢與獨立的團隊或業務部門隔離開來,進而使這些組可以共享同一個資料庫。

架構

Grafana Phlare 具有基于微服務的架構,該系統具有多個可水準擴充的微服務,可以單獨和并行運作。Grafana Phlare 微服務稱為元件。Grafana Phlare 的設計将所有元件的代碼編譯為單個二進制檔案。-target 參數控制單個二進制檔案将作為哪些元件運作,這點和 Grafana Loki 的模式是一樣的。對于想快速體驗的使用者來說,Grafana Phlare 同樣也可以在單體模式下運作,所有元件在一個程序中同時運作。

Grafana Phlare 的大多數元件是無狀态的,不需要在程序重新啟動之間保留任何資料。一些元件是有狀态的,并依靠不容易丢失資料的存儲來防止程序重新開機之間的資料丢失。Grafana Phlare 包括一組互相作用形成叢集的元件:Distributor、Ingester、Querier。

單體模式

單體模式在單個程序中運作所有必需的元件,是預設的操作模式,你可以通過指定 -target=all 參數來設定,單體模式是部署 Grafana Phlare 的最簡單方法,如果你想快速入門或想在開發環境中使用 Grafana Phlare,這将非常有用。要檢視在 -target 設定為 all 時運作的元件清單,請使用 -modules 标志運作 Grafana Phlare:

./phlare -modules
           
齊活了,Grafana 釋出大規模持續性能分析開源資料庫 - Phlare

微服務模式

在微服務模式下,元件部署在不同的程序中。擴充是按元件進行的,這允許在擴充和更細化的故障域方面具有更大的靈活性。微服務模式是生産部署的首選方法,但也是最複雜的。

在微服務模式下,調用每個 Grafana Phlare 程序,并将其 -target 參數設定為特定的 Grafana Phlare 元件(例如,-target=ingester 或 -target=distributor)。要獲得一個正常工作的 Grafana Phlare 執行個體,必須部署每個必需的元件。如果你想使用微服務模式部署 Grafana Phlare,那麼非常建議使用 Kubernetes。

齊活了,Grafana 釋出大規模持續性能分析開源資料庫 - Phlare

部署

我們這裡還是以 Helm Chart 的方式部署在 Kubernetes 叢集中,當然前提是有一個可用的 Kubernetes 叢集,并且配置好了 kubectl 和 helm。

首先我們建立一個名為 phlare-test 的命名空間,将整個應用都部署在該命名空間之内:

☸ ➜ kubectl create namespace phlare-test
           

然後添加 Phlare 的 Helm Chart 倉庫:

☸ ➜ helm repo add grafana https://grafana.github.io/helm-charts
☸ ➜ helm repo update
           

然後我們就可以使用 Helm 來進行安裝了。

如果你想以單體預設進行安裝,隻需要執行下面的指令即可一鍵安裝:

☸ ➜ helm -n phlare-test install phlare grafana/phlare
           

如果想以微服務模式安裝 Grafana Phlare,可以首先擷取官方提供的預設 values 配置檔案:

# 收集微服務的預設配置
☸ ➜ curl -LO values-micro-services.yaml https://raw.githubusercontent.com/grafana/phlare/main/operations/phlare/helm/phlare/values-micro-services.yaml
☸ ➜ cat values-micro-services.yaml
# Default values for phlare.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
phlare:
  components:
    querier:
      kind: Deployment
      replicaCount: 3
      resources:
        limits:
          memory: 1Gi
        requests:
          memory: 256Mi
          cpu: 100m
    distributor:
      kind: Deployment
      replicaCount: 2
      resources:
        limits:
          memory: 1Gi
        requests:
          memory: 256Mi
          cpu: 500m
    agent:
      kind: Deployment
      replicaCount: 1
      resources:
        limits:
          memory: 512Mi
        requests:
          memory: 128Mi
          cpu: 50m
    ingester:
      kind: StatefulSet
      replicaCount: 3
      resources:
        limits:
          memory: 12Gi
        requests:
          memory: 6Gi
          cpu: 1
minio:
  enabled: true
           

我們需要使用上面的 values 檔案來安裝 Grafana Phlare,也可以根據自己的叢集實際情況調整配置,比如 ingester 配置的資源請求的 memory: 6Gi,cpu: 1,我這裡叢集資源不足,可以将其降低一些,将副本數都暫時設定成1(僅供測試),不然沒辦法排程成功。

然後使用下面的指令即可開始安裝:

☸ ➜ helm -n phlare-test upgrade --install phlare grafana/phlare -f values-micro-services.yaml
Release "phlare" does not exist. Installing it now.
NAME: phlare
LAST DEPLOYED: Thu Nov  3 14:37:38 2022
NAMESPACE: phlare-test
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Thanks for deploying Grafana Phlare.
In order to configure Grafana to use the Phlare datasource, you need to add the Phlare datasource to your Grafana instance.
The in-cluster query URL is:
http://phlare-querier.phlare-test.svc.cluster.local.:4100
To forward the query API to your localhost you can use:
kubectl --namespace phlare-test port-forward svc/phlare-querier 4100:4100
           

部署完成後檢視 Pod 狀态是否正常:

☸ ➜ kubectl get pods -n phlare-test
NAME                                  READY   STATUS    RESTARTS   AGE
phlare-agent-56986dd4b9-4s6z6         1/1     Running   0          3m23s
phlare-distributor-7447b4c6c5-f4rjw   1/1     Running   0          3m23s
phlare-ingester-0                     1/1     Running   0          3m23s
phlare-minio-0                        1/1     Running   0          3m23s
phlare-querier-8cdf986c-hhn29         1/1     Running   0          3m23s
           

等到所有 Pod 的狀态變為 Running 或 Completed 表示部署完成了。

使用

然後接下來我們可以配置 Grafana 來查詢 profiles 資料,這裡我們在安裝 Phlare 的同一個 Kubernetes 叢集中安裝 Grafana,同樣使用下面的指令一鍵安裝即可:

☸ ➜ helm template -n phlare-test grafana grafana/grafana \
  --set image.repository=aocenas/grafana \
  --set image.tag=profiling-ds-2 \
  --set env.GF_FEATURE_TOGGLES_ENABLE=flameGraph \
  --set env.GF_AUTH_ANONYMOUS_ENABLED=true \
  --set env.GF_AUTH_ANONYMOUS_ORG_ROLE=Admin \
  --set env.GF_DIAGNOSTICS_PROFILING_ENABLED=true \
  --set env.GF_DIAGNOSTICS_PROFILING_ADDR=0.0.0.0 \
  --set env.GF_DIAGNOSTICS_PROFILING_PORT=6060 \
  --set-string 'podAnnotations.phlare\.grafana\.com/scrape=true' \
  --set-string 'podAnnotations.phlare\.grafana\.com/port=6060' > grafana.yaml
☸ ➜ kubectl apply -f grafana.yaml
           

部署完成後整個 phlare-test 命名空間的 Pod 清單如下所示:

☸ ➜ kubectl get pods -n phlare-test                            
NAME                                  READY   STATUS              RESTARTS   AGE
grafana-5ff87bdfd-whmkm               1/1     Running             0          85s
phlare-agent-56986dd4b9-4s6z6         1/1     Running             0          9m17s
phlare-distributor-7447b4c6c5-f4rjw   1/1     Running             0          9m17s
phlare-ingester-0                     1/1     Running             0          9m17s
phlare-minio-0                        1/1     Running             0          9m17s
phlare-querier-8cdf986c-hhn29         1/1     Running             0          9m17s
           

我們可以使用下面的指令在本地轉發 Grafana 服務:

☸ ➜ kubectl port-forward -n phlare-test service/grafana 3000:80
           

然後在浏覽器中打開 http://localhost:3000 即可通路 Grafana 服務了。

在頁面左側點選配置 -> 資料源來添加 profiles 的資料源,選擇 phlare 類型的資料源。

齊活了,Grafana 釋出大規模持續性能分析開源資料庫 - Phlare

設定資料源的 URL 為 http://phlare-querier.phlare-test.svc.cluster.local.:4100/。

齊活了,Grafana 釋出大規模持續性能分析開源資料庫 - Phlare

點選 Save & Test 即可儲存。資料源添加完成後應該能夠在 Grafana Explore 中查詢到 profiles 檔案,使用方法和 Loki 以及 Prometheus 幾乎一樣,如下所示我們可以查詢 Grafana 應用的 CPU 的情況。

齊活了,Grafana 釋出大規模持續性能分析開源資料庫 - Phlare

Phlare 與 Grafana 原生內建,使你可以将 profiles 資料與 metrics、logging 和 tracing 一起可視化,并全面了解整個堆棧。我們還在 Grafana 中添加了一個火焰圖面闆,它允許你建構儀表闆,在 Grafana 中可視化的數百個不同資料源的資料旁邊顯示分析資料。

齊活了,Grafana 釋出大規模持續性能分析開源資料庫 - Phlare

Phare 的 Helm Chart 使用預設配置,隻要它們有正确的注解,它的代理就會抓取 Pod,該功能使用可能與 Prometheus 或 Grafna Agent 配置相似的 relabel_config 和 kubernetes_sd_config。

為了讓 Phlare 抓取 pod,你必須在 pod 中添加以下注解:

metadata:
  annotations:
    phlare.grafana.com/scrape: "true"
    phlare.grafana.com/port: "8080"
           

其中 phlare.grafana.com/port 應該設定為你的 pod 為 /debug/pprof/ 端點提供服務的端口。請注意,phlare.grafana.io/scrape 和 phlare.grafana.io/port 的值必須用雙引号括起來,以確定它表示為字元串。

上面我們在安裝 Grafana 的時候就配置了這兩個注解,是以我們就可以使用 Phlare 來不斷的抓取 Grafana 應用的 profiles 資料了,這也是為什麼上面我們可以去過濾 Grafana 的 profiles 資料的原因。

參考連結

  • https://github.com/grafana/phlare
  • https://grafana.com/blog/2022/11/02/announcing-grafana-phlare-oss-continuous-profiling-database/

▲ 點選上方卡片關注K8s技術圈,掌握前沿雲原生技術

繼續閱讀