简介:今天搭建下mycat的高可用架构
一 准备环境:
1 两台配置相同的mycat,准备做负载均衡
2 两台安装lvs+keeplived的服务器,准备做高可用
3 两台mysql单实例,做分片集群
二 IP叙述
mycat1 192.168.1.1 keepalived1 192.168.1.3 mysql1 192.168.1.5 VIP 192.168.1.8
mycat2 192.168.1.2 keepalived2 192.168.1.4 mysql2 192.168.1.6
三 架构描述
lvs-keepalived=>mycat集群=>mysql分片集群
四 搭建过程
1 配置文件
1 keepalived1
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS1
}
# db Read
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.1.8/24
}
}
# VIP 192.168.1.8
virtual_server 192.168.1.8 8066 {
delay_loop 10
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 192.168.1.3 8066 {
weight 1
TCP_CHECK {
connect_port 8066
connect_timeout 10
nb_get_retry 3
delay_before_retry 5
}
}
real_server 192.168.1.4 8066 {
weight 1
TCP_CHECK {
connect_port 8066
connect_timeout 10
nb_get_retry 3
delay_before_retry 5
}
}
}
从更改 state=BACKUP,priority=80即可
2 启动keepalived服务
3 ipvsadm -ln 进行VIP和后端real server查看,都存在则代表正常
4 后端real server 进行VIP绑定设置
#!/bin/bash
# description: Config realserver lo and apply noarp
SNS_VIP=192.168.1.8
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_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)
ifconfig lo:0 down
route del $SNS_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
五 进行测试
mysql -umycat -p123456 -h192.168.1.8 -P 8066 -e ‘show @@hostname‘
两个mycat节点的hostname是不相同的,可以测试出是否成功
六 相关补充
1 本架构采用的轮训(RR)算法 ,DR模式
2 对于mycat做负载均衡是非常有必要的,因为mycat是程序与mysql 交互的唯一通道
3 检测后端可用性采用的是TCP+PORT方式,后续会介绍使用misc_check方式检测可用性