天天看點

Xtradb+Haproxy高可用資料庫叢集(二)haproxy負載均衡篇

Xtradb叢集部署完成後,3台機器都能同時讀寫,此時需要在前端搭建haproxy來進行負載均衡。

官網haproxy配置參考位址:

<a href="https://www.percona.com/doc/percona-xtradb-cluster/5.6/howtos/virt_sandbox.html" target="_blank">https://www.percona.com/doc/percona-xtradb-cluster/5.6/howtos/virt_sandbox.html</a>

Haproxy伺服器配置

拿一台機器用作haproxy,此處是192.168.6.219。

安裝haproxy

yum install haproxy -y

配置檔案:

[root@yang-219 ~]# cat /etc/haproxy/haproxy.cfg

global

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

default_backend stats-back

frontend pxc-onenode-front

bind *:3308

default_backend pxc-onenode-back

backend pxc-back

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

balance roundrobin

stats uri /haproxy/stats

stats auth pxcstats:secret

backend pxc-onenode-back

啟動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

        # &lt;user&gt;&lt;pass&gt; &lt;available_when_donor=0|1&gt; &lt;log_file&gt;&lt;available_when_readonly=0|1&gt; &lt;defaults_extra_file&gt;"

        # 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的效果。

     本文轉自楊雲1028 51CTO部落格,原文連結:http://blog.51cto.com/yangrong/1684134,如需轉載請自行聯系原作者