目录
环境:
一、配置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 ]thenecho "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 $LOCKecho "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/nullrm -rf $LOCKecho "stopping LVS-DR server is ok !"
}status(){if [ -e $LOCK ];thenecho "The LVS-DR Server is already running !"elseecho "The LVS-DR Server is not running !"fi
}case "$1" instart)start;;stop)stop;;restart)stopstart;;status)status;;
*)echo "Usage: $1 {start|stop|restart|status}"exit 1
esac
exit 0chmod +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 ]thenecho "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}:20echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" > /proc/sys/net/ipv4/conf/lo/arp_announceecho "1" > /proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" > /proc/sys/net/ipv4/conf/all/arp_announce/bin/touch $LOCKecho "starting LVS-DR-RIP Server is ok!"fi}stop(){/sbin/route del -host $VIP dev ${DipName}:20/sbin/ifconfig ${DipName}:20 down > /dev/nullecho "0" > /proc/sys/net/ipv4/conf/lo/arp_ignoreecho "0" > /proc/sys/net/ipv4/conf/lo/arp_announceecho "0" > /proc/sys/net/ipv4/conf/all/arp_ignoreecho "0" > /proc/sys/net/ipv4/conf/all/arp_announcerm -rf $LOCKecho "stopping LVS-DR-RIP server is ok !"
}status(){if [ -e $LOCK ];thenecho "The LVS-DR Server is already running !"elseecho "The LVS-DR Server is not running !"fi
}case "$1" instart)start;;stop)stop;;restart)stopstart;;status)status;;
*)echo "Usage: $1 {start|stop|restart|status}"exit 1
esac
exit 0chmod +x /etc/init.d/LVS_RS
chkconfig --add /etc/init.d/LVS_RS