环境:

  • OS:CentOS7_x86
  • grafana: v5.4.2
  • MySQL: 5.7.28

主机:

Hostname IP Role
centos1 192.168.2.87 grafana
centos2 192.168.2.88 grafana
centos3 192.168.2.89 grafana,mysql
haproxy1 192.168.2.99 haproxy

架构图:

架构图

其中haproxy换成nginx也可以。

效果:

  • Dashboard及用户等配置共享存储,以此达到多grafana高可用性。共享的完整列表如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
+--------------------------+
| alert |
| alert_notification |
| alert_notification_state |
| annotation |
| annotation_tag |
| api_key |
| dashboard |
| dashboard_acl |
| dashboard_provisioning |
| dashboard_snapshot |
| dashboard_tag |
| dashboard_version |
| data_source |
| login_attempt |
| migration_log |
| org |
| org_user |
| playlist |
| playlist_item |
| plugin_setting |
| preferences |
| quota |
| session |
| star |
| tag |
| team |
| team_member |
| temp_user |
| test_data |
| user |
| user_auth |
+--------------------------+
  • session会话保持:启用多grafana实例反向代理访问,存在会话保持问题;若代理层不支持session sticky,则可将session存储在共享数据库中,解决session无法跨节点同步的问题。

mysql安装配置

mysql安装

mysql安装配置此处略,连接信息如下:

  • 地址:192.168.2.89:3306
  • 用户:root
  • 密码:root_pw

创建grafana库及session表

创建grafana库

1
CREATE DATABASE grafana default CHARACTER SET utf8;

在grafana库中创建session表

1
2
3
4
5
6
7
USE grafana;
CREATE TABLE `session` (
`key` CHAR(16) NOT NULL,
`data` BLOB,
`expiry` INT(11) UNSIGNED NOT NULL,
PRIMARY KEY (`key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

grafana安装及配置

所有garfana节点安装配置均相同

安装grafana

此处直接使用官方rpm包安装

1
2
3
yum install grafana-5.4.2-1.x86_64.rpm
systemctl deamon-reload
systemctl enable grafana-server

配置grafana

配置文件:/etc/grafana/grafana.ini,仅列出自定义配置项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#################################### Server ####################################
[server]
# The full public facing url you use in browser, used for redirects and emails
# If you use reverse proxy and sub path specify full url (with sub path)
root_url = http://192.168.2.99:3001

#################################### Database ####################################
[database]
# use mysql to store grafana data
type = mysql
# mysql databse connection url
host = 192.168.2.89:3306
# mysql db name
name = grafana
# mysql user
user = root
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = root_pw
# Set to true to log the sql calls and execution times.
log_queries =

#################################### Session ####################################
[session]
# session provide use mysql
provider = mysql
# mysql provider connection config
provider_config = root:root_pw@tcp(192.168.2.89: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

注意root_url为haproxy/nginx反向代理后的地址或域名,这里若设置不正确,会导致grafana页面跳转不正常。

启动grafana

1
systemctl start grafana-server

反向代理配置

haproxy添加配置

1
2
3
4
5
6
7
listen grafana-ha-test
bind 192.168.2.99:3001
mode tcp
option tcplog
server centos1 192.168.2.87:3000 check
server centos2 192.168.2.88:3000 check
server centos3 192.168.2.89:3000 check

重载haproxy

1
systemctl reload haproxy

访问grafana-ha:http://192.168.2.99:3001,默认用户名密码admin/admin

使用grafana

dashboard共享

通过grafana web页面导入的dashboard会自动存储到数据库中,所以一经导入(无论通过哪个节点,包括代理节点),所有节点即可生效。此工作原理也对所有共享列表(上述列出,如用户和session等)均有效。

插件安装

由于grafana的插件是本地安装,所以额外插件必须要在三个节点都安装才完全生效。