目录
环境:
一、配置RS
1、安装常见软件
2、配置web服务
3、添加vip
4、arp抑制
二、配置LVS
1、添加vip
2、安装配置工具
3、配置DR
三、测试
四、脚本方式配置
1、LVS-DR
2、LVS-RS
环境:
master lvs 192.168.80.161
node1 rs1 192.168.80.162
node2 rs2 192.168.80.163
VIP:192.168.80.10/32
一、配置RS
1、安装常见软件
yum install vim bash-completion wget tar net-tools lrzsz tree unzip psmisc dos2unix -y
2、配置web服务
yum install nginx -y
cd /usr/share/nginx/heml/
mv index.html index.html.bak
echo "web test page, ip is `hostname -I`." > index.html #修改默认主页
systemctl start nginx
3、添加vip
ifconfig lo:20 192.168.80.10 netmask 255.255.255.255
4、arp抑制
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
二、配置LVS
1、添加vip
ifconfig lo:20 192.168.80.10 netmask 255.255.255.255
2、安装配置工具
yum install ipvsadm -y
3、配置DR
ipvsadm -A -t 192.168.80.10:80 -s rr
ipvsadm -a -t 192.168.80.10:80 -r 192.168.80.162
ipvsadm -a -t 192.168.80.10:80 -r 192.168.80.163
【相关参数】
-A 添加虚拟服务器
-t 设置集群地址(vip)
-s 指定负载调度算法
-a 添加真实服务器
-d 删除真实服务器
-r 指定真实服务器
-m 使用nat模式:-g、-i分别对应DR、TUN模式
-w 为节点服务器设置权重,默认为1
保存:
ipvsadm -Sn > /etc/sysconfig/ipvsadm
三、测试
四、脚本方式配置
1、LVS-DR
vim /etc/init.d/LVS_DR
#!/bin/sh
#chkconfig: 2345 80 90
#description:auto_run
LOCK=/var/lock/ipvsadm.lock
VIP=192.168.80.10
RIP1=192.168.80.162
RIP2=192.168.80.163
DipName=ens33
. /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
/sbin/ifconfig ${DipName}:20 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev ${DipName}:20
/sbin/ipvsadm -C
/sbin/ipvsadm -At $VIP:80 -s rr
/sbin/ipvsadm -at $VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -at $VIP:80 -r $RIP2:80 -g
/bin/touch $LOCK
echo "starting LVS-DR Server is ok!"
fi
}
stop(){
/sbin/ipvsadm -C
/sbin/route del -host $VIP dev ${DipName}:20
/sbin/ifconfig ${DipName}:20 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
chmod +x /etc/init.d/LVS_DR
chkconfig --add /etc/init.d/LVS_DR
2、LVS-RS
vim /etc/init.d/LVS_RS
#!/bin/sh
#chkconfig: 2345 80 90
#description:auto_run
LOCK=/var/lock/ipvsadm.lock
VIP=192.168.80.10
DipName=lo
. /etc/rc.d/init.d/functions
start(){
PID=`ifconfig | grep ${DipName}:20 | wc -l`
if [ $PID -ne 0 ]
then
echo "The LVS-DR-RIP Server is already running!"
else
/sbin/ifconfig ${DipName}:20 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev ${DipName}:20
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
/bin/touch $LOCK
echo "starting LVS-DR-RIP Server is ok!"
fi
}
stop(){
/sbin/route del -host $VIP dev ${DipName}:20
/sbin/ifconfig ${DipName}:20 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/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 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
chmod +x /etc/init.d/LVS_RS
chkconfig --add /etc/init.d/LVS_RS