web集群学习:基于CentOS 7构建 LVS-DR 群集并配置服务启动脚本

目录

1、环境准备

2、配置lvs服务启动脚本

1、在RS上分别配置服务启动脚本

2、在lvs director上配置服务启动脚本

3、客户端测试


配置LVS-DR模式主要注意的有
1、vip绑定在RS的lo接口;
2、RS做arp抑制;

1、环境准备

VIP=192.168.95.10

RS1=192.168.95.11

RS2=192.168.95.12

网络接口:ens33

在两台RS上分别准备web访问环境

cd /usr/share/nginx/html/
echo "web test page,`hostname -I`." > index.html 
systemctl restart nginx

2、配置lvs服务启动脚本

1、在RS上分别配置服务启动脚本

[root@node3 ~]# vim /etc/init.d/lvs_rs
#!/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.95.10
. /etc/rc.d/init.d/functions
start() {
     PID=`ifconfig | grep lo:10 | wc -l`
     if [ $PID -ne 0 ];
     then
         echo "The LVS-DR-RIP Server is already running !"
     else
         /sbin/ifconfig lo:10 $VIP netmask 255.255.255.255 broadcast $VIP up
         /sbin/route add -host $VIP dev lo:10
         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/ens33/arp_ignore
         echo "2" >/proc/sys/net/ipv4/conf/ens33/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:10
         /sbin/ifconfig lo:10 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/ens33/arp_ignore
         echo "0" >/proc/sys/net/ipv4/conf/ens33/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_rs的自启动

chmod +x /etc/init.d/lvs_rs 
chkconfig --add lvs_rs
service lvs_rs start 

将配置好的脚本发送给另一台web服务器并开启

scp /etc/init.d/lvs_rs 192.168.80.12:/etc/init.d/

2、在lvs director上配置服务启动脚本

vim /etc/init.d/lvs_dr 
# 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.95.10
RIP1=192.168.95.11
RIP2=192.168.95.12
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
           #Set the Virtual IP Address
           /sbin/ifconfig ${DipName}:10 $VIP broadcast $VIP netmask 255.255.255.255 up
           /sbin/route add -host $VIP dev ${DipName}:10
           #Clear IPVS Table
           /sbin/ipvsadm -C
           #Set Lvs
           /sbin/ipvsadm -At $VIP:80 -s wrr
           /sbin/ipvsadm -at $VIP:80 -r $RIP1:80 -g -w 2
           /sbin/ipvsadm -at $VIP:80 -r $RIP2:80 -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 ${DipName}:10
           /sbin/ifconfig ${DipName}:10 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

开启lvs_rs的自启动

chmod +x /etc/init.d/lvs_dr 
chkconfig --add lvs_dr
service lvs_rs start 

相关参数及说明

ipvsadm –help
-A 添加虚拟服务器
-t 设置群集地址(VIP,Virtual IP)
-s 指定负载调度算法
-a 添加真实服务器
-d 删除真实服务器
-r 指定真实服务器(Real Server)的地址
-m 使用NAT模式;-g、-i分别对应DR、TUN模式
-w 为节点服务器设置权重,默认为1

3、客户端测试

for ((i=1;i<=6;i++)); do curl 192.168.95.10; done

你可能感兴趣的:(lvs,centos,shell,负载均衡)