1、所有节点安装redis 节点信息: 主1:10.0.7.53 从2:10.0.7.50 从3:10.0.7.51 除了在三个节点部署redis服务,再分别部署sentinel。
2、配置主从,在主节点配置文件添加如下配置: 主节点1配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 pidfile /data/redis/log/redis.pid bind 10.0 .7 .53 port 6379 daemonize yes logfile "/data/redis/log/redis.log" databases 16 maxmemory 1g requirepass 123456 masterauth 123456 slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-ping-slave-period 10 repl-timeout 60 repl-disable-tcp-nodelay no repl-backlog-size 5mb repl-backlog-ttl 3600
从节点2配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 pidfile /data/redis/log/redis.pid bind 10.0 .7 .50 port 6379 daemonize yes logfile "/data/redis/log/redis.log" databases 16 maxmemory 1g slaveof 10.0 .7 .53 6379 masterauth 123456 requirepass 123456 slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-ping-slave-period 10 repl-timeout 60 repl-disable-tcp-nodelay no repl-backlog-size 5mb repl-backlog-ttl 3600
从节点3配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 pidfile /data/redis/log/redis.pid bind 10.0 .7 .50 port 6379 daemonize yes logfile "/data/redis/log/redis.log" databases 16 maxmemory 1g slaveof 10.0 .7 .53 6379 masterauth 123456 requirepass 123456 slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-ping-slave-period 10 repl-timeout 60 repl-disable-tcp-nodelay no repl-backlog-size 5mb repl-backlog-ttl 3600
上面配置相关参数详解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 slaveof <masterip> <masterport> masterauth <master-password> ------------------------------------- 如果主库配置了密码,从库没有添加这行数据,会报一下错误: * Retrying with SYNC... ------------------------------------- slave-serve-stale-data yes slave-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no slave-priority 100
3、修改哨兵配置文件 cat sentinel.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 port 26379 dir "/tmp/sentinel" logfile "/tmp/sentinel.log" daemonize yes sentinel monitor mymaster 10.0.7.53 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel parallel-syncs mymaster 1 sentinel auth-pass mymaster 123456 sentinel failover-timeout mymaster 180000 sentinel config-epoch mymaster 2 sentinel leader-epoch mymaster 2 sentinel known-slave mymaster 10.0.7.51 6379 sentinel known-slave mymaster 10.0.7.50 6379 sentinel current-epoch 2 protected-mode no
指定的dir路径若不存在需要手动创建:
sentinel相关配置参数详解:
1 2 3 4 5 6 7 8 9 10 11 12 13 // 当前Sentinel节点监控 127.0 .0 .1 :6379 这个主节点 // 2 代表判断主节点失败至少需要2个Sentinel节点节点同意 // mymaster是主节点的别名 //每个Sentinel节点都要定期PING命令来判断Redis数据节点和其余Sentinel节点是否可达,如果超过30000毫秒且没有回复,则判定不可达 //当Sentinel节点集合对主节点故障判定达成一致时,Sentinel领导者节点会做故障转移操作,选出新的主节点,原来的从节点会向新的主节点发起复制操作,限制每次向新的主节点发起复制操作的从节点个数为1 //故障转移超时时间为180000毫秒
4、启动服务 各服务器启动redis服务
1 /data/redis/src/redis-server /data/redis/etc/redis.conf
各服务器启动sentinel服务
1 /data/redis/src/redis-sentinel /data/redis/sentinel.conf
查看集群信息
1 /data/redis/src/redis-cli -h 127.0.0.1 -p 26379 INFO Sentinel