membase自動化failover

    在membase web console臺,可以對membase cluster中的server進行fail over,remove等操作,這些操作是手動進行,也可以通過membase提供的CLI接口自動進行。我設想這樣一個環境,由兩臺server組成的membase cluster,server1 : 66.172.38.3 ,server2 : 66.172.38.4  .劃分三個Bucket,分別對應11211,11212,11213,如web application連接該分佈式緩存,如下示:66.172.38.3:11211,66.172.38.3:11212,66.172.38.3:11213, 在運行的過程中,server1 DOWN機,則web application會和membase失去聯繫。爲避免這種情況,將moxi代理安裝到web application端,使用localhost:11211,localhost:11212,localhost:11213連接membase,下面腳本可在兩臺server間提供failover.

 ###################################################################################

  1. #!/bin/bash 
  2.  
  3. status_code_3=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://66.172.38.3:8091/index.html` 
  4. status_code_4=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://66.172.38.4:8091/index.html` 
  5.  
  6. function_bind_3() 
  7. /opt/moxi/bin/moxi -u root -Z port_listen=11211 http://66.172.38.3:8091/pools/default/bucketsStreaming/default & 
  8. /opt/moxi/bin/moxi -u root -Z port_listen=11212 http://66.172.38.3:8091/pools/default/bucketsStreaming/base2 & 
  9. /opt/moxi/bin/moxi -u root -Z port_listen=11213 http://66.172.38.3:8091/pools/default/bucketsStreaming/base3 & 
  10.  
  11. function_bind_4() 
  12. /opt/moxi/bin/moxi -u root -Z port_listen=11211 http://66.172.38.4:8091/pools/default/bucketsStreaming/default & 
  13. /opt/moxi/bin/moxi -u root -Z port_listen=11212 http://66.172.38.4:8091/pools/default/bucketsStreaming/base2 & 
  14. /opt/moxi/bin/moxi -u root -Z port_listen=11213 http://66.172.38.4:8091/pools/default/bucketsStreaming/base3 & 
  15.  
  16. function_failover_3() 
  17. failover_flag=`/opt/membase/bin/cli/membase server-list -c 66.172.38.4:8091  -u Administrator -p 123456 | grep 66.172.38.3 | awk '{print $3$4}'
  18.  
  19. if [ x$failover_flag == "xunhealthyactive" ];then 
  20. echo "-----$(date)-------" >> /root/myselffile/membase/failover.log 
  21. /opt/membase/bin/cli/membase failover -c 66.172.38.4:8091 --server-failover=66.172.38.3:8091 -u Administrator -p 123456 >> /root/myselffile/membase/failover.log 2>&1 
  22. /opt/membase/bin/cli/membase rebalance -c 66.172.38.4:8091 -u Administrator -p 123456 >> /root/myselffile/membase/failover.log 2>&1 
  23. echo "-----End-----------" >> /root/myselffile/membase/failover.log 
  24. fi 
  25.  
  26. function_failover_4() 
  27. failover_flag=`/opt/membase/bin/cli/membase server-list -c 66.172.38.3:8091  -u Administrator -p 123456 | grep 66.172.38.4 | awk '{print $3$4}'
  28.  
  29. if [ x$failover_flag == "xunhealthyactive" ];then 
  30. echo "-----$(date)-------" >> /root/myselffile/membase/failover.log   
  31. /opt/membase/bin/cli/membase failover -c 66.172.38.3:8091 --server-failover=66.172.38.4:8091 -u Administrator -p 123456 >> /root/myselffile/membase/failover.log 2>&1 
  32. /opt/membase/bin/cli/membase rebalance -c 66.172.38.3:8091 -u Administrator -p 123456 >> /root/myselffile/membase/failover.log 2>&1 
  33. echo "-----End-----------" >> /root/myselffile/membase/failover.log 
  34. fi 
  35.  
  36. if [ x$status_code_3 == "x200" ]; then 
  37.    function_bind_3  
  38.    server_flag="x3"  
  39.    else 
  40.      if [ x$status_code_4 == "x200" ]; then 
  41.      function_bind_4 
  42.      server_flag="x4"     
  43.      fi  
  44. fi  
  45.  
  46. while true 
  47. do 
  48. status_code_3=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://66.172.38.3:8091/index.html` 
  49. status_code_4=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://66.172.38.4:8091/index.html` 
  50.  
  51. if [ x$status_code_3 == "x000" ];then 
  52.    if [ x$server_flag == "xx3" ];then 
  53.       if [ x$status_code_4 == "x200" ];then 
  54.       killall moxi 
  55.       function_failover_3 
  56.       function_bind_4 
  57.       server_flag="x4" 
  58.       fi 
  59.    fi 
  60. fi  
  61.  
  62. if [ x$status_code_4 == "x000" ];then 
  63.    if [ x$server_flag == "xx4" ];then 
  64.       if [ x$status_code_3 == "x200" ];then 
  65.       killall moxi 
  66.       function_failover_4 
  67.       function_bind_3 
  68.       server_flag="x3" 
  69.       fi 
  70.    fi 
  71. fi 
  72.  
  73. if [ x$status_code_3 == "x000" ];then 
  74.    if [ x$server_flag == "xx4" ];then 
  75.       if [ x$status_code_4 == "x200" ];then 
  76.       function_failover_3 
  77.       fi 
  78.    fi 
  79. fi 
  80.  
  81.  
  82. if [ x$status_code_4 == "x000" ];then 
  83.    if [ x$server_flag == "xx3" ];then 
  84.       if [ x$status_code_3 == "x200" ];then 
  85.       function_failover_4 
  86.       fi 
  87.    fi 
  88. fi 
  89. done 

 ####################################################################################
本環境只設定是兩臺server構成的membase cluster的failover情況,照此可以擴展爲多臺server構成的cluster的failover情況。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章