LVS-DR服务器的 Shell 脚本

#!/bin/bash

LOCK=/var/lock/ipvsadm.lock
VIP=172.16.100.20

. /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/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() {
    PID=$(ifconfig | grep lo:10 | wc -l)
    if [ $PID -eq 0 ]; then
        echo "The LVS-DR-RIP Server is not running!"
    else
        /sbin/ifconfig lo:10 down
        /sbin/route del -host $VIP dev lo:10
        rm -rf $LOCK
        echo "Stopping LVS-DR-RIP server is ok!"
    fi
}

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: $0 {start | stop | restart | status}"
        exit 1
        ;;
esac

exit 0

#!/bin/bash

LOCK=/var/lock/ipvsadm.lock
VIP=172.16.100.20
RIP1=172.16.100.21
RIP2=172.16.100.22
DipName=ens18

. /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}:0 $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 -A -t $VIP:80 -s rr
        /sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g
        /sbin/ipvsadm -a -t $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}:20
    /sbin/ifconfig ${DipName}:2 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: $0 {start | stop | restart | status}"
        exit 1
        ;;
esac

exit 0

你可能感兴趣的:(运维高级篇,java,前端,网络)