LVS+Keepalived实现MySQL写高可用和读负载均衡
主库高可用和只读库负载均衡,是在主主高可用方案的基础上做改进,这里只介绍实现细节的不同。
keepalived架构
同一个keepalived配置读写分离2个VIP,可以有两种办法:
-
一种是读写VIP共用同一个VRRP实例。这种方案中,两个VIP会绑定在当前的主库机器上,分别提供读写请求。所有的业务请求都会进入主库,其LVS在分离部分读负载到从库。这个配置维护简单。
-
另一种是读写VIP各自独立VRRP示例。这种方案中,两个VIP可以绑定在主从库机器上。写请求会进入主库,读请求进入从库,从库的LVS做负载均衡,部分读分离到主库。这种配置维护复杂。
架构图如下:
当LVS架构中的director与realserver是同一台机器时,会出现两台director无限循环转发请求的情况。具体分析如下:
a) director1(master1)收到目的地址为VIP数据包。
b) director1经过ip_vs对数据包做负载均衡,部分包转发给realserver2(mater2/director2)。
c) director2收到的转发包,目的地址也是VIP,再经过ip_vs做负载均衡,部分包转发给realserver1(master1/director1)。
解决办法是,director使用iptables,对数据包做标记,LVS对fwm转发而不是直接转发TCP。
IP配置
这里的主备库IP配置如下:
库
|
HA IP
|
HA IP的mac
|
服务VIP
|
主
|
10.0.0.69
|
mac-source
aa:aa:aa:b5:7b:88
|
写:10.0.0.68
读:10.0.0.140
|
备
|
10.0.0.70
|
mac-source aa:aa:aa:53:3e:28
|
iptables配置
每台director上,对非其他director发送的数据包,做标记,ip_vs转发这些被标记的包。亦即其他director发送的数据包,不再做ip_vs转发,直接交给上层监听程序。
-
主库上:
-
iptables -t mangle -I PREROUTING -d 10.0.0.140 -p tcp -m tcp --dport 3306 -m mac ! --mac-source aa:aa:aa:53:3e:28 -j MARK --set-mark 0x1
-
备库上:
-
iptables -t mangle -I PREROUTING -d 10.0.0.140 -p tcp -m tcp --dport 3306 -m mac ! --mac-source aa:aa:aa:b5:7b:88 -j MARK --set-mark 0x2
realserver配置
有读请求负载均衡后,就需要配置realserver了。
开启realserver的办法:sh lvs_realserver.sh start
-
vim lvs_realserver.sh
-
-
#!/bin/bash
-
#description : start realserver
-
VIP='10.0.0.140 10.0.0.68'
-
/etc/rc.d/init.d/functions
-
case "$1" in
-
start)
-
echo " start LVS of REALServer"
-
for ip in $VIP;do
-
/sbin/ifconfig lo:0 $ip broadcast $ip netmask 255.255.255.255 up
-
done
-
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 "close LVS Directorserver"
-
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
keepalived配置。10.0.0.69为例
-
global_defs {
-
router_id LVS_1
-
}
-
vrrp_sync_group mysql_ha {
-
group {
-
mysql_lb
-
}
-
notify_backup "/usr/local/keepalived_script/keepalived_notify.sh BACKUP"
-
notify_master "/usr/local/keepalived_script/keepalived_notify.sh MASTER"
-
notify_fault "/usr/local/keepalived_script/keepalived_notify.sh FAULT"
-
notify "/usr/local/keepalived_script/keepalived_notify.sh"
-
}
-
-
vrrp_instance mysql_master_ha_slave_lb {
-
state BACKUP
-
interface eth0
-
virtual_router_id 61
-
priority 150
-
nopreempt
-
advert_int 1
-
authentication {
-
auth_type PASS
-
auth_pass 1111
-
}
-
virtual_ipaddress {
-
10.0.0.68
-
10.0.0.140
-
}
-
}
-
## 写VIP virtual_server,只配置本地机器
-
virtual_server 10.0.0.68 3306 {
-
delay_loop 2
-
lb_algo rr
-
lb_kind DR
-
nat_mask 255.255.255.0
-
persistence_timeout 20
-
protocol TCP
-
real_server 10.0.0.69 3306 {
-
weight 3
-
notify_down "/usr/local/keepalived_script/keepalived_notify.sh MYSQL_DOWN"
-
TCP_CHECK {
-
connect_timeout 10
-
connect_port 3306
-
}
-
}
-
}
-
## 读VIP,配置fwmark转发
-
virtual_server fwmark 1 { # 另一台备库是fwmark 2
-
delay_loop 2
-
lb_algo wrr
-
lb_kind DR
-
nat_mask 255.255.255.0
-
persistence_timeout 20
-
protocol TCP
-
real_server 10.0.0.69 3306 {
-
weight 1
-
TCP_CHECK {
-
connect_timeout 10
-
connect_port 3306
-
}
-
}
-
real_server 10.0.0.70 3306 {
-
weight 5
-
TCP_CHECK {
-
connect_timeout 10
-
connect_port 3306
-
}
-
}
-
}
阅读(1427) | 评论(0) | 转发(0) |