shell脚本-LVS节点健康检查

#!/bin/bash
VIP=202.106.195.1
PORT=80
RIP=(192.168.100.10 192.168.100.20)
 
while true
do
  for ((i=0;i<`echo ${#RIP[*]}`;i++))
  do
    code=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://${RIP[$i]}`
    if [ $code -ne 200 -a $(ipvsadm -Ln|grep ${RIP[$i]}|wc -l) -eq 1 ]
    then ipvsadm -d -t $VIP:$PORT -r ${RIP[$i]}:$PORT
    elif [ $code -eq 200 -a $(ipvsadm -Ln|grep ${RIP[$i]}|wc -l) -lt 1 ]
    then ipvsadm -a -t $VIP:$PORT -r ${RIP[$i]}:$PORT
    fi
  done
sleep 5
done
 
 
keepalived 监控服务
 
#!/bin/bash
while true
do
  if [ $(pidof httpd|wc -l) -eq 1 -a $(pidof keepalived|wc -l) -eq 0 ]
  then systemctl start keepalived
  fi
 
  pidof httpd &>/dev/null
  if [ $? -ne 0 ]
  then systemctl start httpd &>/dev/null
  fi
  sleep 3
 
  pidof httpd &>/dev/null
  if [ $? -ne 0 ]
  then systemctl stop keepalived
  fi
  sleep 3
done

你可能感兴趣的:(#,循序渐进学运维-脚本篇)