Failover

Redis主從使用

  • August 21, 2012

雖然我知道如何在兩台不同的伺服器上配置和執行 Redis 主伺服器和從伺服器,但我想知道如果主 Redis 伺服器出現故障,我的 Web 伺服器是否能夠連接到從屬 Redis 伺服器?

如果不是這樣,我可以知道我該如何實現嗎?

謝謝

巴拉吉

在伺服器端,對於簡單的故障轉移,您可以使用 Nagios 和 NRPE 來完成。

在 Nagios 伺服器上:

define service{
   use                     critical-service
   host_name               B
   service_description     redis:2302
   check_command           check_tcp!2302
   event_handler           promote_redis!C!2302
   contact_groups          admin-sms,admin

}

define command{ 
   command_name    promote_redis
   command_line    $USER1$/eventhandlers/promote_redis.sh $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$ $ARG1$ $ARG2$
}

promote_redis.sh

#!/bin/bash

case "$1" in
   OK)
       ;;
   WARNING)
       ;;
   UNKNOWN)
       /usr/local/nagios/libexec/check_nrpe -H $4 -c promote_redis -a $5
       ;;
   CRITICAL)
       /usr/local/nagios/libexec/check_nrpe -H $4 -c promote_redis -a $5
       ;;
esac

exit 0

在奴隸上:

nrpe.cfg

command[promote_slave_redis]=/usr/lib64/nagios/plugins/promote_redis.sh $ARG1$

promote_redis.sh

#!/bin/bash

echo 'slaveof no one' | /usr/local/redis/bin/redis-cli -h C -p $1

可以使用 設置虛擬 IP keepalived,如下所示:

/etc/keepalived/keepalived.conf

vrrp_script chk_redis {
   script "killall -0 redis-server"
   interval 2
   weight 2
}

vrrp_instance VI_1 {
   state MASTER
   #nopreempt
   interface eth0
   lvs_sync_daemon_interface eth0
   virtual_router_id 51
   priority 101
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass pa$$w0rd
   }
   virtual_ipaddress {
       x.y.z.t
   }
   track_script {
       chk_redis
   }
}

但是如果你想執行多個實例會發生什麼?

Yves Trudeau 和 Percona 編寫了一個很棒的資源代理來故障轉移 MySQL。基於此,Martin Walter 重寫了Redis服務。試試看!

crm configure show

node master_629
node svr200-632.localdomain
primitive VIP ocf:heartbeat:IPaddr2 \
   params ip="192.168.6.179" cidr_netmask="24" \
   meta target-role="Started"
primitive redis_6380 ocf:heartbeat:redis \
   params config="/usr/local/redis/etc/redis.conf"
ms ms_redis_6380 redis_6380 \
   meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true" target-role="Started"
location REDIS-PREFER-master_629 ms_redis_12345 100: master_629
colocation vip_on_ms_redis_6380 inf: VIP:Started ms_redis_6380:Master
order vip_after_ms_redis_6380 inf: ms_redis_6380:promote VIP:start
property $id="cib-bootstrap-options" \
   no-quorum-policy="ignore" \
   default-action-timeout="60s" \
   stonith-enabled="false" \
   startup-fencing="false" \
   dc-version="1.0.12-unknown" \
   cluster-infrastructure="openais" \
   expected-quorum-votes="2"

crm status

============
Last updated: Tue Aug 21 22:12:11 2012
Stack: openais
Current DC: master_629 - partition with quorum
Version: 1.0.12-unknown
2 Nodes configured, 2 expected votes
6 Resources configured.
============

Online: [ svr200-632.localdomain master_629 ]

Master/Slave Set: ms_redis_12345
    Masters: [ master_629 ]
    Slaves: [ svr200-632.localdomain ]
VIP    (ocf::heartbeat:IPaddr2):   Started master_629
Master/Slave Set: ms_redis_6380
    Masters: [ master_629 ]
    Slaves: [ svr200-632.localdomain ]

引用自:https://serverfault.com/questions/419666