一、测试环境
master: 127.0.0.1 6379
slave1: 127.0.0.1 6479
slave2: 127.0.0.1 6579
master-sentinel: 127.0.0.1 26379
slave1-sentinel: 127.0.0.1 26479
slave2-sentinel: 127.0.0.1 26579
二、下载安装redis 2.8.3
wget http://download.redis.io/releases/redis-2.8.3.tar.gz
tar –zxvf redis-2.8.3.tar.gz
cd redis-2.8.3
make;make install(此处可用PREFIX参数将redis安装到其他目录)
三、配置测试环境
—-创建目录:
cd /usr/local
mkdir redis_cluster
mkdir redis_cluster/master_6379
mkdir redis_cluster/slave_6479
mkdir redis_cluster/slave_6579
—-配置redis:
master
cp –a –R –p ~/redis-2.8.3/redis.conf ./redis_cluster/master_6379/
cp –a –R –p ~/redis-2.8.3/sentinel.conf ./redis_cluster/master_6379/6379-sentinel.conf
vi ./redis_cluster/master_6379/redis.conf(将对应配置修改成如下)
daemonize yes
port 6379
requirepass vhreal
masterauth vhreal
appendonly no
save ""
slave-read-only yes
vi ./redis_cluster/master_6379/6379-sentinel.conf
port 26379
sentinel monitor mymaster 192.168.0.86 6379 2
sentinel auth-pass mymaster vhreal
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 900000
slave1
cp –a –R –p ~/redis-2.8.3/redis.conf ./redis_cluster/slave_6479/
cp –a –R –p ~/redis-2.8.3/sentinel.conf ./redis_cluster/slave_6479/6479-sentinel.conf
vi ./redis_cluster/slave_6479/redis.conf(将对应配置修改成如下)
daemonize yes
port 6479
requirepass vhreal
masterauth vhreal
appendonly no
save ""
slave-read-only yes
slaveof 192.168.0.86 6379
vi ./redis_cluster/slave_6479/6479-sentinel.conf
port 26479
sentinel monitor mymaster 192.168.0.86 6379 2
sentinel auth-pass mymaster vhreal
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 900000
slave2
cp –a –R –p ~/redis-2.8.3/redis.conf ./redis_cluster/slave_6579/
cp –a –R –p ~/redis-2.8.3/sentinel.conf ./redis_cluster/slave_6579/6579-sentinel.conf
vi ./redis_cluster/slave_6579/redis.conf(将对应配置修改成如下)
daemonize yes
port 6579
requirepass vhreal
masterauth vhreal
appendonly no
save ""
slave-read-only yes
slaveof 192.168.0.86 6379
vi ./redis_cluster/slave_6579/6579-sentinel.conf
port 26579
sentinel monitor mymaster 192.168.0.86 6379 2
sentinel auth-pass mymaster vhreal
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 900000
四、启动集群
注意:首次构建sentinel环境时,必须首先启动master。
启动master和master-sentinel:
redis-server /usr/local/redis_cluster/master-6379/redis.conf
redis-sentinel /usr/local/redis_cluster/master-6379/6379-sentinel.conf
克隆会话,启动slave1和slave1-sentinel:
redis-server /usr/local/redis_cluster/slave-6479/redis.conf
redis-sentinel /usr/local/redis_cluster/slave-6479/6479-sentinel.conf
克隆会话,启动slave2和slave2-sentinel:
redis-server /usr/local/redis_cluster/slave-6579/redis.conf
redis-sentinel /usr/local/redis_cluster/slave-6579/6579-sentinel.conf
查看master的状态:
redis-cli -h 192.168.0.86 -p 6379
查看slave的状态:
redis-cli –h 192.168.0.86 –p 6479
—-场景1:slave宕机
关闭slave1:
查看sentinel状态,查看master的Replication信息,此时只存在一个slave2 6579。
—-场景2:slave恢复
重新开启slave1:
redis-server /usr/local/redis_cluster/slave-6479/redis.conf
查看sentinel状态:
sentinel能快速的发现slave加入到集群中:
—-场景3:master宕机
master-sentinel作为master 1的leader,会选取一个master 1的slave作为新的master。slave的选取是根据一个判断DNS情况的优先级来得到,优先级相同通过runid的排序得到,但目前优先级设定还没实现,所以直接获取runid排序得到slave 1。
然后发送命令slaveof no one来取消slave 1的slave状态来转换为master。当其他sentinel观察到该slave成为master后,就知道错误处理例程启动了。sentinel A然后发送给其他slave slaveof new-slave-ip-port 命令,当所有slave都配置完后,sentinel A从监测的masters列表中删除故障master,然后通知其他sentinels。
关闭master:
查看sentinel状态:
6379-sentinel:
自动将slave2即6579切换成master,原来的master变成slave。
6579-sentinel:
显示了failover的过程:
—-场景4:master恢复
重新启动原来的master:
redis-server /usr/local/redis_cluster/master-6379/redis.conf
查看sentinel状态:
原来的master自动切换成slave,不会自动恢复成master:
测试完成。
注意:若在sentinel已选出新主但尚未完成其它实例的reconfigure之前,重启old master,则整个系统会出现无法选出new master的异常。