由于主从架构无法实现master和slave角色的自动切换,所以在发送master节点宕机时,redis主从复制无法实现自动的故障转移,即将slave 自动提升为新的master。因此,需要配置哨兵来"盯"着它们干活,一旦发现master节点宕机,会快速的将slave节点提升为新master节点。
#在所有节点都安装redis yum install redis -y
#所有节点都修改如下: [root@localhost ~]#vim /etc/redis.conf # replicaof <masterip> <masterport> replicaof 10.0.0.8 6379 #指定master的IP和端口号 ....省略 # masterauth <master-password> masterauth 123456 ...省略
# systemctl enable --now redis # ss -ntl ss -State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 511 0.0.0.0:6379 0.0.0.0:* ...省略...
#在master节点查看到如下内容: [root@localhost ~]#redis-cli -a 123456 info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=10.0.0.18,port=6379,state=online,offset=6398,lag=0 slave1:ip=10.0.0.28,port=6379,state=online,offset=6398,lag=1 master_failover_state:no-failover master_replid:1d5939408b470ad048e8e397b2c258836c6caa73 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:6398 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:6398
Sentinel实际上是一个特殊的redis服务器,有些redis指令支持,但很多指令并不支持.默认监听在26379/tcp端口。
##每个节点配置相同 # cp /etc/redis-sentinel.conf{,.bak} # cat > /etc/redis-sentinel.conf<<EOF bind 0.0.0.0 port 26379 daemonize yes pidfile "/var/run/redis-sentinel.pid" logfile "/var/log/sentinel_26379.log" dir "/tmp" sentinel monitor mymaster 10.0.0.8 6379 2 sentinel auth-pass mymaster 123456 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes EOF # mdkri /var/log/redis #创建log目录 # systemctl enable --now redis-sentinel.service # ss -ntl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 511 0.0.0.0:26379 0.0.0.0:* LISTEN 0 511 0.0.0.0:6379 0.0.0.0:* LISTEN 0 128 0.0.0.0:111 0.0.0.0:* LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 100 127.0.0.1:25 0.0.0.0:* LISTEN 0 511 [::1]:6379 [::]:* LISTEN 0 128 [::]:111 [::]:* LISTEN 0 128 [::]:22 [::]:* LISTEN 0 100 [::1]:25 [::]:*
[root@localhost ~]#redis-cli -p 26379 info sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=10.0.0.8:6379,slaves=2,sentinels=1 #查看日志,观察启动过程 [root@localhost ~]#tail -f /var/log/redis/sentinel.log 6206:X 07 Feb 2022 23:30:54.262 # Configuration loaded 6206:X 07 Feb 2022 23:30:54.263 * Increased maximum number of open files to 10032 (it was originally set to 1024). 6206:X 07 Feb 2022 23:30:54.263 * monotonic clock: POSIX clock_gettime 6206:X 07 Feb 2022 23:30:54.264 * Running mode=sentinel, port=26379. 6206:X 07 Feb 2022 23:30:54.266 # Sentinel ID is 9fa9b4b370f49ca201d9d52bb28bb7d41c95ff65 6206:X 07 Feb 2022 23:30:54.266 # +monitor master mymaster 10.0.0.8 6379 quorum 2 6206:X 07 Feb 2022 23:30:54.267 * +slave slave 10.0.0.18:6379 10.0.0.18 6379 @ mymaster 10.0.0.8 6379 6206:X 07 Feb 2022 23:30:54.268 * +slave slave 10.0.0.28:6379 10.0.0.28 6379 @ mymaster 10.0.0.8 6379 6206:X 07 Feb 2022 23:30:55.548 * +sentinel sentinel ca0733135183c8aebf666f068e9134e3adffc362 10.0.0.8 26379 @ mymaster 10.0.0.8 6379 6206:X 07 Feb 2022 23:31:10.040 * +sentinel sentinel 48b97abdf2a0628bb425291e968c25a7ecc6e19d 10.0.0.28 26379 @ mymaster 10.0.0.8 6379
停掉身为master的主机10.0.0.8
# systemctl stop redis
# systemctl start redis
可以发现,当master恢复重新加入回来后,并没有重新获得master,而是以slave身份存在。