1.环境介绍
集群机器 redis服务 哨兵(sentinel) 节点描述
虚拟机:192.168.20.105 安装redis服务,端口6379 安装sentinel服务,端口26379 主节点
虚拟机:192.168.20.29 安装redis服务,端口6379 安装sentinel服务,端口26379 从节点
虚拟机:192.168.20.108 安装redis服务,端口6379 安装sentinel服务,端口26379 从节点
即:3个redis服务,三个sentinel服务,192.168.20.105 为主节点,实现按照上篇文章介绍配置好主从关系。
2.相关启动命令
cd到/usr/redis/bin目录可以执行以下命令:
redis-server /usr/redis/etc/redis.conf#启动redis服务
./redis-sentinel /usr/redis/etc/sentinel.conf#启动sentinel哨兵服务
./redis-cli -p 6379 -a 123456#打开本机redis客户端连接,连接上之后可执行set、get、keys *等命令
ps -ef|grep redis#查看redis状态
3.配置文件
虚拟机:192.168.20.105
redis.conf配置文件:
- daemonize yes#后台启动
- pidfile "/var/run/redis.pid"
- port 6379
- timeout 0
- tcp-keepalive 0
- loglevel notice
- logfile ""
- databases 16
- save 900 1
- save 300 10
- save 60 10000
- stop-writes-on-bgsave-error yes
- rdbcompression yes
- rdbchecksum yes
- dbfilename "dump.rdb"
- dir "/usr/redis/data"
- masterauth "123456"#如果做故障切换,不论主从节点都要填写密码且要保持一致
- slave-serve-stale-data yes
- slave-read-only yes
- repl-disable-tcp-nodelay no
- slave-priority 98
- requirepass "123456"#当前redis密码
- appendonly yes
- # appendfsync always
- appendfsync everysec
- # appendfsync no
- no-appendfsync-on-rewrite no
- auto-aof-rewrite-percentage 100
- auto-aof-rewrite-min-size 64mb
- lua-time-limit 5000
- slowlog-log-slower-than 10000
- slowlog-max-len 128
- notify-keyspace-events ""
- hash-max-ziplist-entries 512
- hash-max-ziplist-value 64
- list-max-ziplist-entries 512
- list-max-ziplist-value 64
- set-max-intset-entries 512
- zset-max-ziplist-entries 128
- zset-max-ziplist-value 64
- activerehashing yes
- client-output-buffer-limit normal 0 0 0
- client-output-buffer-limit slave 256mb 64mb 60
- client-output-buffer-limit pubsub 32mb 8mb 60
- hz 10
- aof-rewrite-incremental-fsync yes
- # Generated by CONFIG REWRITE
- port 26379
- sentinel monitor mymaster 192.168.20.105 6379 2#2表示在sentinel集群中只要有两个节点检测到redis主节点出故障就进行切换,单sentinel节点无效(自己测试发现的)
- sentinel down-after-milliseconds mymaster 3000#如果3s内mymaster无响应,则认为mymaster宕机了
- sentinel failover-timeout mymaster 10000#如果10秒后,mysater仍没活过来,则启动failover
- sentinel auth-pass mymaster 123456#redis主节点密码
- sentinel config-epoch mymaster 89
- daemonize yes
- dir "/usr/redis/bin"#指定工作目录
- sentinel known-slave mymaster 192.168.20.29 6379#sentinel自动生成的
- sentinel known-slave mymaster 192.168.20.108 6379#sentinel自动生成的
- sentinel known-sentinel mymaster 192.168.20.108 26379 695a7bf69bf7c0bdacb3a01ea262319cfca85de9#sentinel自动生成的
- # Generated by CONFIG REWRITE
- sentinel known-sentinel mymaster 192.168.20.29 26379 b1fba42ff8b363b2aea5c52e5dfa2c1ae52869e9#sentinel自动生成的
虚拟机:192.168.20.29
redis.conf配置文件:
- daemonize yes
- pidfile "/var/run/redis.pid"
- port 6379
- timeout 0
- tcp-keepalive 0
- loglevel notice
- logfile ""
- databases 16
- save 900 1
- save 300 10
- save 60 10000
- stop-writes-on-bgsave-error yes
- rdbcompression yes
- rdbchecksum yes
- dbfilename "dump.rdb"
- dir "/usr/redis/data"
- masterauth "123456"#主节点密码
- slave-serve-stale-data yes
- slave-read-only yes
- repl-disable-tcp-nodelay no
- slave-priority 98
- requirepass "123456"
- appendonly yes
- # appendfsync always
- appendfsync everysec
- # appendfsync no
- no-appendfsync-on-rewrite no
- auto-aof-rewrite-percentage 100
- auto-aof-rewrite-min-size 64mb
- lua-time-limit 5000
- slowlog-log-slower-than 10000
- slowlog-max-len 128
- notify-keyspace-events ""
- hash-max-ziplist-entries 512
- hash-max-ziplist-value 64
- list-max-ziplist-entries 512
- list-max-ziplist-value 64
- set-max-intset-entries 512
- zset-max-ziplist-entries 128
- zset-max-ziplist-value 64
- activerehashing yes
- client-output-buffer-limit normal 0 0 0
- client-output-buffer-limit slave 256mb 64mb 60
- client-output-buffer-limit pubsub 32mb 8mb 60
- hz 10
- aof-rewrite-incremental-fsync yes
- # Generated by CONFIG REWRITE
- slaveof 192.168.20.105 6379#配置主节点信息
- port 26379
- sentinel monitor mymaster 192.168.20.105 6379 2
- sentinel down-after-milliseconds mymaster 3000
- sentinel failover-timeout mymaster 10000
- sentinel auth-pass mymaster 123456
- sentinel config-epoch mymaster 89
- daemonize yes
- dir "/usr/redis/bin"
- sentinel known-slave mymaster 192.168.20.108 6379
- sentinel known-slave mymaster 192.168.20.29 6379
- sentinel known-sentinel mymaster 192.168.20.108 26379 695a7bf69bf7c0bdacb3a01ea262319cfca85de9
- # Generated by CONFIG REWRITE
- sentinel known-sentinel mymaster 192.168.20.105 26379 a8d94fbe0f1120278136c84a612ea84d57d2501c
虚拟机:192.168.20.108
redis.conf配置文件:
- daemonize yes
- pidfile "/var/run/redis.pid"
- port 6379
- timeout 0
- tcp-keepalive 0
- loglevel notice
- logfile ""
- databases 16
- save 900 1
- save 300 10
- save 60 10000
- stop-writes-on-bgsave-error yes
- rdbcompression yes
- rdbchecksum yes
- dbfilename "dump.rdb"
- dir "/usr/redis/data"
- masterauth "123456"
- slave-serve-stale-data yes
- slave-read-only yes
- repl-disable-tcp-nodelay no
- slave-priority 100
- requirepass "123456"
- appendonly yes
- # appendfsync always
- appendfsync everysec
- # appendfsync no
- no-appendfsync-on-rewrite no
- auto-aof-rewrite-percentage 100
- auto-aof-rewrite-min-size 64mb
- lua-time-limit 5000
- slowlog-log-slower-than 10000
- slowlog-max-len 128
- notify-keyspace-events ""
- hash-max-ziplist-entries 512
- hash-max-ziplist-value 64
- list-max-ziplist-entries 512
- list-max-ziplist-value 64
- set-max-intset-entries 512
- zset-max-ziplist-entries 128
- zset-max-ziplist-value 64
- activerehashing yes
- client-output-buffer-limit normal 0 0 0
- client-output-buffer-limit slave 256mb 64mb 60
- client-output-buffer-limit pubsub 32mb 8mb 60
- hz 10
- aof-rewrite-incremental-fsync yes
- # Generated by CONFIG REWRITE
- slaveof 192.168.20.105 6379
- port 26379
- sentinel monitor mymaster 192.168.20.105 6379 2
- sentinel down-after-milliseconds mymaster 3000
- sentinel failover-timeout mymaster 10000
- sentinel auth-pass mymaster 123456
- sentinel config-epoch mymaster 89
- daemonize yes
- dir "/usr/redis/bin"
- sentinel known-slave mymaster 192.168.20.108 6379
- sentinel known-slave mymaster 192.168.20.29 6379
- sentinel known-sentinel mymaster 192.168.20.105 26379 a8d94fbe0f1120278136c84a612ea84d57d2501c
- # Generated by CONFIG REWRITE
- sentinel known-sentinel mymaster 192.168.20.29 26379 b1fba42ff8b363b2aea5c52e5dfa2c1ae52869e9
4.sentinel支持集群(废话)
即使有一些sentinel进程宕掉了,依然可以进行redis集群的主备切换;
如果只有一个sentinel进程,如果这个进程运行出错,或者是网络堵塞,那么将无法实现redis集群的主备切换(单点问题);
如果有多个sentinel,redis的客户端可以随意地连接任意一个sentinel来获得关于redis集群中的信息。
以上配置文件除加了注释的地方外,其他地方基本都是redis自动生成的,只要注意一下加注释的点即可。
可能在配置过程中会出现故障不能切换的问题,极大可能是因为在原主节点配置文件中没有配置主节点密码,因为sentinel不会自动给主节点配置文件加上新的主节点密码。
Redis目前的复制是异步的,只保证最终一致性,而不是强一致性(主从数据库的更新还是分先后,先主后从)。要是一致性要求高的应用,目前还是读写都在主库上。
如果想要一句话讲清楚sentinel实现redis切换的原理那就是:sentinel心跳检测到主节点出现异常后,通过修改redis.conf配置文件的形式实现redis主从节点之间的故障切换。