Redis以主从的模式搭建集群后,若是主节点Master挂掉,虽然能够实现将备用节点Slave切换成主节点,可是Redis自己并无自动监控机制,须要借助Sentinel哨兵模式,实现监控并实现自动切换。为了实现Sentinel的高可用,须要sentinel也以集群模式来搭建,这里经过一台机器的不一样端口来模拟。相关环境信息以下:redis
一、Redis集群信息:centos
角色 | IP地址 | 监听端口 |
Master | 127.0.0.1 | 6379 |
Slave | 127.0.0.1 | 6380 |
Slave | 127.0.0.1 | 6381 |
Sentinel集群信息:spa
哨兵角色 | IP地址 | 监听端口 |
Node-1 | 127.0.0.1 | 26379 |
Node-2 | 127.0.0.1 | 26380 |
Node-3 | 127.0.0.1 | 26381 |
二、Redis集群搭建过程参考上篇,这里再也不描述。首先启动Redis的主节点和两个Slave节点。日志
进入Master节点,查看信息:code
1 [root@VM_0_14_centos bin]# ./redis-cli -h 127.0.0.1 -p 6379 -a funnyboy 2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 3 127.0.0.1:6379> info replication 4 # Replication 5 role:master 6 connected_slaves:2 7 slave0:ip=127.0.0.1,port=6380,state=online,offset=4256,lag=0 8 slave1:ip=127.0.0.1,port=6381,state=online,offset=4256,lag=1 9 master_replid:d5802af0905736ae28201050ce4871ee2921c16c 10 master_replid2:0000000000000000000000000000000000000000 11 master_repl_offset:4256 12 second_repl_offset:-1 13 repl_backlog_active:1 14 repl_backlog_size:1048576 15 repl_backlog_first_byte_offset:1 16 repl_backlog_histlen:4256 17 127.0.0.1:6379>
显示当前节点role为master,而且链接的slave个数为2,OK。server
分别进入两个slave节点查看信息:blog
1 [root@VM_0_14_centos bin]# ./redis-cli -h 127.0.0.1 -p 6380 -a funnyboy 2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 3 127.0.0.1:6380> info replication 4 # Replication 5 role:slave 6 master_host:127.0.0.1 7 master_port:6379 8 master_link_status:up 9 master_last_io_seconds_ago:8 10 master_sync_in_progress:0 11 slave_repl_offset:4424 12 slave_priority:100 13 slave_read_only:1 14 connected_slaves:0 15 master_replid:d5802af0905736ae28201050ce4871ee2921c16c 16 master_replid2:0000000000000000000000000000000000000000 17 master_repl_offset:4424 18 second_repl_offset:-1 19 repl_backlog_active:1 20 repl_backlog_size:1048576 21 repl_backlog_first_byte_offset:1 22 repl_backlog_histlen:4424 23 127.0.0.1:6380>
1 [root@VM_0_14_centos bin]# ./redis-cli -h 127.0.0.1 -p 6381 -a funnyboy 2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 3 127.0.0.1:6381> info replication 4 # Replication 5 role:slave 6 master_host:127.0.0.1 7 master_port:6379 8 master_link_status:up 9 master_last_io_seconds_ago:5 10 master_sync_in_progress:0 11 slave_repl_offset:4550 12 slave_priority:100 13 slave_read_only:1 14 connected_slaves:0 15 master_replid:d5802af0905736ae28201050ce4871ee2921c16c 16 master_replid2:0000000000000000000000000000000000000000 17 master_repl_offset:4550 18 second_repl_offset:-1 19 repl_backlog_active:1 20 repl_backlog_size:1048576 21 repl_backlog_first_byte_offset:1 22 repl_backlog_histlen:4550 23 127.0.0.1:6381>
角色都为slave,而且master信息正常。确保Redis集群OK后,开始准备搭建sentinel集群。进程
三、Sentinel集群搭建ip
step一、将redis-sentinel拷贝到redis对应的执行目录bin(该命令是redis-server的一个链接,用redis-server也OK,后面讲到,启动sentinel会有两种方式),而后拷贝sentinel.config到redis配置文件目录config。it
step二、分别编辑sentinel的配置文件(没特别强调的保持默认便可)
sentinel-26379.conf配置以下:
1 daemonize yes #开启后台守护进程 2 port 26379 #端口配置 3 pidfile "/usr/local/redis/pid/redis-sentinel-26379.pid" #PID文件 4 logfile "/usr/local/redis/logs/sentinel-26379.log" #日志文件 5 sentinel monitor mymaster 127.0.0.1 6379 2 #哨兵监控配置。注意,若是配置了认证,改配置必须在auth-pass配置以前,不然启动报找不到master的错误 6 sentinel auth-pass mymaster funnyboy #认证配置 7 sentinel down-after-milliseconds mymaster 5000 #master或者slave多少时间(默认30秒)不能使用标记为down状态。 8 sentinel failover-timeout mymaster 9000 #若哨兵在配置值内未能完成故障转移操做,则任务本次故障转移失败。
sentinel-26380.conf配置以下:
1 daemonize yes #开启后台守护进程 2 port 26380 #端口配置 3 pidfile "/usr/local/redis/pid/redis-sentinel-26380.pid" #PID文件 4 logfile "/usr/local/redis/logs/sentinel-26380.log" #日志文件 5 sentinel monitor mymaster 127.0.0.1 6379 2 #哨兵监控配置。注意,若是配置了认证,改配置必须在auth-pass配置以前,不然启动报找不到master的错误 6 sentinel auth-pass mymaster funnyboy #认证配置 7 sentinel down-after-milliseconds mymaster 5000 #master或者slave多少时间(默认30秒)不能使用标记为down状态。 8 sentinel failover-timeout mymaster 9000 #若哨兵在配置值内未能完成故障转移操做,则任务本次故障转移失败。
sentinel-26381.conf配置以下:
1 daemonize yes #开启后台守护进程 2 port 26381 #端口配置 3 pidfile "/usr/local/redis/pid/redis-sentinel-26381.pid" #PID文件 4 logfile "/usr/local/redis/logs/sentinel-26381.log" #日志文件 5 sentinel monitor mymaster 127.0.0.1 6379 2 #哨兵监控配置。注意,若是配置了认证,改配置必须在auth-pass配置以前,不然启动报找不到master的错误 6 sentinel auth-pass mymaster funnyboy #认证配置 7 sentinel down-after-milliseconds mymaster 5000 #master或者slave多少时间(默认30秒)不能使用标记为down状态。 8 sentinel failover-timeout mymaster 9000 #若哨兵在配置值内未能完成故障转移操做,则任务本次故障转移失败。
step三、启动哨兵监控程序:
1 2 [root@VM_0_14_centos redis]# ./bin/redis-sentinel ./config/sentinel-26379.conf 3 [root@VM_0_14_centos redis]# ./bin/redis-sentinel ./config/sentinel-26380.conf 4 [root@VM_0_14_centos redis]# ./bin/redis-sentinel ./config/sentinel-26381.conf
启动有两种方式:
一是执行:redis-sentinel sentinel.conf
二是执行:redis-server sentinel --sentinel
step四、经过哨兵链接,并检查信息:
1 [root@VM_0_14_centos bin]# ./redis-cli -h 127.0.0.1 -p 26379 -a funnyboy 2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 3 127.0.0.1:26379> info sentinel 4 # Sentinel 5 sentinel_masters:1 6 sentinel_tilt:0 7 sentinel_running_scripts:0 8 sentinel_scripts_queue_length:0 9 sentinel_simulate_failure_flags:0 10 master0:name=mymaster,status=ok,address=127.0.0.1:6379,slaves=2,sentinels=3 11 127.0.0.1:26379>
能够看到监控的redis服务,一个Master、两个Slave、sentinels = 3 说明配置OK。
四、模拟场景:Redis Master节点挂掉,查看Redis集群状态。
step一、关掉Master节点:
1 [root@VM_0_14_centos bin]# ./redis-cli -p 6379 -a funnyboy 2 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 3 127.0.0.1:6379> shutdown 4 not connected> 5 [root@VM_0_14_centos bin]#
step二、经过哨兵查看集群状态:
1 127.0.0.1:26379> info sentinel 2 # Sentinel 3 sentinel_masters:1 4 sentinel_tilt:0 5 sentinel_running_scripts:0 6 sentinel_scripts_queue_length:0 7 sentinel_simulate_failure_flags:0 8 master0:name=mymaster,status=ok,address=127.0.0.1:6380,slaves=2,sentinels=3 9 127.0.0.1:26379>
经过sentinel信息能够看到,Master节点已经自动切换到6380端口了,说明主节点挂掉后,6380 Slave节点自动升级成为了Master节点。
step三、启动6379 redis服务,而后查看节点角色,此时6379变成了Slave,6380为Master节点,OK。
1 [root@VM_0_14_centos redis]# ./bin/redis-server ./config/redis-6379.conf 2 [root@VM_0_14_centos redis]# ./bin/redis-cli -p 6379 -a funnyboy 3 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 4 127.0.0.1:6379> 5 127.0.0.1:6379> info replication 6 # Replication 7 role:slave 8 master_host:127.0.0.1 9 master_port:6380 10 master_link_status:up 11 master_last_io_seconds_ago:1 12 master_sync_in_progress:0 13 slave_repl_offset:392671 14 slave_priority:100 15 slave_read_only:1 16 connected_slaves:0 17 master_replid:c77763408266bcebf233bdc9e59e3bcf14dc7a08 18 master_replid2:0000000000000000000000000000000000000000 19 master_repl_offset:392671 20 second_repl_offset:-1 21 repl_backlog_active:1 22 repl_backlog_size:1048576 23 repl_backlog_first_byte_offset:378586 24 repl_backlog_histlen:14086