yuandan 发表于 2019-1-1 13:12:32

Xtradb+Haproxy高可用数据库集群(二)haproxy负载均衡篇

  Xtradb集群部署完成后,3台机器都能同时读写,此时需要在前端搭建haproxy来进行负载均衡。
  官网haproxy配置参考地址:
  

  https://www.percona.com/doc/percona-xtradb-cluster/5.6/howtos/virt_sandbox.html
  

  Haproxy服务器配置
  拿一台机器用作haproxy,此处是192.168.6.219。
  安装haproxy
yum install haproxy -y
  配置文件:
# 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
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
http://s3.运维网.com/wyfs02/M02/71/51/wKioL1XLHSPRsohPAAIHV_hx58I687.jpg
  

xtradb服务器上xinetd配置
  在所有xtradb服务器上,都要配置xinetd打开9200端口来进行监控。
yum instalxinetd
  配置mysqlchk监控
# 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
      #    "
      # 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的效果。
  




页: [1]
查看完整版本: Xtradb+Haproxy高可用数据库集群(二)haproxy负载均衡篇