Xtradb集羣部署完成後,3臺機器都能同時讀寫,此時須要在前端搭建haproxy來進行負載均衡。html
官網haproxy配置參考地址:前端
https://www.percona.com/doc/percona-xtradb-cluster/5.6/howtos/virt_sandbox.htmlnode
Haproxy服務器配置mysql
拿一臺機器用做haproxy,此處是192.168.6.219。web
安裝haproxyredis
yum install haproxy -ysql
配置文件:服務器
[root@yang-219 ~]# cat /etc/haproxy/haproxy.cfg負載均衡
globalfrontend
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
chroot /usr/share/haproxy
user haproxy
group haproxy
daemon
defaults
log global
mode http
option tcplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
frontend pxc-front
bind *:3307
mode tcp
default_backend pxc-back
frontend stats-front
bind *:8080
mode http
default_backend stats-back
frontend pxc-onenode-front
bind *:3308
mode tcp
default_backend pxc-onenode-back
backend pxc-back
mode tcp
balance leastconn
option httpchk
server c1 192.168.70.71:3306 check port 9200 inter 12000 rise 3fall 3
server c2 192.168.70.72:3306 check port 9200 inter 12000 rise 3fall 3
server c3 192.168.70.73:3306 check port 9200 inter 12000 rise 3fall 3
backend stats-back
mode http
balance roundrobin
stats uri /haproxy/stats
stats auth pxcstats:secret
backend pxc-onenode-back
mode tcp
balance leastconn
option httpchk
server c1 192.168.70.71:3306 check port 9200 inter 12000 rise 3fall 3
server c2 192.168.70.72:3306 check port 9200 inter 12000 rise 3fall 3
server c3 192.168.70.73:3306 check port 9200 inter 12000 rise 3fall 3
啓動haproxy
/etc/init.d/haproxy start
web訪問
上面配置的8080端口及/haproxy/stats
訪問url:192.168.6.219:8080/haproxy/stats
用戶名密碼是上面配置的pxcstats:secret
在全部xtradb服務器上,都要配置xinetd打開9200端口來進行監控。
yum instal xinetd
配置mysqlchk監控
[root@percona2 mysql]# cat /etc/xinetd.d/mysqlchk
# default: on
# description: mysqlchk
service mysqlchk
{
# this is a config for xinetd, place it in /etc/xinetd.d/
disable = no
flags = REUSE
socket_type = stream
type = UNLISTED
port = 9200
wait = no
user = nobody
server = /usr/bin/clustercheck
log_on_failure += USERID
only_from = 0.0.0.0/0
#
# Passingarguments to clustercheck
# <user><pass> <available_when_donor=0|1> <log_file><available_when_readonly=0|1> <defaults_extra_file>"
# Recommended:server_args = user pass 1/var/log/log-file 0 /etc/my.cnf.local"
# Compatibility:server_args = user pass 1 /var/log/log-file 1 /etc/my.cnf.local"
# 55-to-56upgrade: server_args = user pass 1 /var/log/log-file 0 /etc/my.cnf.extra"
#
# recommended toput the IPs that need
# to connectexclusively (security purposes)
per_source = UNLIMITED
}
默認安裝xtradb server後會安裝此配置.
重啓xinetd服務
/etc/init.d/xinetd restart
haproxy每一段時間檢測xtradb服務器上的9200端口,當clustercheck命令執行的結果不是200時,haproxy的檢測將會把該機器從負載均衡中摘除,從而達到自動failover的效果。