1.环境信息
在三台主机上,ct6601为redis master,ct6602为redis slave,
ct6601,ct6602,ct6604各装一个sentinel
redis version:2.8.20
hsot:ct6601 192.108.56.117
ct6602 192.108.56.119
ct6604 192.108.56.120
master: 192.108.56.117 6379
slave1: 192.108.56.119 6379
sentinel1: 192.108.56.117 26379
sentinel2: 192.108.56.119 26379
sentinel3: 192.108.56.120 26379
2.配置
#在ct6601,ct6602,ct6604安装redis
[root@ct6601 ~]# tar -xzvf redis-2.8.20.tar.gz
[root@ct6601 ~]# cd /root/redis-2.8.20
[root@ct6601 ~]# make
[root@ct6601 ~]# make install
[root@ct6601 redis-2.8.20]# mkdir /usr/local/redis
#在ct6601,ct6602中复制默认的redis.conf文件
[root@ct6601 redis-2.8.20]# cp /root/redis-2.8.20/redis.conf /usr/local/redis/redis.conf
#修改ct6601的redis.conf配置文件
[root@ct6601 redis-2.8.20]# vi /usr/local/redis/redis.conf
修改:daemonize yes
#修改ct6602的redis.conf配置文件
[root@ct6602 redis-2.8.20]# vim /usr/local/redis/redis.conf
修改:daemonize yes
修改:slaveof 192.108.56.117 6379
#在ct6601,ct6602上启动redis服务
[root@ct6601 redis]# redis-server /usr/local/redis/redis.conf
#在ct6601,ct6602上查看验证master-slave
[root@ct6601 redis]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.108.56.119,port=6379,state=online,offset=225,lag=1
master_repl_offset:225
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:224
127.0.0.1:6379> set mykey hello,select~
OK
[root@ct6602 redis-2.8.20]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.108.56.117
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:334
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379> get mykey
"hello,select~"
#在ct6601,ct6602,ct6604上配置redis sentinel
[root@ct6601 redis]# cp /root/redis-2.8.20/sentinel.conf /usr/local/redis/sentinel.conf
[root@ct6601 redis]# vi /usr/local/redis/sentinel.conf
加入:daemonize yes
修改:sentinel monitor mymaster 192.108.56.117 6379 2
[root@ct6601 redis]# redis-sentinel /usr/local/redis/sentinel.conf
#查看验证redis sentinel
[root@ct6601 redis]# redis-cli -p 26379 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=192.108.56.117:6379,slaves=1,sentinels=3
#关闭ct6601的redis服务
[root@ct6601 redis]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.108.56.119,port=6379,state=online,offset=45753,lag=0
master_repl_offset:45896
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:45895
127.0.0.1:6379> shutdown
not connected> exit
#ct6602上的redis变为master角色
[root@ct6602 redis-2.8.20]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
#重新开启ct6601上的redis服务,可以看到ct6601上的redis已经变为slave角色
[root@ct6601 redis]# redis-server /usr/local/redis/redis.conf
[root@ct6601 redis]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.108.56.119
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:2040
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@ct6602 redis-2.8.20]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.108.56.117,port=6379,state=online,offset=9089,lag=0
master_repl_offset:9089
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:9088
3.备注
经测试,redis sentinel需要最少3个实例,否则如果一台主机redis和redis sentinel都down掉,无法自动切换.