1.部署前说明
VIP:172.17.100.55 (DIR服务器链接外网的地址)
DIR服务器:172.17.100.55
Real-server(提供http服务):172.17.100.60 172.17.100.61
2.在DIR服务器上面安装ipvsadm软件包以及相关的依赖
Yum install openssl-devel popt-devel libnl-devel ipvsadm -y
3.vim /etc/init.d/lvs-nat
脚本内容为
LOCK=/var/lock/ipvsadm.lock
VIP=172.17.100.55
RIP1=172.17.100.60
RIP2=172.17.100.61
. /etc/rc.d/init.d/functions
start() {
PID=`ipvsadm -Ln | grep ${VIP} | wc -l`
if [ $PID -gt 0 ];
then
echo "The LVS-NAT Server is already running !"
else
echo "1" >/proc/sys/net/ipv4/ip_forward
echo "0" >/proc/sys/net/ipv4/conf/all/send_redirects
echo "0" >/proc/sys/net/ipv4/conf/default/send_redirects
echo "0" >/proc/sys/net/ipv4/conf/ens160/send_redirects
#echo "0" >/proc/sys/net/ipv4/conf/ens192/send_redirects
/bin/touch $LOCK
#Clear IPVS table
/sbin/ipvsadm -C
#set LVS
/sbin/ipvsadm -At $VIP:80 -s rr
/sbin/ipvsadm -at $VIP:80 -r $RIP1:8080 -m -w 1
/sbin/ipvsadm -at $VIP:80 -r $RIP2:8080 -m -w 1
echo "starting LVS-NAT Server is ok!"
fi
}
stop() {
echo "0" >/proc/sys/net/ipv4/ip_forward
echo "1" >/proc/sys/net/ipv4/conf/all/send_redirects
echo "1" >/proc/sys/net/ipv4/conf/default/send_redirects
echo "1" >/proc/sys/net/ipv4/conf/ens160/send_redirects
#echo "1" >/proc/sys/net/ipv4/conf/ens192/send_redirects
/sbin/ipvsadm -C
rm -rf $LOCK
echo "stopping LVS-NAT server is ok!"
}
status() {
if [ -e $LOCK ];
then
echo "The LVS-NAT Server is already running !"
else
echo "The LVS-NAT 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
4.授权并启动该脚本
Chmod 777 /etc/init.d/lvs-nat
Service lvs-nat start
5.后端的real-server(172.17.100.60 172.17.100.61)的网关统一指向DIR内网地址IP。
Vim /etc/sysconfig/network-scripts/ifcfg-ens160
修改GATEWAY = 172.17.100.55
改完后执行 /etc/init.d/network restart
6.测试负载均衡
Curl http://172.17.100.55/test