跳转至

LVS RealServer绑定VIP脚本


2014-04-02 by dongnan

功能

  • RealServer 自动完成绑定VIP地址。
  • RealServer 自动完成忽略对VIParp响应参数设置。

环境

使用环境请参考LVS - Linux虚拟服务器

脚本

# 执行命令
cat /etc/init.d/real_server.sh

#!/bin/bash
### BEGIN INIT INFO
# Provides: lvs_realserver
# Default-Start:  3 4 5
# Default-Stop: 0 1 6
# Short-Description: LVS real_server service scripts
# Description: LVS real_server start and stop controller
### END INIT INFO
#
#  chkconfig: - 20 80
#
#201403 by dongnan

#variables 
#多个虚拟IP以空格分隔,例如:
#VIP="172.272.233.45 10.0.0.45"
VIP="172.27.233.45"

#function

start() {
num=0

for ips in $VIP;do
    /sbin/ifconfig lo:$num $ips netmask 255.255.255.255 broadcast $ips up
    /sbin/route add -host $ips dev lo:$num
#lo
    ((num++))
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

sysctl -e -p > /dev/null 2>&1
}


stop() {
num=0

for ips in $VIP;do
    /sbin/ifconfig lo:$num down
    ((num++))
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

sysctl -e -p > /dev/null 2>&1
}

status() {
num=0

for ips in $VIP;do
    if ifconfig lo:$num | grep -i 'inet addr:' > /dev/null 2>&1;then
        echo "VIP [${ips}] is binding of lo:$num"
    else
        echo "VIP [${ips}] Not bound"
    fi
done
}


#main
if [[ -z "$VIP"  ]];then
    echo 'Error,VIP is NULL'
    exit 1
fi

case "$1" in
    start)
      start
      echo "Start OK"
      ;;

    stop)
      stop
      echo "Stoped OK"
      ;;

    status)
      status
      ;;

    *)
      echo "Usage: $0 {start|stop|status}"
      exit 1
esac

使用方法

mv real_server.sh /etc/init.d/
chmod 700 /etc/init.d/real_server.sh
chkconfig --add real_server.sh
chkconfig real_server.sh on

参考

回到页面顶部