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

一.介绍
由于互联网系统读的压力要远大于写的压力,因此该软件主要实现分散压力,负载均衡的功能。
- 四层负载均衡(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

- 根据内核版本进行编译
这里写图片描述
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.207
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博客 ” ,链接:http://blog.itpub.net/15412087/viewspace-2157512/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15412087/viewspace-2157512/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值