redis-sentinel
1.配置一主两从
2.配置redis-sentinel.conf
# 每一个sentinel的节点上的port最好设置成不同,容易区分 port 26379 sentinel monitor mymaster 10.1.1.123 6379 2 sentinel failover-timeout mymaster 900000 sentinel config-epoch mymaster 2 sentinel leader-epoch mymaster 2
# 注: 当redis集群切换后,sentinel会把切换的信息写入sentinel的配置文件中,
3.启动
下面是单机配置详细步骤:
本文使用的是redis2.8.24的版本;
1.测试环境
master: 192.168.136.130 6379 slave1: 192.168.136.130 6479 slave2: 192.168.136.130 6579 master-sentinel: 192.168.136.130 26379 slave1-sentinel: 192.168.136.130 26479 slave2-sentinel: 192.168.136.130 26579
2.下载安装
地址:http://www.redis.io
#安装 tar zxf redis-2.8.24.tar.gz cd redis-2.8.24 make && make install #安装之前可能需要安装别的底层软件; yum install -y gcc makc
3.配置测试环境
cd /usr/local mkdir redis_cluster mkdir redis_cluster/master_6379 mkdir redis_cluster/slave_6479 mkdir redis_cluster/slave_6579
配置redis
1) master
cp -f /usr/local/src/redis-2.8.24/redis.conf /usr/local/redis_cluster/master_6379/redis.conf cp -f /usr/local/src/redis-2.8.24/sentinel.conf /usr/local/redis_cluster/master_6379/6379_sentinel.conf
master_6379/redis.conf 修改为如下:
daemonize yes port 6379 requirepass 123456 masterauth 123456 appendonly no save "" slave-read-only yes
6379_sentinel.conf 修改如下:
port 26379 sentinel monitor mymaster 192.168.136.130 6379 2 sentinel auth-pass mymaster 123456 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 900000
2)slave1
cp -f /usr/local/src/redis-2.8.24/redis.conf /usr/local/redis_cluster/slave_6479/redis.conf cp -f /usr/local/src/redis-2.8.24/sentinel.conf /usr/local/redis_cluster/slave_6479/6479_sentinel.conf
slave_6479/redis.conf 修改如下:
daemonize yes port 6479 requirepass 123456 masterauth 123456 appendonly no save "" slave-read-only yes slaveof 192.168.136.130 6379
6479_sentinel.conf 修改如下:
port 26479 sentinel monitor mymaster 192.168.136.130 6379 2 sentinel auth-pass mymaster 123456 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 900000
3)slave2
cp -f /usr/local/src/redis-2.8.24/redis.conf /usr/local/redis_cluster/slave_6579/redis.conf cp -f /usr/local/src/redis-2.8.24/sentinel.conf /usr/local/redis_cluster/slave_6579/6579_sentinel.conf
slave_6579/redis.conf 修改如下:
daemonize yes port 6579 requirepass 123456 masterauth 123456 appendonly no save "" slave-read-only yes slaveof 192.168.136.130 6379
6479_sentinel.conf 修改如下:
port 26579 sentinel monitor mymaster 192.168.136.130 6379 2 sentinel auth-pass mymaster 123456 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 900000
4.启动集群
注意:首次构建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.136.130 -p 6379 #查看集群状态 info replication
5.测试
1)master 宕机
结果:master宕机后,sentinel集群会在slave集群中选取一台当做master继续提供服务;
2)master 恢复
结果:当之前的master恢复正常后,加入redis集群,被阶级为slave;
3) slave 宕机
结果:slave当机对master无影响;
本文参考 http://blog.csdn.net/wtyvhreal/article/details/46517483