在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.
###################################################################################
- #!/bin/bash
- status_code_3=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://66.172.38.3:8091/index.html`
- status_code_4=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://66.172.38.4:8091/index.html`
- function_bind_3()
- {
- /opt/moxi/bin/moxi -u root -Z port_listen=11211 http://66.172.38.3:8091/pools/default/bucketsStreaming/default &
- /opt/moxi/bin/moxi -u root -Z port_listen=11212 http://66.172.38.3:8091/pools/default/bucketsStreaming/base2 &
- /opt/moxi/bin/moxi -u root -Z port_listen=11213 http://66.172.38.3:8091/pools/default/bucketsStreaming/base3 &
- }
- function_bind_4()
- {
- /opt/moxi/bin/moxi -u root -Z port_listen=11211 http://66.172.38.4:8091/pools/default/bucketsStreaming/default &
- /opt/moxi/bin/moxi -u root -Z port_listen=11212 http://66.172.38.4:8091/pools/default/bucketsStreaming/base2 &
- /opt/moxi/bin/moxi -u root -Z port_listen=11213 http://66.172.38.4:8091/pools/default/bucketsStreaming/base3 &
- }
- function_failover_3()
- {
- 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}'`
- if [ x$failover_flag == "xunhealthyactive" ];then
- echo "-----$(date)-------" >> /root/myselffile/membase/failover.log
- /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
- /opt/membase/bin/cli/membase rebalance -c 66.172.38.4:8091 -u Administrator -p 123456 >> /root/myselffile/membase/failover.log 2>&1
- echo "-----End-----------" >> /root/myselffile/membase/failover.log
- fi
- }
- function_failover_4()
- {
- 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}'`
- if [ x$failover_flag == "xunhealthyactive" ];then
- echo "-----$(date)-------" >> /root/myselffile/membase/failover.log
- /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
- /opt/membase/bin/cli/membase rebalance -c 66.172.38.3:8091 -u Administrator -p 123456 >> /root/myselffile/membase/failover.log 2>&1
- echo "-----End-----------" >> /root/myselffile/membase/failover.log
- fi
- }
- if [ x$status_code_3 == "x200" ]; then
- function_bind_3
- server_flag="x3"
- else
- if [ x$status_code_4 == "x200" ]; then
- function_bind_4
- server_flag="x4"
- fi
- fi
- while true
- do
- status_code_3=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://66.172.38.3:8091/index.html`
- status_code_4=`/usr/bin/curl -o /dev/null -s -w %{http_code} http://66.172.38.4:8091/index.html`
- if [ x$status_code_3 == "x000" ];then
- if [ x$server_flag == "xx3" ];then
- if [ x$status_code_4 == "x200" ];then
- killall moxi
- function_failover_3
- function_bind_4
- server_flag="x4"
- fi
- fi
- fi
- if [ x$status_code_4 == "x000" ];then
- if [ x$server_flag == "xx4" ];then
- if [ x$status_code_3 == "x200" ];then
- killall moxi
- function_failover_4
- function_bind_3
- server_flag="x3"
- fi
- fi
- fi
- if [ x$status_code_3 == "x000" ];then
- if [ x$server_flag == "xx4" ];then
- if [ x$status_code_4 == "x200" ];then
- function_failover_3
- fi
- fi
- fi
- if [ x$status_code_4 == "x000" ];then
- if [ x$server_flag == "xx3" ];then
- if [ x$status_code_3 == "x200" ];then
- function_failover_4
- fi
- fi
- fi
- done
####################################################################################
本環境只設定是兩臺server構成的membase cluster的failover情況,照此可以擴展爲多臺server構成的cluster的failover情況。