ITPub博客

首页 > 数据库 > MySQL > haproxy+keepalived+mha 实现slave集群负载均衡

haproxy+keepalived+mha 实现slave集群负载均衡

原创 MySQL 作者:wwjfeng 时间:2018-07-07 21:11:55 0 删除 编辑

一.介绍
由于互联网系统读的压力要远大于写的压力,因此该软件主要实现分散压力,负载均衡的功能。
- 四层负载均衡(TCPIP协议的负载均衡)
haproxy lvs

- 七层负载均衡(http协议的负载均衡)
nginx

二.结构图


三.安装配置haproxy
1.将安装包上传
haproxy-1.7.9.tar.gz

2.编译安装
tar -xzvf haproxy-1.7.9.tar.gz
- 查看内核版本
# uname -r
2.6.32-696.el6.x86_64

- 根据内核版本进行编译
<1img src="https://img-blog.csdn.net/20170415144338483?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamlhbmt1bmtpbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" title="" style="box-sizing:border-box;outline:0px;margin:24px 0px;max-width:100%;word-break:break-all;cursor:zoom-in;color:#4F4F4F;font-family:-apple-system, " font-size:16px;text-align:justify;white-space:normal;background-color:#ffffff;"="">
yum install gcc*
make TARGET=linux26 PREFIX=/usr/local/haproxy ARCH=X86_64
make install PREFIX=/usr/local/haproxy

# /usr/local/haproxy/sbin/haproxy -v
HA-Proxy version 1.7.9 2017/08/18
Copyright 2000-2017 Willy Tarreau


3.建立haproxy用户
useradd haproxy
chown -R  haproxy.haproxy /usr/local/haproxy

4.配置
- 复制haproxy文件到/usr/sbin下 
因为下面的haproxy.init启动脚本默认会去/usr/sbin下找,当然你也可以修改,不过比较麻烦。
cp /usr/local/haproxy/sbin/haproxy /usr/sbin/

- 复制haproxy脚本,到/etc/init.d下
cp /tmp/haproxy-1.7.9/examples/haproxy.init /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy

- 创建配置文件
mkdir /etc/haproxy
vi /etc/haproxy/haproxy.cfg

点击( 此处)折叠或打开

  1. # this config needs haproxy-1.1.28 or haproxy-1.2.1
  2. global
  3. maxconn 4096
  4. log 127.0.0.1   local1 notice
  5. chroot /usr/local/haproxy
  6. user haproxy
  7. group haproxy
  8. daemon
  9. nbproc 1
  10. pidfile /usr/local/haproxy/logs/haproxy.pid
  11. defaults
  12. log     global
  13. retries 3
  14. option          redispatch
  15. maxconn 4096
  16. timeout         http-keep-alive 10s
  17. timeout check 10s
  18. timeout connect 5000
  19. timeout client  50000
  20. timeout server  50000
  21. listen  admin_stats
  22. bind 192.168.1.208:48800
  23. stats enable
  24. stats hide-version
  25. stats realm <realm>
  26. stats refresh 5s
  27. stats uri /admin-status
  28. stats auth admin:admin
  29. stats admin if TRUE
  30. mode http
  31. option httplog
  32. timeout connect 50000
  33. timeout check 50000
  34. timeout client 300000
  35. timeout server 300000
  36. listen  mysql_proxy
  37. bind 192.168.1.208:3307
  38. mode tcp
  39. balance roundrobin
  40. option tcpka
  41. option tcplog
  42. server  mysql5.7 192.168.1.203:3306 weight 1 check inter 2000 rise 2 fall 5
  43. server  mysql5.7-2 192.168.1.204:3306 weight 1 check inter 2000 rise 2 fall 5
  44. server  mysql5.7-3 192.168.1.205:3306 weight 1 check inter 2000 rise 2 fall 5

(1).roundrobin:轮询,轮流分配到后端服务器; 
(2).static-rr:根据后端服务器性能分配; 
(3).leastconn:最小连接者优先处理; 
(4).source:根据请求源IP,与Nginx的IP_Hash类似

- 配置日志
vi /etc/rsyslog .conf  去掉下面两列的注释
$ModLoad imudp
$UDPServerRun 514
然后添加下面的行
local1 . * / var / log /haproxy . log

重启rsyslog
# service rsyslog restart
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]

5.启动haproxy
在管理server上添加vip
ifconfig eth0:0 192.168.1.208
service haproxy start

四.测试负载均衡
1.数据库连接

点击( 此处)折叠或打开

  1. [root@mysql5 ~]# mysql -uroot -p -h 192.168.1.208 -P 3307 -e "select @@hostname"
  2. Enter password:
  3. +------------+
  4. | @@hostname |
  5. +------------+
  6. | mysql5.7   |
  7. +------------+
  8. [root@mysql5 ~]# mysql -uroot -p -h 192.168.1.208 -P 3307 -e "select @@hostname"
  9. Enter password:
  10. +------------+
  11. | @@hostname |
  12. +------------+
  13. | mysql5.7-2 |
  14. +------------+
  15. [root@mysql5 ~]# mysql -uroot -p -h 192.168.1.208 -P 3307 -e "select @@hostname"
  16. Enter password:
  17. +------------+
  18. | @@hostname |
  19. +------------+
  20. | mysql5.7-3 |
  21. +------------+
  22. [root@mysql5 ~]# mysql -uroot -p -h 192.168.1.208 -P 3307 -e "select @@hostname"
  23. Enter password:
  24. +------------+
  25. | @@hostname |
  26. +------------+
  27. | mysql5.7   |
  28. +------------+


2.登陆网页
http://192.168.1.208:48800/admin-status



来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15412087/viewspace-2157512/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-03-27

  • 博文量
    57
  • 访问量
    199171