為了獲得高可用性的服務,需要對單台部署grafana的伺服器進行擴充。
圖檔引自grafana文檔
這個過程中需要做兩件事:
- 配置一個公共資料庫,使所有的grafana伺服器可以将配置寫入公共資料庫中而不是存在本地;
- 選用一個會話存儲方案,這一步可以通過讓負載均衡啟用會話保持功能解決
資料庫配置
grafana為資料庫提供多種可選方案,包括:mysql,postgresql,sqlite3(default)。
配置檔案為:
vi /etc/grafana/grafana.ini
配置項為database部分:
# You can configure the database connection by specifying type, host, name, user and password
# as separate properties or as on string using the url properties.
# Either "mysql", "postgres" or "sqlite3", it's your choice
type = mysql ;資料庫類型
host = www.url.com ;資料庫域名
name = grafana_configure ;資料庫名稱
user = admin ;資料庫使用者名
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = admin_password ;資料庫使用者密碼
# Use either URL or the previous fields to configure the database
# Example: mysql://user:secret@host:port/database
;url =
# For "postgres" only, either "disable", "require" or "verify-full"
;ssl_mode = disable
"/etc/grafana/grafana.ini" 487L, 15112C written
僅需要為grafana開辟一個資料庫即可,資料表和字段将在使用者建立一個dashboard後自動寫入。
會話政策
grafana支援将回話儲存在磁盤/資料庫中,這兩種儲存方法對應着兩種政策:會話保持和無狀态會話。
-
會話保持
配合負載均衡,将同一使用者的請求轉發到相同的伺服器上。這樣工作量最小,但是也會出現某台伺服器負載較其他伺服器更高的情況。
-
無狀态回話
grafana将使用者的會話儲存在資料庫中,這樣同一使用者的會話也将會被分發到各個伺服器中處理,這種方式需要在資料庫中提前預設一張資料表,在grafana.ini中的session部分有關于此項的配置資訊。
方案步驟
- 建立一個資料庫,為grafana建立可操作使用者
- 部署N台grafana伺服器,修改各伺服器的grafana.ini檔案,寫入資料庫相關配置資訊,重新開機grafana-server。
- 将配置好的grafana伺服器挂載到負載均衡中,開啟會話保持
- 通路負載均衡的IP,獲得高可用的grafana服務