基于 CentOS 7 构建 LVS-DR 群集(2)脚本方式

 内容续  基于 CentOS 7 构建 LVS-DR 群集(1)_spring℡的博客-CSDN博客

目录

一、第一台虚拟机(192.168.157.10)

1、编写lvs脚本

2、lvs_dr设置权限

二、第二台电脑和第二台电脑

1、编写lvs_rs

2、设置 lvs_rs权限为755

3、启动 lvs_rs


一、第一台虚拟机(192.168.157.10)

1、编写lvs脚本

删除之前写的集群  ipvsadm -C

vim /etc/init.d/lvs_dr 
#!/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
LOCK=/var/lock/ipvsadm.lock
VIP=192.168.157.10
RIP1=192.168.157.200
RIP2=192.168.157.201
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 rr
         /sbin/ipvsadm -at $VIP:80 -r $RIP1:80 -g
         /sbin/ipvsadm -at $VIP:80 -r $RIP2:80 -g
         /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

2、lvs_dr设置权限

chmod +x /etc/init.d/lvs_dr

3、 启动lvs_dr

chkconfig --add lvs_dr
systemctl start lvs_dr

如果修改过lvs_dr需要在重启之前输入如下

systemctl daemon-reload

二、第二台电脑和第二台电脑

手工方式写过route需要重启network

systemctl restart network

1、编写lvs_rs

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
# balancer running on Linux.
# description: start LVS of DR-RIP
LOCK=/var/lock/ipvsadm.lock
VIP=192.168.157.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/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/eth0/arp_ignore
        echo "2" >/proc/sys/net/ipv4/conf/eth0/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/eth0/arp_ignore
        echo "0" >/proc/sys/net/ipv4/conf/eth0/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

2、设置 lvs_rs权限为755

3、启动 lvs_rs

chkconfig --add lvs_rs
systemctl start lvs_rs

你可能感兴趣的:(#,重点,centos,linux,lvs)