K8S 中的 Grafana 數據持久化

自從將 Grafana 部署到 K8S 中以後,帶來了很多的便利性,但是也有一個問題一直困擾着我,那就是 Grafana 中的數據無法持久化,經常配置好的 Datasource 和 Dashboards 在運行一段時間後就丟失了,有時候剛要排查問題,結果什麼都找不到了。我們都知道,Grafana 在啓動後,數據會存儲到數據庫中,包括 datasource 的配置,Dashboards 的配置。要解決 Grafana 的數據持久化可以從 2 個方面來進行思考並且解決。

方案一:

Grafana 數據存儲在數據庫中,當前支持 MySQL 、sqlite3、Postgres 這三個,缺省情況下,Grafana 會在本地啓動一個 Sqlite3 來存儲數據,你也可以在 grafana.ini 配置文件中修改這個配置來切換數據庫。

基於這個方式,第一種方案出來了。 當 Grafana 運行在 K8S 中時,數據庫不使用自動啓動的 Sqlite3 ,在 K8S 外啓動一個關係數據庫(可以是 MySQL 、sqlite3、Postgres 中的任何一個),修改 grafana.ini 文件,使得 Grafana 在啓動時連接外部的數據庫,這樣數據就不會丟失了。

方案二:

Grafana 在 v5.0 之後引入了 provisioning 功能,可以將 Datasource 、Dashboards 這些內容,通過配置文件的方式在 Grafana 啓動的時候加載。把這些文件放在 K8S 的配置映射中,然後掛載到 Grafana 容器中的指定路徑即可。這樣配置後在 Web UI 修改已經不會生效,只有在對應的配置文件中修改纔會生效。

接下里我們看看方案二的怎麼配置使用,我使用的 Grafana 版本是 v6.4.3。

首先需要在 Grafana 啓動時添加相應的參數來打開 provisioning 功能,官方提供的 docker 鏡像已經將該功能打開,缺省的路徑是 /etc/grafana/provisioning ,這個路徑下有 dashboards、datasources、notifiers 三個文件夾,相關的配置會放在這裏。如果是自己構建的鏡像,請參考文檔或者官方的 dockerfile 把該功能打開。

其次我們需要解決數據源的持久化問題,我們準備 datasources.yaml 文件,用來配置數據源。datasources.yaml 文件內容如下:

# config file version
apiVersion: 1

# list of datasources that should be deleted from the database
deleteDatasources:
  - name: Prometheus
    orgId: 1

# list of datasources to insert/update depending
# what's available in the database
datasources:
  # <string, required> name of the datasource. Required
  - name: Prometheus
    # <string, required> datasource type. Required
    type: prometheus
    # <string, required> access mode. proxy or direct (Server or Browser in the UI). Required
    access: proxy
    # <int> org id. will default to orgId 1 if not specified
    orgId: 1
    # <string> custom UID which can be used to reference this datasource in other parts of the configuration, if not specified will be generated automatically
    uid: my_unique_uid
    # <string> url
    url: http://127.0.0.1:9090
    # <string> Deprecated, use secureJsonData.password
    password:
    # <string> database user, if used
    user:
    # <string> database name, if used
    database:
    # <bool> enable/disable basic auth
    basicAuth:
    # <string> basic auth username
    basicAuthUser:
    # <string> Deprecated, use secureJsonData.basicAuthPassword
    basicAuthPassword:
    # <bool> enable/disable with credentials headers
    withCredentials:
    # <bool> mark as default datasource. Max one per org
    isDefault:
    # <map> fields that will be converted to json and stored in jsonData
    jsonData:
      graphiteVersion: '1.1'
      tlsAuth: false
      tlsAuthWithCACert: false
    # <string> json object of data that will be encrypted.
    secureJsonData:
      tlsCACert: '...'
      tlsClientCert: '...'
      tlsClientKey: '...'
      # <string> database password, if used
      password:
      # <string> basic auth password
      basicAuthPassword:
    version: 1
    # <bool> allow users to edit datasources from the UI.
    editable: false

將該文件放在 K8S 的 config map 中,在容器中時添加數據卷,卷類型選配置映射卷 ,選擇 config map 中的 datasources.yaml 掛載到容器的 /etc/grafana/provisioning/datasources ,然後重啓容器,讓容器重新加載配置。

使用後在 Web UI 的數據源頁面進行修改會提示 This datasource was added by config and cannot be modified using the UI. Please contact your server admin to update this datasource. 表示數據源的配置,在 Web UI 修改已經不會生效,只有在 datasources.yaml 文件中修改纔會生效。

接下來解決 dashboards 的持久化問題,準備 dashboards.yaml 文件,用來指定之後的 dashboards 的 json 文件放在哪個目錄。

apiVersion: 1

providers:
  # <string> an unique provider name. Required
  - name: 'a unique provider name'
    # <int> Org id. Default to 1
    orgId: 1
    # <string> name of the dashboard folder.
    folder: ''
    # <string> folder UID. will be automatically generated if not specified
    folderUid: ''
    # <string> provider type. Default to 'file'
    type: file
    # <bool> disable dashboard deletion
    disableDeletion: false
    # <bool> enable dashboard editing
    editable: true
    # <int> how often Grafana will scan for changed dashboards
    updateIntervalSeconds: 10
    # <bool> allow updating provisioned dashboards from the UI
    allowUiUpdates: false
    options:
      # <string, required> path to dashboard files on disk. Required when using the 'file' type
      path: /etc/grafana/dashboards-files

將該文件放在 K8S 的 config map 中,在容器中時添加數據卷,卷類型選配置映射卷 ,選擇 config map 中的 dashboards.yaml 掛載到容器的 /etc/grafana/provisioning/dashboards ,然後重啓容器,讓容器重新加載配置。

接下來將準備好的 json 格式的 Dashboards 文件放到 /etc/grafana/dashboards-files, 重新加載這些 json 文件即可。

參考鏈接:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章