最近由于要做负载均衡:LVS + keepalived ,想在真实服务器网上上添加LO:0接口,并设置虚拟IP在此接口上:
[root@itcast01 ~]# cd /etc/init.d/
[root@itcast01 ~]#touch realserver
[root@itcast01 ~]#vim realserver
SNS_VIP=192.168.88.137 . /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_VIP dev lo:0 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 -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down route del $SNS_VIP >/dev/null 2>&1 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 echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0
[root@itcast01 ~]#chmod 750 realserver #给此脚本赋予权限
[root@itcast01 ~]#chkconfig realserver on #设置开机自启动此脚本
此处出现了“service realserver does not support chkconfig”的问题。
很是奇怪,后经过查找资料,发现如果想添加脚本用service启动,必须要脚本里面包含这2行:
# chkconfig: 2345 10 90
# description: realserver ....
其他的都不所谓,只是个注意而已!!!
修改后的启动脚本:
# chkconfig: 2345 10 90 # description: realserver .... SNS_VIP=192.168.88.137 . /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_VIP dev lo:0 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 -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down route del $SNS_VIP >/dev/null 2>&1 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 echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0
[root@itcast01 ~]# cd /etc/init.d/
[root@itcast01 ~]#touch realserver
[root@itcast01 ~]#vim realserver
[root@itcast01 ~]#ll
[root@itcast01 ~]#chmod 750 realserver #给此脚本赋予权限
[root@itcast01 ~]#chkconfig realserver on #设置开机自启动此脚本
[root@itcast01 ~]#service realserver start #开启此脚本
RealServer Start OK #表示启动成功!
[root@itcast01 ~]#ifconfig #查看一下是否真的成功添加了lo:0接口
脚本参数说明:
#chkconfig: 2345 10 90 #description :realserver .... SNS_VIP=192.168.88.137 #定义VIP变量 . /etc/rc.d/init.d/functions #导脚本库 case "$1" in #case语句 $1传递给该shell脚本的第一个参数 start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP #设置Lo:0 VIP netmask 及广播 /sbin/route add -host $SNS_VIP dev lo:0 ##route del 增加本地路由 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 -p >/dev/null 2>&1 # -p <file> (default /etc/sysctl.conf) 将标准信息输入设备空文件 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down route del $SNS_VIP >/dev/null 2>&1 #route del 删除本地路由 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 echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" #$0 是脚本本身的名字 exit 1 #表示进程正常退出 esac #case结束 exit 0 #表示进程非正常退出