keepalived+lvs实现对mysql读负载均衡

环境:redhat 6.5

real server: 172.25.254.189 172.25.254.224

lvs: 172.25.254.5 172.25.254.112

给real server端配置VIP,写脚本

#!/bin/bash
VIP=172.25.254.222
. /etc/init.d/functions
case "$1" in
start)
/sbin/ifconfig lo:0 $VIP broadcast netmask 255.255.255.255 up
echo "real server lvs start"
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
/sbin/ifconfig lo:0 down
echo "real server lvs stop"
echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
;;
*)
echo "Usage: $0 (start|stop)"
exit 1
esac
exit 0

配置lvs和keepalived

安装ipvsadm

yum install ipvsadm -y

在两台调度端上安装keepalived

tar zxf keepalived-1.2.19.tar.gz

 yum install -y libnl-devel  openssl-devel gcc

./configure --prefix=/usr/local/keepalived

 make

 make install

scp -r /usr/local/keepalived/ 172.25.7.4:/usr/local/

两台lvs上配置

ln -s /usr/local/keepalived/etc/keepalived/ /etc/  ##配置文件

 ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

 ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/  ##启动脚本

ln -s /usr/local/keepalived/sbin/keepalived /sbin/

对keepalived的配置

vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 { ##vrrp虚拟路由协议 14 state MASTER ##备机改为BACKUP,此状态是由priority的值来决定的,当前的优先级值小于备机的值,则将会失去MASTER状态 15 interface eth0 ##HA监测网络接口,用于心跳信息的传递 16 virtual_router_id 7 ##主备机的虚拟路由id必须相同,取值0-255 17 priority 100 ##主机的优先级,备份机改为90,主机优先级一定大于 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.25.254.222 } }
virtual_server 172.25.254.222 3306 { delay_loop 3 lb_algo rr lb_kind DR # nat_mask 255.255.255.0 # persistence_timeout 50 protocol TCP real_server 172.25.254.189 3306{ weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 172.25.254.224 3306{ weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }

对两台real server的mysql做主从复制,然后开启服务,具体的主从复制步骤可以查看mysql的笔记

 sh /etc/lvs.sh start
/etc/init.d/mysqld start

调度端

 [root@server5 mail]# /etc/init.d/keepalived restart

Stopping keepalived:                                       [  OK  ]

Starting keepalived:                                       [  OK  ]

[root@server5 mail]# ipvsadm -L

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  172.25.254.111:http rr

  -> localhost:http               Local   1      0          0

TCP  172.25.254.222:mysql rr

  -> sky.example.com:mysql        Route   1      0          0

  -> server0.example.com:mysql    Route   1      0          0

常见问题

1)为什么同一个IP的某一个时间段访问的都分配到同一个real server.

LVS的持久链接有两种,持久时间工作原理

当一个新的请求进来的时候,就创建了一个此IP的信息,并添加一个state为none的记录,该记录有效期会轮询至此ip端的链接信息的超时退出DIRCTOR。

2) RealServer 为什么要在 lo 接口上配置 VIP,在出口网卡上配置 VIP 可以吗?

既然要让 RS 能够处理目标地址为 vip 的 IP 包,首先必须要让 RS 能接收到这个包。在 lo 上配置 vip 能够完成接收包并将结果返回 client。

答案是不可以将 VIP 设置在出口网卡上,否则会响应客户端的 arp request,造成 client/gatewayarp table 紊乱,以至于整个 loadbalance 都不能正常工作

  1. RealServer 为什么要抑制 arp 帧?这个问题在上一问题中已经作了说明,这里结合实施命令进一步阐述。我们在具体实施部署的时候都会作如下调整:
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce

echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore

echo "2">/proc/sys/net/ipv4/conf/all/arp_announce

4)脑裂:

如果实际生产环境中为了避免脑裂问题,需要把BACKUP和MATER的优先级设置的不一样。

05-17 13:44