前提:现在有主从结构,主库没有配置持久化,从库配置AOF。(主库用来备份和写服务,从库用来提供读服务)
场景:哪天主库突然宕了,怎么办?
方法:连上从库,做save操作。将会在从库的data目录保存一份从库最新的dump.rdb文件。将这份dump.rdb文件拷贝到主库的data目录下。再重启主库。 |
就因为这个我想到了用集群的方案,但是redis官方没有MysqL那样的master-
master的模式~ 这样的情况下,只能咱们自己想办法了~
实现的原理~
当 Master 与 Slave 均运作正常时,Master负责读,Slave负责同步;
当 Master 挂掉,Slave 正常时,Slave接管服务,同时关闭主从复制功能;
然后依次循环。
这样,两台redis服务器中的任何一台挂掉,都会由另一台继续提供服务,不会对网站形
成可察觉的影响,也不会丢失数据。
你也可以实现
当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复
Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。
高可用方面
需要把读写进行分离的,写的话,就指向到一个vip~ 那两个主 用keepalived加脚本进行判断。
读的话,尽量用haproxy进行分流,这样的话,哪怕一个从down的话,haproxy会自动剔除的~
redis安装~
wgethttp://redis.googlecode.com/files/redis-2.2.13.tar.gztar-zxfredis-2.2.13.tar.gzcdredis-2.2.13makemakeinstall
keepalived的安装~
tar-xzvfkeepalived-1.1.20.tar.gzcdkeepalived-1.1.20./configure--prefix=/usr/local/webserver/keepalivedmakemakeinstallcp/usr/local/webserver/keepalived/sbin/keepalived/usr/sbincp/usr/local/webserver/keepalived/etc/sysconfig/keepalived/etc/sysconfigcp/usr/local/webserver/keepalived/etc/rc.d/init.d/keepalived/etc/init.dmkdir/etc/keepalivedcp/usr/local/webserver/keepalived/etc/keepalived/keepalived.conf/etc/keepalived
安装完之后,具体说下高可用性的部分,下面是 主的keepalived.conf 的配置文件~
用killall-0redis-server 来判断进程的存活~
也可以用checkredis.sh 这个脚本~
【大家可以写简单点~】 这个方法是redis自带的一个判断服务存活的程序
#!/bin/bash
REDIS_HOME="/home/redis"
REDIS_COMMANDS="/home/redis/src"#Thelocationoftheredisbinary
REDIS_MASTER_IP="172.16.0.180"#RedisMASTERip
REDIS_MASTER_PORT="6379"#RedisMASTERport
ERROR_MSG=`${REDIS_COMMANDS}/redis-cliPING`
#
#ChecktheoutputforPONG.
#
if["$ERROR_MSG"!="PONG"]
then
#redisisdown,returnhttp503
/bin/echo-e"HTTP/1.1503ServiceUnavailable\r\n"
/bin/echo-e"Content-Type:Content-Type:text/plain\r\n"
/bin/echo-e"\r\n"
/bin/echo-e"Redisis*down*.\r\n"
/bin/echo-e"\r\n"
exit1
else
#redisisfine,returnhttp200
/bin/echo-e"HTTP/1.1200OK\r\n"
/bin/echo-e"Content-Type:Content-Type:text/plain\r\n"
/bin/echo-e"\r\n"
/bin/echo-e"Redisisrunning.\r\n"
/bin/echo-e"\r\n"
exit0
fi
在变为backup的状态下,所运行的服务,为主的时候所运行的服务~
global_defs{ router_idDBPOOL_01 } vrrp_scriptchk_redis{ script"killall-0redis-server" interval2 } vrrp_instanceVI_ETH0{ interfaceeth0 virtual_router_id100 priority100 advert_int1 stateMASTER track_script{ chk_redis } virtual_ipaddress{ 172.16.0.180 } notify_master"/opt/redis.sh-m" notify_backup"/opt/redis.sh-s" notify_fault"/opt/redis.sh-k" }
下面是从的配置
global_defs{ router_idDBPOOL_01 } vrrp_scriptchk_redis{ script"killall-0redis-server" interval2 } vrrp_instanceVI_ETH0{ interfaceeth0 virtual_router_id100 nopreempt priority88 advert_int1 stateBACKUP track_script{ chk_redis } virtual_ipaddress{ 172.16.0.180 } notify_master"/opt/redis.sh-m" notify_backup"/opt/redis.sh-s" notify_fault"/opt/redis.sh-k" }
下面是/opt/redis.sh 的脚本
#!/bin/sh # #ScripttostartRedisandpromotetoMASTER/SLAVE #UsageOptions: #-mpromotetheredis-servertoMASTER #-spromotetheredis-servertoSLAVE #-kstarttheredis-serverandpromoteittoMASTER # REDIS_HOME="/home/redis" REDIS_COMMANDS="/home/redis/src"#redis执行文件的目录 REDIS_MASTER_IP="172.16.0.180"#RedisMASTERip REDIS_MASTER_PORT="6379"#RedisMASTERport REDIS_CONF="redis-mdb.conf"#配置文件 E_INVALID_ARGS=65 E_INVALID_COMMAND=66 E_NO_SLAVES=67 E_DB_PROBLEM=68 error(){ E_CODE=$? echo"Exiting:ERROR${E_CODE}:$E_MSG" exit$E_CODE } start_redis(){ alive=`${REDIS_COMMANDS}/redis-cliPING` if["$alive"!="PONG"];then ${REDIS_COMMANDS}/redis-server${REDIS_HOME}/${REDIS_CONF} sleep1 fi } start_master(){ ${REDIS_COMMANDS}/redis-cliSLAVEOFnoone } start_slave(){ ${REDIS_COMMANDS}/redis-cliSLAVEOF${REDIS_MASTER_IP}${REDIS_MASTER_PORT} } usage(){ echo-e"StartRedisandpromotetoMASTER/SLAVE-version0.3(c)AlexWilliams-www.alexwilliams.ca" echo-e"\nOptions:" echo-e"\t-m\tpromotetheredis-servertoMASTER" echo-e"\t-s\tpromotetheredis-servertoSLAVE" echo-e"\t-k\tstarttheredis-serverandpromoteittoMASTER" echo-e"" exit$E_INVALID_ARGS } forargin"$@" do case$argin -m)arg_m=true;; -s)arg_s=true;; -k)arg_k=true;; *)usage;; esac done if[$arg_m];then echo-e"Promotingredis-servertoMASTER\n" start_redis wait start_master elif[$arg_s];then echo-e"Promotingredis-servertoSLAVE\n" start_redis wait start_slave elif[$arg_k];then echo-e"Startingredis-serverandpromotingtoMASTER\n" start_redis wait start_master else usage fi