虚拟化容器,大数据,DBA,中间件,监控。

CentOS6.4 配置LVS(DR模式)

14 10月
作者:admin|分类:系统运维

CentOS6.4 配置LVS(DR模式)

DR模式中LVS主机与实际服务器都有一块网卡连在同一物理网段上。

IP分配

VIP:10.10.3.170

RIP1:10.10.3.140

RIP2:10.10.3.141

1、安装所需的依赖包

yum install -y wget make kernel-devel gcc gcc-c++ libnl* libpopt* popt-static

2、创建一个软链接,防止后面编译安装ipvsadm时找不到系统内核

ln -s /usr/src/kernels/2.6.32-358.18.1.el6.x86_64/ /usr/src/linux

3、下载安装ipvsadm

wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
tar zxvf ipvsadm-1.26.tar.gz
cd ipvsadm-1.26
make && make install

4、编写并运行脚本(LVS服务器的脚本)

vi lvs.sh
复制代码
#!/bin/bash
# 2013-09-07 by kgdxpr

VIP=10.10.3.170
RIP1=10.10.3.140
RIP2=10.10.3.141

/etc/rc.d/init.d/functions

logger $0 called with $1
case "$1" in

start)
echo " start LVS of DirectorServer"
/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:0
echo "1" >/proc/sys/net/ipv4/ip_forward

#Clear IPVS table
/sbin/ipvsadm -C
#set LVS
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g

#Run LVS
/sbin/ipvsadm
;;
stop)
echo "close LVS Directorserver"
echo "0" >/proc/sys/net/ipv4/ip_forward
/sbin/ipvsadm -C
/sbin/ifconfig eth0:0 down
;;
*)

echo "Usage: $0 {start|stop}"
exit 1
esac
复制代码

注:/sbin/ipvsadm -A -t $VIP:80 -s rr (rr代表轮询,还有分配方式)

/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g (-g代表DR模式,还有其他模式)

5、给脚本加权限,并执行

chmod +x lvs.sh
chmod 755 /etc/rc.d/init.d/functions
./lvs.sh start

6、配置后端的WEB服务器脚本

vi realserver.sh
复制代码
#!/bin/bash
# 2013-09-07 by kgdxpr
VIP=10.10.3.170
/etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of REALServer"
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 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/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
;;
stop)
/sbin/ifconfig lo:0 down
echo "close LVS Directorserver"
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 "Usage: $0 {start|stop}"
exit 1
esac
复制代码

 7、给脚本加权限,并执行 

chmod +x realserver.sh
chmod 755 /etc/rc.d/init.d/functions
./realserver.sh start
浏览1741 评论0
返回
目录
返回
首页
CentOS6.5 LVS + KeepAlived搭建步骤 CentOS 6.3部署LVS(NAT)+keepalived实现高性能高可用负载均衡