一.實現Grafana高可用
1.Grafana實現高可用性有兩步:
>>使用共享數據庫存儲儀表板,用戶和其他持久數據
>>決定如何存儲會話數據。
二 grafna代理配置
前言:因為我這里使用了supervisor進程管理器,每一個grafana都被進程管理器接管了,沒有使用supervisor的同學,參考高可用配置就好,忽略關於supervisor相關的命令。
1.nginx配置
選取一台機器做主節點配置:
cd /data/yy-monitor-server/etc
# grafana upstream gf{ ip_hash; server 主機ip:3000; server 其他機器ip:3000; server 其他機器ip:3000; } # grafana location /grafana/ { proxy_set_header Authorization "Basic YWRtaW46YWRtaW4="; proxy_pass http://gf/; } |
注:ip_hash;使用粘滯會話。
重啟nginx:
~]# supervisorctl restart nginx nginx: stopped nginx: started |
2 驗證配置
其中一台# supervisorctl stop grafana grafana: stopped 另一台# supervisorctl stop grafana grafana: stopped |
訪問ui
結果:正常訪問,代理配置成功如下圖。
三.使用同一數據源的配置
1.准備環境:
至少3台主機,其中一台主機已經裝好mysql數據庫以及redis數據庫。
# mysql -uroot -p12345678 mysql> CREATE DATABASE grafana DEFAULT CHARACTER SET utf8; Query OK, 1 row affected (0.02 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | apolloconfigdb | | apolloportaldb | | grafana | | mysql | | performance_schema | | sys | | testforliuxw | | txc | +--------------------+ 9 rows in set (0.01 sec) mysql> use grafana; Database changed mysql> show tables; Empty set (0.00 sec) mysql> CREATE TABLE `session` ( -> `key`CHAR(16) NOT NULL, -> `data`BLOB, -> `expiry`INT(11) UNSIGNED NOT NULL, -> PRIMARY KEY (`key`) -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.04 sec) mysql> show tables; +-------------------+ | Tables_in_grafana | +-------------------+ | session | +-------------------+ 1 row in set (0.00 sec) |
2.修改配置文件
2.1 修改數據源
Grafana默認使用了內嵌數據庫sqlite3來進行用戶以及dashboard相關配置的存儲。更改配置文件的[database]部分,改為mysql(可以更改為"postgres"等其他數據庫):
cd /data/yy-monitor-server/etc
vi grafana.ini
#################################### Database #################################### [database] # You can configure the database connection by specifying type, host, name, user and password # as seperate properties or as on string using the url propertie. # Either "mysql", "postgres" or "sqlite3", it's your choice ;type = mysql ;host = 裝有mysql數據庫的主機ip:3306 ;name = grafana ;user = root # If the password contains # or ; you have to wrap it with trippel quotes. Ex """#password;""" ;password =12345678 # 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 # For "sqlite3" only, path relative to data_path setting ;path = grafana.db # Max conn setting default is 0 (mean not set) ;max_conn = ;max_idle_conn = ;max_open_conn = |
注:
Grafana支持memory,file,mysql,postgres,memchche,redis這幾種存儲。默認把session存在本地的文件系統,因此如果是采用session sticky策略進行轉發的,則沒有影響,否則的話,需要處理session同步問題。
2.3 配置session
#################################### Session #################################### [session] # Either "memory", "file", "redis", "mysql", "postgres", default is "file" ;provider = mysql # Provider config options # memory: not have any config yet # file: session dir path, is relative to grafana data_path # redis: config like redis server e.g. `addr=127.0.0.1:6379,pool_size=100,db=grafana` # mysql: go-sql-driver/mysql dsn config string, e.g. `user:password@tcp(127.0.0.1:3306)/database_name` # postgres: user=a password=b host=localhost port=5432 dbname=c sslmode=disable ;provider_config = root:12345678@tcp(裝有mysql數據庫的主機ip:3306)/grafana # Session cookie name ;cookie_name = grafana_sess # If you use session in https only, default is false ;cookie_secure = false # Session life time, default is 86400 ;session_life_time = 86400 |
#################################### Session #################################### [session] ;provider = redis ;provider_config = addr=裝有redis數據庫的主機ip:6379,pool_size=100,db=grafana ;cookie_name = grafana_sess ;cookie_secure = false ;session_life_time = 86400 |
注:可以通過修改;session_life_time,在grafna上創建用戶,驗證效果。
2.4 重啟grafana
# supervisorctl restart grafana grafana: stopped grafana: started |
3.ui上配置數據庫
配置相同的Mysql數據源,配置成功如下圖:
4.驗證配置
可在某一主機的grafna頁面保存新的dashboard,在其他機器上可見。