keepalived+LVS+MySql Cluster 高可用配置
说明:
keepalived+LVS 实现SQL节点的负载均衡及故障转移,另 mysql cluster sql节点无单点故障,myql cluster 数据节点本身无单点故障问题!
网上资料很多,杂乱无章,走了很多弯路,特此总结!
测试具体如下:
1,IP及角色规划
192.168.10.100:MGM/SQL1
192.168.10.2:NDBD1/lvs+keepalived(主机)
192.168.10.3:NDBD2/lvs+keepalived(备机)
192.168.10.20: SQL2 (LVS节点)
192.168.10.30: SQL3 (LVS节点)
VIP:192.168.10.200 // 此为虚拟IP, client端用此IP连接mysql数据库,替代SQL节点的实ip
2, 软件环境:
vmware 12 + centos6.8
keepalived-1.2.1
ipvsadm-1.26-4.el6.x86_64
5.6.31-ndb-7.4.12-cluster-gpl MySQLCluster Community Server (GPL) #mysql cluster请自行配置,不在此文讨论范围
3, lvs+keepalived角色配置
3.1.1 lvs+keepalived(主机+备机)配置 192.168.10.2 192.168.10.3
#两台keepalived主机机软件环境相同,区别在于/etc/keepalived/keepalived.conf文件的配置不同,即以下两个值不同,其它全部一致
state BACKUP(MASTER)
priority 100(150)
#安装keepalived-1.2.1,源码安装,自行到网站下载;安装ipvsadm,yum安装
#编译所需包及ipvsadm等安装环境设置
[root@localhost]#yum groupinstall "Additional Development" -y
[root@localhost]#yum groupinstall "Development tools" -y
[root@localhost]#yum install kernel-devel ipvsadm -y
[root@localhost]# uname -r
2.6.32-642.el6.x86_64
[root@localhost]#ln -s /usr/src/kernels/2.6.32-642.el6.x86_64/ /usr/src/linux
[root@localhost]# ./configure
##显示以下信息为安装正确
Keepalived configuration
------------------------
Keepalived version : 1.2.1
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : No
IPVS sync daemon support : No
Use VRRP Framework : Yes
Use Debug flags : No
[root@localhost]# make & make install
[root@localhost]#ll /usr/local/etc //keepalived默认安装路径
drwxr-xr-x. 3 root root 4096 May 2400:37 keepalived
drwxr-xr-x. 3 root root 4096 May 2400:29 rc.d
drwxr-xr-x. 2 root root 4096 May 2400:29 sysconfig
[root@localhost]#lsmod |grep ip_vs //查看模块是否加载成功
ip_vs_rr 1420 1
ip_vs 126897 3 ip_vs_rr
libcrc32c 1246 1 ip_vs
ipv6 336282 281 ip_vs
##配置以系统方式service启动
[root@localhost]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
[root@localhost]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost]# cp /usr/local/sbin/keepalived /usr/sbin
[root@localhost]#chkconfig --add keepalived
[root@localhost]#chkconfig keepalived on
##配置文件设置: /etc/keepalived/keepalived.conf
[root@localhost]# mkdir /etc/keepalived/
[root@localhost]#vi /etc/keepalived/keepalived.conf
配置文件内容如下:
#guration File forkeepalived
#global define
global_defs {
router_id HaMySQL_1
}
vrrp_sync_group VGM {
group {
VI_MYSQL
}
}
vrrp_instance VI_MYSQL {
state MASTER //LB-Backup设置为BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 55
priority 100 //LB-Backup设置为90
advert_int 5
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.10.200/24 dev eth0
}
}
########## LVS MySQL Start ###########
virtual_server192.168.10.200 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 6
protocol TCP
real_server 192.168.10.20 3306 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
real_server 192.168.10.30 3306 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
########## LVS MySQL END #############
3.1.2 ipvsadm配置 192.168.10.2 192.168.10.3
#yum安装 ipvsadm
[root@localhost]yum install ipvsadm -y
#设定配置文件
[root@localhost]vi/etc/sysconfig/ipvsadm
#ipvsadm文件配置如下:
-C
-A -t 192.168.10.200:3306-s rr
-a -t 192.168.10.200:3306-r 192.168.10.20:3306 -g -w 100
-a -t 192.168.10.200:3306-r 192.168.10.30:3306 -g -w 100
常用命令:
service keepalived start
service ipvsadm start
ipvsadm -ln
ipvsadm -lnc
4. realserver配置 192.168.10.20 192.168.10.30
#新建lvsrs文件
[root@localhost]vi/etc/init.d/lvsrs
#配置内容如下:
#!/bin/bash
# description: Configrealserver lo and apply noarp
SNS_VIP=192.168.10.200
/etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask255.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/null2>&1
echo "0">/proc/sys/net/ipv4/conf/lo/arp_ignoreecho 'test 22' >> /etc/rc.local
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
#增加开机启动
[root@localhost]echo 'service lvsrs start' >> /etc/rc.local
#测试发现两台SQL节点必须同时在线,如果只有一台在线的话,切换到备机时,可能无法连线(实际telnet3306端口是通的),目前没找到问题所在。