虽然我知道如何在两台不同的服务器上配置和运行 Redis 主服务器和从服务器,但我想知道如果主 redis 服务器发生故障,我的 Web 服务器是否能够连接到从属 redis 服务器?
如果不是这样,请问我该如何实现呢?
谢谢
巴拉吉
答案1
在服务器端,对于简单的故障转移,您可以使用 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
可以使用以下方式设置虚拟 IPkeepalived
,类似这样:
/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 ]
答案2
您可以在客户端处理它,也可以使用类似