首页 > 代码库 > LVS+Keepalived搭建MyCAT高可用負載均衡集群

LVS+Keepalived搭建MyCAT高可用負載均衡集群

1.前面我们已经搭建好mysql主主,并且用mycat实现双写功能,主要配置文件:

[root@mycat2 conf]# cat schema.xml
<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="test" checkSQLschema="false" sqlMaxLimit="100" dataNode="dn1">
</schema>
<dataNode name="dn1" dataHost="192.168.6.163" database="infosys" />
<dataHost name="192.168.6.163" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<!-- can have multi write hosts -->
<writeHost host="192.168.6.163" url="192.168.6.163:3306" user="root"
password="******" >
</writeHost>
<writeHost host="192.168.6.167" url="192.168.6.167:3306" user="root" password="*******" >
</writeHost>
</dataHost>
</mycat:schema>

两边都一样:

2.安裝keepalived和ipvsadm

安装原理很简单,直接给配置:

[root@java1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 172.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.6.113
}
}

virtual_server 192.168.6.113 8066 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.248.0
persistence_timeout 50
protocol TCP

real_server 192.168.6.155 8066 {
weight 1
TCP_CHECK { #realserver的状态检测设置部分,单位是秒
connect_timeout 3 #表示3秒无响应超时
nb_get_retry 3 #表示重试次数
delay_before_retry 3 #表示重试间隔
connect_port 8066
}
}
real_server 192.168.6.168 8066 {
weight 1
TCP_CHECK { #realserver的状态检测设置部分,单位是秒
connect_timeout 3 #表示3秒无响应超时
nb_get_retry 3 #表示重试次数
delay_before_retry 3 #表示重试间隔
connect_port 8066
}
}
}

第二台机器稍微改动一下  priority 99 #修改優先順序

3.在两台Mycat上面执行脚本:

 

#!/bin/bash
#description: Config realserver

VIP=192.168.244.10
 
/etc/rc.d/init.d/functions
 
case "$1" in
start)
       /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
       /sbin/route add -host $VIP dev lo:0
       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
       sysctl -p >/dev/null 2>&1
       echo "RealServer Start OK"
       ;;
stop)
       /sbin/ifconfig lo:0 down
       /sbin/route del $VIP >/dev/null 2>&1
       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 "RealServer Stoped"
       ;;
*)
       echo "Usage: $0 {start|stop}"
       exit 1
esac
 
exit 0
这个直接分配vip
4.都启动起来验证:

技术分享

只要是两台互补的,掉了其中一台,都可以正常使用mysql服务,避免单点儿故障,好简单对吧!!!!不怕从0开始,就怕不开始。

LVS+Keepalived搭建MyCAT高可用負載均衡集群