一、准备环境
#配置环境:
Redis Sentinel:192.68.40.16:26379
Redis server主:192.168.40.17:6379
Redis server从:192.168.40.18:6380
#查看主服务器状态:
[root@localhost ~]# redis-cli -p 6379 -a pwd123 info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=27794,lag=0
master_repl_offset:27794
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:27793
You have new mail in /var/spool/mail/root
#查看从服务器状态
[root@localhost ~]# redis-cli -p 6380 -a pwd123 info Replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:29162
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
二、搭建sentinel哨兵服务
#启动Redis Sentinel
#Sentinel的配置文件为sentinel.conf,复制一下配置,修改IP地址即可
[root@localhost ~]# vim /etc/redis/sentinel.conf
#sentinel实例监听的端口
port 26379
#守护进程模式
daemonize yes
#指明日志文件名
logfile "./sentinel1.log"
#工作路径,sentinel一般指定/tmp比较简单
dir ./
#master1
#哨兵监控这个master,在至少quorum个哨兵实例都认为master down后把master标记为odown
#(objective down客观down;相对应的存在sdown,subjective down,主观down)状态。
#slaves是自动发现,所以你没必要明确指定slaves。
sentinel monitor master1 192.168.40.17 6379 1
# master或slave多长时间(默认30秒)不能使用后标记为s_down状态。
sentinel down-after-milliseconds master1 1500
#若sentinel在该配置值内未能完成failover操作(即故障时master/slave自动切换),则认为本次failover失败。
sentinel failover-timeout master1 10000
#设置master和slaves验证密码
sentinel auth-pass master1 pwd123
[root@localhost ~]# redis-server /etc/redis/sentinel.conf --sentinel &
[1] 19296
#在sentinel中查看redis主从信息
[root@localhost ~]# redis-cli -p 26379
127.0.0.1:26379> info Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=master1,status=ok,address=192.168.40.17:6379,slaves=3,sentinels=1
#在sentinel中查看所监控道的master和slave
127.0.0.1:26379> SENTINEL masters
1) 1) "name"
2) "master1"
3) "ip"
4) "192.168.40.17"
5) "port"
6) "6379"
7) "runid"
8) "91399b3205a9061a5f1aa306692886f74e90"
9) "flags"
10) "master"
11) "pending-commands"
12) "0"
13) "last-ok-ping-reply"
14) "898"
15) "last-ping-reply"
16) "898"
17) "info-refresh"
18) "5019"
19) "role-reported"
20) "master"
21) "role-reported-time"
22) "266970"
23) "config-epoch"
24) "0"
25) "num-slaves"
26) "1"
27) "num-other-sentinels"
28) "0"
29) "quorum"
30) "1"
31) "down-after-milliseconds"
32) "1500"
33) "failover-timeout"
34) "10000"
35) "parallel-syncs"
36) "1"
127.0.0.1:26379> SENTINEL slaves master1
1) "name"
2) "192.168.40.18:6380"
3) "ip"
4) "192.168.40.18"
5) "port"
6) "6380"
7) "runid"
8) "7087b2cf81bcb53da6603ed5d7bc6509a632d11a"
9) "flags"
10) "slave"
11) "pending-commands"
12) "0"
13) "last-ok-ping-reply"
14) "843"
15) "last-ping-reply"
16) "842"
17) "info-refresh"
18) "7765"
19) "role-reported"
20) "slave"
21) "role-reported-time"
22) "88238"
23) "master-link-down-time"
24) "0"
25) "master-link-status"
26) "ok"
27) "master-host"
28) "192.168.40.17"
29) "master-port"
30) "6379"
31) "slave-priority"
32) "100"
33) "slave-repl-offset"
34) "6114"
#查看当前Master
127.0.0.1:26379> SENTINEL get-master-addr-by-name master1
1) "192.168.40.17"
2) "6379"
三、验证:
#停止主服务器
[root@localhost ~]# kill -9 2793
#登陆sentinel查看6380从服务器已经自动转为主服务器
[root@localhost ~]# redis-cli -p 26379
127.0.0.1:26379> SENTINEL get-master-addr-by-name master1
1) "127.0.0.1"
2) "6380"
#6379主服务器转为从服务器,状态为down
127.0.0.1:26379> SENTINEL slaves master1
1) 1) "name"
2) "127.0.0.1:6379"
3) "ip"
4) "127.0.0.1"
5) "port"
6) "6379"
7) "runid"
8) ""
9) "flags"
10) "s_down,slave,disconnected"
11) "pending-commands"
12) "0"
13) "last-ok-ping-reply"
14) "47310"
15) "last-ping-reply"
16) "47310"
17) "s-down-time"
18) "45778"
19) "info-refresh"
20) "1488253183255"
21) "role-reported"
22) "slave"
23) "role-reported-time"
24) "47310"
25) "master-link-down-time"
26) "0"
27) "master-link-status"
28) "err"
29) "master-host"
30) "?"
31) "master-port"
32) "0"
33) "slave-priority"
34) "100"
35) "slave-repl-offset"
36) "0"
#/etc/redis/6379.conf配置文件自动增加slaveof 127.0.0.1 6380
#/etc/redis/6380.conf配置文件里面的slaveof 127.0.0.1 6379自动消失
#如果需要将6379在此变为主服务器,需要手动更改slaveof配置即可。