针对web服务器容灾自动切换方案

思路: 
当服务器A 发生故障,服务器B可以迅速接管服务器A的任务,不影响用户的正常访问。 
当服务器A 故障恢复,服务器A可以马上接管服务器B的任务,服务器B恢复备机状态。 

做法: 
两台服务器上配置一个虚拟IP地址,主服务器先绑定虚拟ip地址,当发生故障时,备机自动接管虚拟ip地址,刷新网关路由地址。当主机恢复后,备机释放虚拟ip地址,主机再次主动接管虚拟ip地址,刷新网关路由地址。 

结构图: 
针对web服务器容灾自动切换方案 

主服务器:ip 192.168.190.199 
备服务器: ip 192.168.190.208 

vip:192.168.190.88 
gateWay=192.168.190.254 
netMask=255.255.255.0 
bcast=192.168.190.255 

步骤: 
1、配置虚拟ip:192.168.190.88 到 www.test.com 绑定虚拟ip 
 

Shell代码 针对web服务器容灾自动切换方案 针对web服务器容灾自动切换方案
  1. /sbin/ifconfig eth0:1 192.168.190.88 broadcast 192.168.190.255     
  2. netmask 255.255.255.0  up   
  3. /sbin/route add -host 192.168.190.88 dev eth0:1  



2、刷新网关路由 
 

Shell代码 针对web服务器容灾自动切换方案 针对web服务器容灾自动切换方案
  1. /sbin/arping -i eth0 -s 192.168.190.88  192.168.190.254 > /dev/null 2>&1  



3、当发生故障时,主机192.168.190.199 ,释放虚拟ip192.168.190.88,备机192.168.190.208接管 虚拟ip192.168.190.88 
 

Shell代码 针对web服务器容灾自动切换方案 针对web服务器容灾自动切换方案
  1. /sbin/ifconfig eth0:1 192.168.190.88  broadcast  192.168.190.255   netmask 255.255.255.0 down           
  2. /sbin/arping -i eth0 -s 192.168.190.88  192.168.190.254 > /dev/null 2>&1  



3、这时 www.test.com 解析到了备机服务器192.168.190.208 

4、重启备机web服务器 

5、若主机192.168.190.199服务恢复正常,备机192.168.190.208释放虚拟IP,主机 
绑定虚拟IP 192.168.190.88 
备机释放虚拟ip: 
 

Shell代码 针对web服务器容灾自动切换方案 针对web服务器容灾自动切换方案
  1. /sbin/ifconfig eth0:1 192.168.190.88  broadcast  192.168.190.255   netmask 255.255.255.0 down           
  2. /sbin/arping -i eth0 -s 192.168.190.88  192.168.190.254 > /dev/null 2>&1  



6、重启启动主备机web服务 

主机192.168.190.199 
autoSwitchMain.sh切换脚本 
 

Shell代码 针对web服务器容灾自动切换方案 针对web服务器容灾自动切换方案
  1. #!/bin/sh   
  2. #############################################################   
  3. #desc:服务器宕机自动切换服务   
  4. #author:gaozhonghui   
  5. #mail:[email protected]163.com   
  6. #date:20121101  
  7. #############################################################   
  8.   
  9. vip=192.168.190.88  
  10. gateWay=192.168.190.254  
  11. netMask=255.255.255.0  
  12. bcast=192.168.190.255  
  13.   
  14. function_bind_vip1(){   
  15. /sbin/ifconfig eth0:1 ${vip} broadcast ${bcast}  netmask ${netMask} up   
  16. /sbin/route add -host ${vip} dev eth0:1  
  17. }   
  18.   
  19. function_remove_vip1(){   
  20. /sbin/ifconfig eth0:1 ${vip} broadcast ${bcast}  netmask ${netMask down   
  21. }   
  22.   
  23. function_vip_arping1(){   
  24. /sbin/arping -i eth0 -s ${vip} ${gateWay} > /dev/null 2>&1  
  25. }   
  26.   
  27. function_restart_nginx(){   
  28. /web/webserver/nginx/sbin/nginx -s reload   
  29. }   
  30.   
  31. bind_time_vip="N"  
  32.   
  33. while true   
  34. do   
  35.     httpCode_rip1=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://192.168.190.199`   
  36.        
  37.     if [ x${httpCode_rip1} == "x200" ];   
  38.     then   
  39.         if [ ${bind_time_vip} == "N" ];   
  40.         then   
  41.             function_bind_vip1   
  42.             function_vip_arping1   
  43.             bind_time_vip="Y"  
  44.         fi   
  45.         function_vip_arping1   
  46.     else   
  47.         if [ ${bind_time_vip} == "Y" ]   
  48.         then   
  49.             function_remove_vip1   
  50.             bind_time_vip="N"  
  51.         fi   
  52.     fi   
  53.     sleep 10  
  54. done  



然后linux 启动守候进程 
/usr/bin/nohup /bin/sh  /home/Gzh/shell/ autoSwitchMain.sh   2>&1 > /dev/null & 

备机192.168.190.208
autoSwitchSlave.sh 
 

Shell代码 针对web服务器容灾自动切换方案 针对web服务器容灾自动切换方案
  1. #!/bin/sh   
  2. #############################################################   
  3. #desc:服务器宕机自动切换服务   
  4. #author:gaozhonghui   
  5. #mail:[email protected]163.com   
  6. #date:20121101  
  7. #############################################################   
  8.   
  9. vip=192.168.190.88  
  10. gateWay=192.168.190.254  
  11. netMask=255.255.255.0  
  12. bcast=192.168.190.255  
  13.   
  14. function_bind_vip1(){   
  15. /sbin/ifconfig eth0:1 ${vip} broadcast ${bcast}  netmask ${netMask} up   
  16. /sbin/route add -host ${vip} dev eth0:1  
  17. }   
  18.   
  19. function_remove_vip1(){   
  20. /sbin/ifconfig eth0:1 ${vip} broadcast ${bcast}  netmask ${netMask} down   
  21. }   
  22.   
  23. function_vip_arping1(){   
  24. /sbin/arping -i eth0 -s ${vip} ${gateWay} > /dev/null 2>&1  
  25. }   
  26.   
  27. function_restart_nginx(){   
  28. /web/webserver/nginx/sbin/nginx -s reload   
  29. }   
  30.   
  31. bind_time_vip="N"  
  32.   
  33. while true   
  34. do   
  35.         httpCode_rip1=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://192.                                                168.190.199`   
  36.   
  37.         if [ x${httpCode_rip1} == "x200" ];   
  38.         then   
  39.                 if [ ${bind_time_vip} == "Y" ];   
  40.                 then   
  41.                         function_remove_vip1   
  42.                         bind_time_vip="N"  
  43.                 fi   
  44.                 function_vip_arping1   
  45.         else   
  46.                 if [ ${bind_time_vip} == "N" ]   
  47.                 then   
  48.                         function_bind_vip1   
  49.                         function_vip_arping1   
  50.                         bind_time_vip="Y"  
  51.                 fi   
  52.         fi   
  53.   
  54.         sleep 10  
  55. done  



启动守候进程 
/usr/bin/nohup /bin/sh  /home/Gzh/shell/autoSwitchSlave.sh   2>&1 > /dev/null & 




本文转自 freeterman 51CTO博客,原文链接:http://blog.51cto.com/myunix/1120659,如需转载请自行联系原作者