主从哨兵工作原理讲述
https://blog.csdn.net/hayre/article/details/78753683
http://www.redis.cn/documentation.html
主从哨兵搭建实战
需求如下:
贴一张超级丑的图
1. 安装redis+哨兵
按照如下链接的方式安装 主从哨兵
http://www.cnblogs.com/lazyball/p/8946064.html
2.修改主从哨兵的配置文件
bind 172.16.3.42 # protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile "/data/redis-4.0.9/6376/logs/redis_6379.pid" # loglevel notice logfile "/data/redis-4.0.9/6379/logs/redis.log" # databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/data/redis-4.0.9/6379/data" # masterauth "root123" # 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 requirepass "root123" lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no slave-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble no lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 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
bind 172.16.3.42 port 26379 dir "/data/redis-4.0.9/6379/sentinel" logfile "/data/redis-4.0.9/6379/sentinel/sentinel.log" sentinel myid 7adde137e24e5d458f5779b7896e1bb67b01bae8 sentinel monitor mymaster 172.16.3.42 6379 2 sentinel failover-timeout mymaster 10000 sentinel auth-pass mymaster root123 sentinel config-epoch mymaster 284 sentinel leader-epoch mymaster 285 sentinel known-slave mymaster 172.16.3.28 6379 sentinel known-sentinel mymaster 172.16.3.28 26379 7adde137e24e5d458f5779b7896e1bb67b01bae8 sentinel current-epoch 285
bind 172.16.3.28 ## protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile "/data/redis-4.0.9/6376/logs/redis_6379.pid" ## loglevel notice logfile "/data/redis-4.0.9/6379/logs/redis.log" ## databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/data/redis-4.0.9/6379/data" ## masterauth "root123" ## 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 requirepass "root123" ## lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no slave-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble no lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 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 slaveof 172.16.3.42 6379 ##
bind 172.16.3.28 port 26379 dir "/data/redis-4.0.9/6379/sentinel" logfile "/data/redis-4.0.9/6379/sentinel/sentinel.log" sentinel myid 7adde137e24e5d458f5779b7896e1bb67b01bae8 sentinel monitor mymaster 172.16.3.42 6379 2 sentinel failover-timeout mymaster 10000 sentinel auth-pass mymaster root123 sentinel config-epoch mymaster 284 sentinel leader-epoch mymaster 285 sentinel known-slave mymaster 172.16.3.28 6379 sentinel known-sentinel mymaster 172.16.3.42 26379 7adde137e24e5d458f5779b7896e1bb67b01bae8
根据自身情况修改配置文件后 重启 然后观察从主哨兵的日志信息
3. 登录主redis 创建key进行测试
# 登录redis /data/redis-4.0.9/bin/redis-cli -h 172.16.3.42 -a 123 # 使用info 可以查看主从关系 #在主上创建key 首先创建一些 key,并赋上对应值: redis 127.0.0.1:6379> SET runoob1 redis OK redis 127.0.0.1:6379> SET runoob2 mysql OK redis 127.0.0.1:6379> SET runoob3 mongodb OK 查找以 runoob 为开头的 key: redis 127.0.0.1:6379> KEYS runoob* 1) "runoob3" 2) "runoob1" 3) "runoob2" 获取 redis 中所有的 key 可用使用 *。 redis 127.0.0.1:6379> KEYS * 1) "runoob3" 2) "runoob1" 3) "runoob2"
4. 在从redis上查看是否key同步过去了
5.有兴趣可以把主停了,然后再起来,然后用info命令查看,就会主从调换了