采用LVS DR模式做负载均衡。
环境为LightDB分布式环境(虚拟机模拟搭建)
2cn,2dn如下图所示:
其中137为主(primary),140为备(standby)
1. 首先安装ipvsadm: yum install ipvsadm
, 或使用光盘中rpm包安装。
2. ssh到137上(root账户执行)
准备2个执行脚本
1)lvs_dr.sh
#!/bin/sh
#
# Startup script handle the initialisation of LVS
# chkconfig: - 28 72
# description: Initialise the Linux Virtual Server for DR
#
### BEGIN INIT INFO
# Provides: ipvsadm
# Required-Start: $local_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: Initialise the Linux Virtual Server
# Description: The Linux Virtual Server is a highly scalable and highly
# available server built on a cluster of real servers, with the load
# balancer running on Linux.
# description: start LVS of DR
LOCK=/var/lock/ipvsadm.lock
VIP=192.168.237.200
RIP1=192.168.237.137 # CN1 IP
RIP2=192.168.237.140 # CN2 IP
ethx=ens33
port=5432 # CN1 port
. /etc/rc.d/init.d/functions
start() {
PID=`ipvsadm -Ln | grep ${VIP} | wc -l`
if [ $PID -gt 0 ];
then
echo "The LVS-DR Server is already running !"
else
#Set the Virtual IP Address
/sbin/ifconfig $ethx:1 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev $ethx:1
#Clear IPVS Table
/sbin/ipvsadm -C
#Set Lvs
#echo $VIP:$port
#echo $RIP1:$port
#echo $RIP2:$port
#echo $RIP3:$port
/sbin/ipvsadm -At $VIP:$port -s rr
/sbin/ipvsadm -at $VIP:$port -r $RIP1:$port -g -w 1
/sbin/ipvsadm -at $VIP:$port -r $RIP2:$port -g -w 1
#/sbin/ipvsadm -at $VIP:$port -r $RIP3:$port -g -w 1
/bin/touch $LOCK
#Run Lvs
echo "starting LVS-DR Server is ok !"
fi
}
stop() {
#clear Lvs and vip
/sbin/ipvsadm -C
/sbin/route del -host $VIP dev $ethx:1
/sbin/ifconfig $ethx:1 down >/dev/null
rm -rf $LOCK
echo "stopping LVS-DR server is ok !"
}
status() {
if [ -e $LOCK ];
then
echo "The LVS-DR Server is already running !"
else
echo "The LVS-DR Server is not running !"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $1 {start|stop|restart|status}"
exit 1
esac
exit 0
2) lvs_rs.sh
#!/bin/sh
#
# Startup script handle the initialisation of LVS
# chkconfig: - 28 72
# description: Initialise the Linux Virtual Server for DR
#
### BEGIN INIT INFO
# Provides: ipvsadm
# Required-Start: $local_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: Initialise the Linux Virtual Server
# Description: The Linux Virtual Server is a highly scalable and highly
# available server built on a cluster of real servers, with the load
# balancer running on Linux.
# description: start LVS of DR-RIP
LOCK=/var/lock/ipvsadm.lock
VIP=192.168.237.200
ethx=ens33
. /etc/rc.d/init.d/functions
start() {
PID=`ifconfig | grep lo:0 | wc -l`
if [ $PID -ne 0 ];
then
echo "The LVS-DR-RIP Server is already running !"
else
/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP up
/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/$ethx/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/$ethx/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
/bin/touch $LOCK
echo "starting LVS-DR-RIP server is ok !"
fi
}
stop() {
/sbin/route del -host $VIP dev lo:0
/sbin/ifconfig lo:0 down >/dev/null
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/$ethx/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/$ethx/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
rm -rf $LOCK
echo "stopping LVS-DR-RIP server is ok !"
}
status() {
if [ -e $LOCK ];
then
echo "The LVS-DR-RIP Server is already running !"
else
echo "The LVS-DR-RIP Server is not running !"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $1 {start|stop|restart|status}"
exit 1
esac
exit 0
执行这两个脚本:
./lvs_dr.sh start # Director脚本
./lvs_rs.sh start # RealServer脚本
3. ssh到140上(root账户执行)
准备脚本 lvs_rs.sh(同137上的 lvs_rs.sh)
执行脚本:
./lvs_rs.sh start # RealServer脚本
4. 验证
1)执行ip a
2) 执行ipvsadm -Ln --stats
部署LVS负载均衡(VIP)完成。
详细语法可参考LightDB官网查看https://www.hs.net/lightdb
更多请登录LightDB官网进行查看:https://www.hs.net/lightdb