21:开发脚本管理服务端LVS案例
[root@lb03 scripts]# cat lv_manager.sh #!/bin/bash #定义只能是root用户执行 if [ $UID -ne 0 ];then echo "permission deny 必须root用户执行" exit fi #文件存在就加载文件 [ -f /etc/init.d/functions ] && . /etc/init.d/functions #定义绑定的虚拟ip vip_netmask=10.0.0.13/24 vip=10.0.0.3 service_addr=10.0.0.13:80 wbs=( 10.0.0.17:80 10.0.0.18:80 ) count_pkg=$(rpm -qa|egrep ipvsadm|wc -l) function start () { #检查虚拟IP个数,IP检测和ipvsadm -ln两种方法检查 count_vip_check=$(ip a|grep 10.0.0.1[3]|wc -l) count_vip_check2=$(ipvsadm -ln|grep "10.0.0.1[3]:80"|wc -l) if [ ${count_vip_check} -eq 1 -o ${count_vip_check2} -eq 1 ] then echo "${count_vip_check} 虚拟IP已经存在" else ip addr add 10.0.0.13/24 dev eth0 ipvsadm -C ipvsadm --set 30 5 60 ipvsadm -A -t 10.0.0.13:80 -s wrr -p 20 fi #管理工具ipvsadm不存在就安装 [ ${count_pkg} -eq 0 ] && yum install ipvsadm -y 2>/dev/null #先检查一下网址通不通 for i in ${wbs[@]} do web_code=$(curl -o /dev/null -w "%{http_code}" -s $i) if [ "${web_code}" == "200" ] then echo "${i} is ok" ipvsadm -a -t 10.0.0.13:80 -r $i -g -w 1 else echo "添加的服务 ${i} 没有打开" fi done } stop () { count_vip_check=$(ip a|grep 10.0.0.1[3]|wc -l) if [ ${count_vip_check} -eq 0 ] then echo "虚拟IP 不存在" exit 11 else ip addr del 10.0.0.13/24 dev eth0 ipvsadm -C fi } case "$1" in start) start ;; stop) stop ;; restart) stop sleep 2 start ;; *) echo "Usage {start|stop|restart}" ;; esac
================================
下面是改过的比上面好
[root@lb04 scripts]# cat lv_manager.sh #!/bin/bash #定义只能是root用户执行 if [ $UID -ne 0 ];then echo "permission deny 必须root用户执行" exit fi #文件存在就加载文件 [ -f /etc/init.d/functions ] && . /etc/init.d/functions #定义绑定的虚拟ip vip_netmask=10.0.0.13/24 service_addr=10.0.0.13:80 wbs=( 10.0.0.17:80 10.0.0.18:80 ) count_pkg=$(rpm -qa|egrep ipvsadm|wc -l) function start () { #检查虚拟IP个数,IP检测和ipvsadm -ln两种方法检查 count_vip_check=$(ip a|grep 10.0.0.1[3]|wc -l) count_vip_check2=$(ipvsadm -ln|grep "10.0.0.1[3]:80"|wc -l) if [ ${count_vip_check} -eq 1 -o ${count_vip_check2} -eq 1 ] then echo "${count_vip_check} 本地虚拟IP已经存在,将退出" && exit 22 else check_count=$(nmap 10.0.0.13|grep "Host is up"|wc -l) [ ${check_count} -gt 0 ] && echo "远端 13 ip开启,将退出" && exit 11 ip addr add 10.0.0.13/24 dev eth0 ipvsadm -C ipvsadm --set 30 5 60 ipvsadm -A -t 10.0.0.13:80 -s wrr -p 20 fi #管理工具ipvsadm不存在就安装 [ ${count_pkg} -eq 0 ] && yum install ipvsadm -y 2>/dev/null #先检查一下网址通不通 for i in ${wbs[@]} do web_code=$(curl -o /dev/null -w "%{http_code}" -s $i) if [ "${web_code}" == "200" ] then echo "${i} is ok" ipvsadm -a -t 10.0.0.13:80 -r $i -g -w 1 else echo "添加的服务 ${i} 没有打开" fi done } stop () { count_vip_check=$(ip a|grep 10.0.0.1[3]|wc -l) if [ ${count_vip_check} -eq 0 ] then echo "虚拟IP 不存在" exit 11 else ip addr del 10.0.0.13/24 dev eth0 ipvsadm -C fi } case "$1" in start) start ;; stop) stop ;; restart) stop sleep 2 start ;; *) echo "Usage {start|stop|restart}" ;; esac
时来天地皆同力,运去英雄不自由