Redis哨兵配置
Sentinel(哨兵)是Redis 的高可用性解决方案:由一个或多个Sentinel 实例 组成的Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所有从服务器,并在被监视的主服务器进入下线状态时,自动将下线主服务器属下的某个从服务器升级为新的主服务器。
192.168.56.11的redis配置如下
bind 0.0.0.0 protected-mode yes port 6379 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile "/var/run/redis_6379.pid" loglevel notice logfile "" databases 16 always-show-logo yes stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename "dump.rdb" dir "/etc/redis" masterauth "123456" 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 "123456" 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
192.168.56.12的redis配置是一样的
启动两台主机的redis
redis-server /etc/redis/6379.conf
在192.168.56.12的redis命令行执行命令设置主从
slaveof 192.168.56.11 6379
在11上面查看主从状态
在12上面查看主从状态
设置两台主机的哨兵配置/etc/redis/sentinel.conf
protected-mode no port 26379 daemonize yes logfile "/var/log/redis/sentinel.log" dir "/tmp" sentinel myid 3e945f7ff33d43dc4a451fa13297bd0bd3940338 sentinel monitor mymaster 192.168.56.11 6379 2 sentinel auth-pass mymaster 123456 sentinel config-epoch mymaster 4 sentinel leader-epoch mymaster 4
两台主机配置一样
只需要设置监控的master对应的主机IP和端口,不要忘记需要设置加密的密码
启动哨兵
redis-sentinel /etc/redis/sentinel.conf
查看进程
关闭主的redis模拟master宕机同时查看哨兵日志
登录192.168.56.12查看主从状态可以看到提升为master了从是空的
重新启动11的redis
redis-server /etc/redis/6379.conf
查看日志11成为12的从,12还是master
登录11查看主从状态