0、登录
将MariaDB服务器IP和主机名添加到“/etc/hosts”文件中,
1、yum安装
yum install haproxy
2、配置HAProxy的日志
通过编辑rsyslog配置文件配置HAProxy的日志:
nano /etc/rsyslog.conf
取消注释udp端口:
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 51
保存文件并退出编辑器。
3、rsyslog配置
在rsyslog.d目录中添加HAProxy配置文件:
nano /etc/rsyslog.d/haproxy.conf
粘贴配置如下:
local2.=info /var/log/haproxy-access.log
local2.notice /var/log/haproxy-info.log
保存文件并重新启动rsyslog:
systemctl restart rsyslog
接下来,创建一个新的HAProxy配置文件。
4、HAProxy配置
创建一个新的HAProxy配置文件。
转到/ etc / haproxy目录并备份dafult haproxy配置文件。
cd /etc/haproxy/
mv haproxy.cfg haproxy.cfg.bak
创建新的haproxy配置文件:
nano /etc/haproxy/haproxy.cfg
粘贴以下内容:
global
log 127.0.0.1 local2
maxconn 1024
user haproxy
group haproxy
daemon
stats socket /var/run/haproxy.sock mode 600 level admin # Make sock file for haproxy
defaults
log global
mode http
option tcplog
option dontlognull
retries 3
option redispatch
maxconn 1024
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
listen mariadb_cluster 0.0.0.0:3030
## MariaDB balance leastconn - the cluster listening on port 3030.
mode tcp
balance leastconn
option httpchk
server mariadb1 192.168.1.132:3306 check port 9200
server mariadb2 192.168.1.133:3306 check port 9200
server mariadb3 192.168.1.134:3306 check port 9200 backup # Make mariadb3 as backup - automatic replication data
listen stats 0.0.0.0:9000
## HAProxy stats web gui running on port 9000 - username and password: howtoing.
mode http
stats enable
stats uri /stats
stats realm HAProxy\ Statistics
stats auth howtoing:howtoing
stats admin if TRUE
端口3030是负载平衡的MariaDB端口,我们可以使用负载均衡器IP和该端口访问所有MariaDB服务器上的数据库。
端口9000是web-gui的端口,它允许从浏览器监控HAProxy。
现在启动firewalld,打开端口9000和3030,然后重新加载它:
systemctl start firewalld
firewall-cmd --permanent --add-port=9000/tcp
firewall-cmd --permanent --add-port=3030/tcp
firewall-cmd --reload
使用systemctl命令启动HAProxy:
systemctl start haproxy
systemctl enable haproxy
您可以通过访问端口3030上的负载平衡器IP来查看数据库服务器的用户。您可以通过访问3030端口上的HAProxy IP来访问MySQL节点上的数据库。
5、登录到HAProxy Web-Gui
您可以访问端口9000上的HAProxy监控服务器,用户名和密码“howtoing”显示HAProxy Web UI:
http://192.168.1.135:9000/stats
HAProxy已经成功安装,您可以看到MariaDB服务器被监控。
6、坑
遇到了Starting proxy mariadb_cluster: cannot bind socket [0.0.0.0:3030]故障。服务起不来
经搜索,找到一个有效的解决方案
setsebool -P haproxy_connect_any=1
6、结语
MariaDB Galera Cluster是一个同步多主机集群,为MySQL数据库提供负载平衡。 这是为您的站点构建强大的数据库系统的良好解决方案。 MariaDB Galera Cluster支持 xtradb和innodb存储引擎,它提供自动复制并允许自动加入新节点。 我们可以在数据库集群前面使用HAProxy作为负载均衡器。 负载平衡器有助于将传入的请求分发到所有数据库。 我们使用bash脚本clustercheck从HAProxy执行数据库监视。 MariaDB Galera Cluster和HAProxy是构建高可用性数据库系统的最佳解决方案之一。