Shell脚本范例(二十四)——LVS客户端配置

本文分享常用的一些Shell脚本的例子,这是第二十四篇——开发LVS节点健康检查及管理脚本。

要求:请在LVS客户端节点上,开发LVS客户端设置VIP及抑制ARP的管理脚本。实现:/etc/init.d/lvsclient {start|stop|restart}。

参考:

#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

RETVAR=0
VIP=(
10.0.0.3
10.0.0.4
)

. /etc/init.d/functions

case "$1" in
    start)
        for ((i=0;i<`echo ${#VIP[*]}`;i++))
        do
            interface="lo:`echo ${VIP[$i]}|awk -F . '{print $4}'`"
            /sbin/ip addr add ${VIP[$i]}/24 dev lo label $interface
            REVTAR=$?
        done
        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
        if [ $RETVAR -eq 0 ]
        then
            action "Start LVS Config of RealServer." /bin/true
        else
            action "Start LVS Config of RealServer." /bin/false
        fi
        ;;
    stop)
        for((i=0;i<`echo ${#VIP[$i]}`;i++))
        do
            interface="lo:`echo ${VIP[$i]}|awk -F . '{print $4}'`"
            /sbin/ip addr del ${VIP[$i]}/24 dev lo label $interface &> /dev/null
        done
        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/all/arp_ignore
        echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
        if [ $RETVAR -eq 0 ]
        then
            action "Close LVS Config of RealServer." /bin/true
        else
            action "Close LVS Config of RealServer." /bin/false
        fi
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
esac
exit $RETVAR

标签: none