Maison > Article > base de données > LVS+Mysql集群负载均衡
一、IP信息列表: 名称 IP LVS-DR-VIP 192.168.50.245 LVS-DR-Master 192.168.50.216 LVS-DR-BACKUP 192.168.50.217 MySQL1-Realserver 192.168.50.212 MySQL2-Realserver 192.168.50.213 GateWay 192.168.50.254 其他(部署LVS过程中无需配置): MGM管理节
一、IP信息列表:
名称 IP
LVS-DR-VIP 192.168.50.245
LVS-DR-Master 192.168.50.216
LVS-DR-BACKUP 192.168.50.217
MySQL1-Realserver 192.168.50.212
MySQL2-Realserver 192.168.50.213
GateWay 192.168.50.254
其他(部署LVS过程中无需配置):
MGM管理节点 192.168.50.211
NDBD存储节点1 192.168.50.214
NDBD存储节点2 192.168.50.215
一. 安装LVS和Keepalvied软件包
1. 下载相关软件包
#cd /usr/local/src/tarbag
#wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
#wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
2. 安装LVS和Keepalived
#lsmod |grep ip_vs
#uname -r
2.6.18-128.el5
#ln -s /usr/src/kernels/2.6.18-128.el5-i686/ /usr/src/linux
//这步一定要做,否则ipvsadm编译会报错
#tar zxvf ipvsadm-1.24.tar.gz
#cd ipvsadm-1.24
#make && make install
#tar zxvf keepalived-1.1.15.tar.gz
#cd keepalived-1.1.15
#./configure && make && make install
#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
#mkdir /etc/keepalived
#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
#cp /usr/local/sbin/keepalived /usr/sbin/
#chkconfig add keepalived
#chkconfig keepalived on #做成系统服务
#service keepalived start|stop|status
二、配置lvs与RealServer脚本
1.lvs脚本(主备两台LVS上操作)
#vi /usr/local/sbin/lvs-dr.sh
//以下为脚本内容
#!/bin/bash
# description: start LVS of DirectorServer
#Written by :NetSeek http://www.linuxtone.org
GW=192.168.50.254 #当前局域网网关ip
# website director vip.
MySQL_VIP=192.168.50.246 #MySQL虚拟IP地址
MySQL_RIP1=192.168.50.212
MySQL_RIP2=192.168.50.213
/etc/rc.d/init.d/functions
logger $0 called with $1 #记录到系统日志messages
case "$1" in
start)
# set squid vip
/sbin/ipvsadm --set 30 5 60 #分别为协议tcp tcpfin udp 的超时时间
/sbin/ifconfig eth0:0 $MySQL_VIP broadcast $MySQL_VIP netmask 255.255.255.255 broadcast $MySQL_VIP up #新建VIP
/sbin/route add -host $MySQL_VIP dev eth0:0 #为VIP添加路由
/sbin/ipvsadm -A -t $MySQL_VIP:3306 -s wrr -p 3 #添加一个VIP
/sbin/ipvsadm -a -t $MySQL_VIP:3306 -r $MySQL_RIP1:3306 -g -w 1 #将VIP:3306的请求转发到RIP1上,-g表示使用DR模式,-w表示权重为1
/sbin/ipvsadm -a -t $MySQL_VIP:3306 -r $MySQL_RIP2:3306 -g -w 1
touch /var/lock/subsys/ipvsadm >/dev/null 2>&1 #新建文件ipvsadm,主要用来表示服务器已经启动,为status做准备
;;
stop)
/sbin/ipvsadm -C #清空LVS所有规则
/sbin/ipvsadm -Z #所有服务器计数器清零
ifconfig eth0:0 down #禁用虚拟网卡eth0:0
route del $MySQL_VIP #删除VIP的路由
rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1
echo "ipvsadm stoped"
;; #相当于C语言中的break,跳出case
status)
if [ ! -e /var/lock/subsys/ipvsadm ];then #如果ipvsadm不存在则输出ipvsadm stoped
echo "ipvsadm stoped"
exit 1
else
echo "ipvsadm OK"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit 0
2.RealServer脚本(所有MySQL上操作)
#vi /usr/local/sbin/realserver.sh
#!/bin/bash
# description: Config realserver lo and apply noarp
#Written by :NetSeek http://www.linuxtone.org
MySQL_VIP=192.168.50.245 #虚拟IP
/etc/rc.d/init.d/functions #这个作用暂时不知道,知道的同学帮忙修改一下
case "$1" in
start)
ifconfig lo:0 $MySQL_VIP netmask 255.255.255.255 broadcast $MySQL_VIP #新建虚拟ip,LVS中所有的VIP都一致
/sbin/route add -host $MySQL_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 $MySQL_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
三、启动相关服务
1.主备LVS
#chmod 755 /usr/local/sbin/lvs-dr.sh #赋予执行权限
#/usr/local/sbin/lvs-dr.sh #启动lvs
#service keepalived start #启动keepalived服务
2.MySQL Cluster中的2台SQL节点
#cd /usr/local/sbin
#chmod 755 realserver.sh
#./realserver.sh
四、测试
方法1:
#telnet 192.168.50.245 3306
N
5.1.39-ndb-7.0.9-cluster-gpl-log菔1sti5i5+;*^{qFI{OWJ
//出现以上内容说明请求转发成功
方法二:
使用mysql的各种客户端工具连接mysql。