一.实现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,在其他机器上可见。