2:启动sentinel(三个节点):
# redis-sentinel /etc/redis/sentinel_26379.conf
# redis-sentinel /etc/redis/sentinel_26381.conf
# redis-sentinel /etc/redis/sentinel_26382.conf
注意,必须使用redis-sentinel命令启动sentinel_26379.conf,虽然redis-sentinel是redis-server的软连接。
启动sentinel后,查看sentinel_26379.conf、sentinel_26380.conf、sentinel_26381.conf
可发现配置文件会记录已经加入的集群节点,并且配置文件内容改变了:
[root@localhost redis]# cat sentinel-26379.conf
port 26379
daemonize yes
pidfile "/var/run/redis-26379.pid"
logfile "/var/log/redis_26379.log"
dir "/tmp"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 127.0.0.1 6382
# Generated by CONFIG REWRITE
sentinel known-slave mymaster 127.0.0.1 6381
sentinel known-sentinel mymaster 127.0.0.1 263818fa3f71262abb6adfb838a598f449f855a9d4f3f
sentinel known-sentinel mymaster 127.0.0.1 2638226f3c5cf596971dbf8c8ed112d015fa252f36a08
sentinel current-epoch 0
[root@localhost redis]# cat sentinel-26381.conf
#master1
port 26381
daemonize yes
pidfile "/var/run/redis-26381.pid"
logfile "/var/log/redis_26381.log"
dir "/tmp"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 127.0.0.1 6382
# Generated by CONFIG REWRITE
sentinel known-slave mymaster 127.0.0.1 6381
sentinel known-sentinel mymaster 127.0.0.1 263798eb55903cd604571d4a816a2abfc6e7d60f9e356
sentinel known-sentinel mymaster 127.0.0.1 2638226f3c5cf596971dbf8c8ed112d015fa252f36a08
sentinel current-epoch 0
[root@localhost redis]# cat sentinel-26382.conf
#master1
port 26382
daemonize yes
pidfile "/var/run/redis-26382.pid"
logfile "/var/log/redis_26382.log"
dir "/tmp"
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
sentinel known-slave mymaster 127.0.0.1 6381
# Generated by CONFIG REWRITE
sentinel known-slave mymaster 127.0.0.1 6382
sentinel known-sentinel mymaster 127.0.0.1 263818fa3f71262abb6adfb838a598f449f855a9d4f3f
sentinel known-sentinel mymaster 127.0.0.1 263798eb55903cd604571d4a816a2abfc6e7d60f9e356
sentinel current-epoch 0
查看启动状态:
[root@localhost redis]# ps -ef |grep redis
root 2630 1 016:41 ? 00:00:03 redis-server*:6379
root 2645 1 016:46 ? 00:00:02 redis-server*:6381
root 2650 1 016:46 ? 00:00:02 redis-server*:6382
root 2705 1 017:13 ? 00:00:02 redis-sentinel*:26379 [sentinel]
root 2724 2276 017:22 pts/1 00:00:00 tail -f/var/log/redis_26379.log
root 2736 1 0 17:26 ? 00:00:00 redis-sentinel *:26382[sentinel]
root 2745 1 117:27 ? 00:00:00 redis-sentinel*:26381 [sentinel]
root 2749 1640 017:27 pts/0 00:00:00 grep redis
查看集群状态:
[root@localhost redis]# redis-cli info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=262006,lag=0
slave1:ip=127.0.0.1,port=6382,state=online,offset=262006,lag=0
master_repl_offset:262139
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:262138
查看sentinel状态:
[root@localhost 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=127.0.0.1:6379,slaves=2,sentinels=4
[root@localhost redis]# redis-cli -p 26381 infosentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3
[root@localhost redis]# redis-cli -p 26382 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=4
查看sentinel监控的所有master:
[root@localhost redis]# redis-cli -p 26379 sentinelmasters
查看mymaster下有哪些slaves
[root@localhost redis]# redis-cli -p 26379 sentinelslaves mymaster
强制进行主从切换:
[root@localhost redis]# redis-cli -p 26379 sentinelfailover mymaster
OK
[root@localhost redis]# redis-cli -p 26379 sentinelget-master-addr-by-name mymaster
1) "127.0.0.1"
2) "6382"
[root@localhost redis]# redis-cli -p 26379 sentinelfailover mymaster
OK
[root@localhost redis]# redis-cli -p 26379 sentinelget-master-addr-by-name mymaster
1) "127.0.0.1"
2) "6381
查看mymaster集群中的master地址和端口
[root@localhost redis]# redis-cli -p 26379 sentinelget-master-addr-by-name mymaster
1) "127.0.0.1"
2) "6379"
模拟故障转移:
Down掉6379服务,看是否自动切换:
[root@localhost redis]# redis-cli -p 26379 sentinelget-master-addr-by-name mymaster
1) "127.0.0.1"
2) "6382"
从上可看到会自动切换到了6382服务了。
模拟master恢复服务后的集群状态:
[root@localhost redis]# redis-cli -p 26379 sentinelget-master-addr-by-name mymaster
1) "127.0.0.1"
2) "6382"
[root@localhost redis]# redis-cli -p 26379 sentinelmasters
1) 1)"name"
2)"mymaster"
3)"ip"
4)"127.0.0.1"
5)"port"
6) "6382"
。。。。。。。
[root@localhost redis]# redis-cli -p 26379 sentinelslaves mymaster
1) 1)"name"
2)"127.0.0.1:6381"
3)"ip"
4)"127.0.0.1"
5)"port"
6)"6381"
7)"runid"
8)"a05d4350123b04c8bc98b00a3c1b0179b3b756e5"
9)"flags"
10) "slave"
11)"pending-commands"
12)"0"
13)"last-ping-sent"
14)"0"
15)"last-ok-ping-reply"
16)"498"
17)"last-ping-reply"
18)"498"
19)"down-after-milliseconds"
20)"30000"
21)"info-refresh"
22)"8512"
23)"role-reported"
24)"slave"
25)"role-reported-time"
26)"219661"
27)"master-link-down-time"
28)"0"
29)"master-link-status"
30)"ok"
31)"master-host"
32)"127.0.0.1"
33)"master-port"
34)"6382"
35)"slave-priority"
36)"100"
37)"slave-repl-offset"
38)"41424"
2) 1)"name"
2)"127.0.0.1:6379"
3)"ip"
4)"127.0.0.1"
5)"port"
6)"6379"
7)"runid"
8)"a2b0b1b19758c3cdfeb534a3c1274bcc6c19e55b"
9)"flags"
10)"slave"
11) "pending-commands"
12)"0"
13)"last-ping-sent"
14)"0"
15)"last-ok-ping-reply"
16)"209"
17)"last-ping-reply"
18)"209"
19)"down-after-milliseconds"
20)"30000"
21)"info-refresh"
22)"9908"
23)"role-reported"
24)"slave"
25)"role-reported-time"
26)"29937"
27)"master-link-down-time"
28)"0"
29)"master-link-status"
30)"ok"
31)"master-host"
32)"127.0.0.1"
33)"master-port"
34)"6382"
35)"slave-priority"
36)"100"
37)"slave-repl-offset"
38)"41158"
结果:可以从上看出,就算master恢复了,集群也不会让master重新接管,而是作为slave出现了。
3:设置开机启动(三个节点)
# echo "redis-sentinel /etc/redis/sentinel_26379.conf " >>/etc/rc.local