lvs相关四道shell编程题


 Home> LiXinYu> lvs相关四道shell编程题  :2015 :Leo 转载请注明来源  阅读: 2,590 次 

本题旨在复习lvs原理,工作场景中有keepalived工具能更完美的实现

目录:

1       lvs服务器端启动脚本… 1

2       lvs客户端启动脚本… 3

3       RS健康检查… 5

4       lvs主备检查… 9

 

 

1         lvs服务器端启动脚本

【LVS服务器】手工开发ipvsadm管理ip_vs脚本,/etc/init.d/lvs {start|stop|restart}

[root@c66-lvs1 scripts]# cat lvs_server.sh
#!/bin/bash
# chkconfig: 2345 90 60
# description: lvs server start and stop scripts
# lixinyu QQ:496920337
. /etc/init.d/functions

VIP=(
  192.168.235.100
  192.168.235.200
)

RS=(
  192.168.235.144
  192.168.235.145
)

dev=eth0
arp_ip=192.168.235.253

lvs_start(){
  ipvsadm -C
  ipvsadm --set 30 5 60
  for ((i=0;i<${#VIP[*]};i++))
  do
    count=`ip addr|grep ${VIP[i]}|wc -l`
    [ $count -eq 0 ]&&ip addr add ${VIP[i]}/24 dev $dev
    RETVAL1=$?
    /sbin/arping -I $dev -c 3 -s ${VIP[i]} $arp_ip >/dev/null 2>&1
    ipvsadm -A -t ${VIP[i]}:80 -s wrr -p20
    for ((n=0;n<${#RS[*]};n++))
    do
      ipvsadm -a -t ${VIP[i]}:80 -r ${RS[n]} -g -w 1
    done
  done
  ((RETVAL=$RETVAL1+$?))
  sleep 1
  if [ $RETVAL -eq 0 ]
    then
      action "The lvs_server is start" /bin/true
  else
      action "The lvs_server is start" /bin/false
  fi
}

lvs_stop(){
  ipvsadm -C
  for ((i=0;i<${#VIP[*]};i++))
  do
    count=`ip addr|grep ${VIP[i]}|wc -l`
    [ $count -ne 0 ]&&ip addr del ${VIP[i]}/24 dev $dev
    RETVAL1=$?
  done
  ((RETVAL=$RETVAL1+$?))
  sleep 1
  if [ $RETVAL -eq 0 ]
    then
      action "The lvs_server is stop" /bin/true
  else
      action "The lvs_server is stop" /bin/false
  fi
}

USAGE(){
  echo "USAGE:$0{start|stop|restart}"
}

[ $# -ne 1 ]&&{
  USAGE
  exit 2
}

case "$1" in
  start)
       lvs_start
       exit $RETVAL
       ;;
  stop)
       lvs_stop
       exit $RETVAL
       ;;
  restart)
       lvs_stop
       lvs_start
       exit $RETVAL
       ;;
  *)
       USAGE
       exit 3
esac

 

 

2         lvs客户端启动脚本

【客户端服务器】开发LVS客户端设置VIP以及抑制ARP的管理脚本/etc/init.d/lvsclient {start|stop|restart}

[root@c66-apache scripts]# cat lvs_client.sh
#!/bin/bash
# chkconfig: 2345 90 60
# description: lvs client start and stop scripts
# lixinyu QQ:496920337

. /etc/init.d/functions

VIP=(
  192.168.235.100
  192.168.235.200
)

lvs_start(){
  for ((i=0;i<${#VIP[*]};i++))
  do
    count=`ip addr|grep ${VIP[i]}|wc -l`
    [ $count -eq 0 ]&&ip addr add ${VIP[i]}/32 dev lo
  done
  RETVAL=$?
  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
  sleep 1
  if [ $RETVAL -eq 0 ]
    then
      action "The lvs_client is start" /bin/true
  else
      action "The lvs_client is start" /bin/false
  fi
}

lvs_stop(){
  for ((i=0;i<${#VIP[*]};i++))
  do
    count=`ip addr|grep ${VIP[i]}|wc -l`
    [ $count -ne 0 ]&&ip addr del ${VIP[i]}/32 dev lo
  done
  RETVAL=$?
  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
  sleep 1
  if [ $RETVAL -eq 0 ]
    then
      action "The lvs_client is stop" /bin/true
  else
      action "The lvs_client is stop" /bin/false
  fi
}

USAGE(){
  echo "USAGE:$0{start|stop|restart}"
}

[ $# -ne 1 ]&&{
  USAGE
  exit 2
}

case "$1" in
  start)
       lvs_start
       exit $RETVAL
       ;;
  stop)
       lvs_stop
       exit $RETVAL
       ;;
  restart)
       lvs_stop
       lvs_start
       exit $RETVAL
       ;;
  *)
       USAGE
       exit 3
esac

 

3         RS健康检查

【LVS服务器】模拟keepalived对LVS节点做健康检查,挂掉剔除,好了加进来(ipvsadm命令实现添加和减少节点)。

[root@c66-lvs1 scripts]# cat lvs_rs_check.sh
#!/bin/bash
# lixinyu QQ:496920337

VIP=(
  192.168.235.100
  192.168.235.200
)

RS=(
  192.168.235.144
  192.168.235.145
)

web_file=/var/html/check/index.html

html(){
  cat >$web_file<<EOF
<html>
<head>
<title>rs node detection</title>
<meta http-equiv="refresh" content="1">
</head>
<body >
<center><a target=_blank><font size=8 color="0000ff"><b>RS Check</b></font></a><br></center>
<hr>
<div align=center><p>rs check list</p></div>
<table width="400" height="200" border=1 align=center>
<tr size=300>
<td bgcolor="Cyan" width="200">RS</td>
<td bgcolor="Cyan" width="150">STAT</td>
</tr>
EOF

  for ((i=0;i<${#RS[*]};i++))
  do
    echo " <tr size=300>" >>$web_file
    echo " <td bgcolor=\"#00CC33\" >${RS[i]}</td>" >>$web_file
    echo " <td id=${RS[i]} bgcolor=\"#00CC33\" >OK</td>" >>$web_file
    echo " </tr>" >>$web_file
  done

  cat >>$web_file<<EOF
</table>
</body>
<html>
EOF
}

lvs_add(){
  for ((n=0;n<${#VIP[*]};n++))
  do
    if [ `ipvsadm -L -n|grep ${RS[i]}|wc -l` -ne ${#VIP[*]} ];then
      ipvsadm -a -t ${VIP[n]}:80 -r ${RS[i]} -g -w 1
    fi
  done
}

lvs_del(){
  for ((n=0;n<${#VIP[*]};n++))
  do
    if [ `ipvsadm -L -n|grep ${RS[i]}|wc -l` -ne 0 ];then
      ipvsadm -d -t ${VIP[n]}:80 -r ${RS[i]}
    fi
  done
}
check_rs(){
  for ((i=0;i<${#RS[*]};i++))
  do
    num=`curl -I -s -w "%{http_code}" -o /dev/null ${RS[i]}`
    if [ $num -ne 200 ]
      then
        sed -i /'<td id='${RS[i]}' bgcolor'/s@.*@' <td id='${RS[i]}' bgcolor="#FF0000" >BAD</td>'@g $web_file
        lvs_del
    else
        sed -i /'<td id='${RS[i]}' bgcolor'/s@.*@' <td id='${RS[i]}' bgcolor="#00CC33" >OK</td>'@g $web_file
        lvs_add
    fi
  done
}

main(){
  html
  while true;do
    check_rs
    sleep 1
  done
}

main

 

 

检查RS是否存活,通过web界面显示,并且将有问题的RS从lvs配置里剔除,当RS恢复后再加进来。

相关细节如下

1

2

3

4         lvs主备检查

【LVS备节点】,模拟keepalved vrrp功能,监听主节点,如果主节点不可访问则,启动配置LVS接管主节点的资源(提醒:ARP缓存arping)

[root@c66-lvs2 scripts]# cat lvs_check.sh
#!/bin/bash
# lixinyu QQ:496920337
VIP=(
  192.168.235.100
  192.168.235.200
)
RS=(
  192.168.235.144
  192.168.235.145
)
dev=eth0
lvs_server=192.168.235.128
arp_ip=192.168.235.253

lvs_add(){
  ipvsadm -C
  ipvsadm --set 30 5 60
  for ((i=0;i<${#VIP[*]};i++))
  do
    count=`ip addr|grep ${VIP[i]}|wc -l`
    [ $count -eq 0 ]&&ip addr add ${VIP[i]}/24 dev $dev
    /sbin/arping -I $dev -c 3 -s ${VIP[i]} $arp_ip >/dev/null 2>&1
    ipvsadm -A -t ${VIP[i]}:80 -s wrr -p20
    for ((n=0;n<${#RS[*]};n++))
      do
        ipvsadm -a -t ${VIP[i]}:80 -r ${RS[n]} -g -w 1
    done
  done
}

lvs_del(){
  ipvsadm -C
  for ((i=0;i<${#VIP[*]};i++))
  do
    count=`ip addr|grep ${VIP[i]}|wc -l`
    [ $count -ne 0 ]&&ip addr del ${VIP[i]}/24 dev $dev
  done
}

check_lvs(){
  ping -c2 $lvs_server >>/dev/null 2>&1
  RETVAL=$?
  if [ $RETVAL -ne 0 ]
    then
      [ `ipvsadm -L -n|grep "TCP"|wc -l` -eq 0 ]&& \
      lvs_add
  else
      [ `ipvsadm -L -n|grep "TCP"|wc -l` -ne 0 ]&& \
      lvs_del
  fi
}

main(){
  while true;do
    check_lvs
    sleep 1
  done
}

main

 

 

 

 



正文部分到此结束

转载请注明原文链接 http://www.z-dig.com/four-shell-lvs-related-programs.html

 <- 点击复制网址
 <- 点击分享