redis-哨兵模式(sentinel)
1、基础
1.1、什么是哨兵模式(sentinel)
能够后台监控主机是否故障,如果故障了根据投票数自动将从库转换为主库
1.2、流程图
2、环境准备
2.1、调整为一主二仆模式,6379带着6380、6381
redis-server /usr/local/redis-6.2.5/etc/6379.conf redis-server /usr/local/redis-6.2.5/etc/6380.conf redis-server /usr/local/redis-6.2.5/etc/6381.conf # 从节点 redis-cli -p 6381 slaveof 192.168.10.19 6379 redis-cli -p 6380 slaveof 192.168.10.19 6379
2.2、复制三个哨兵的配置文件
cp sentinel.conf sentinel_26379.conf
cp sentinel.conf sentinel_26380.conf
cp sentinel.conf sentinel_26381.conf
3、哨兵模式(sentinel)配置
3.1、sentinel的配置
3.1.1、sentinel_26379.conf
]# cat sentinel_26379.conf | egrep -v '^$|^#' bind 192.168.10.19 port 26379 daemonize yes pidfile /var/run/redis-sentinel_26379.pid logfile "/usr/local/redis-6.2.5/log/sentinel_26379.log" dir /tmp sentinel monitor mymaster 192.168.10.19 6379 2 sentinel down-after-milliseconds mymaster 30000 acllog-max-len 128 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes SENTINEL resolve-hostnames no SENTINEL announce-hostnames no
3.1.2、sentinel_26380.conf
]# cat sentinel_26380.conf | egrep -v '^$|^#' bind 192.168.10.19 port 26380 daemonize yes pidfile /var/run/redis-sentinel_26380.pid logfile "/usr/local/redis-6.2.5/log/sentinel_26380.log" dir /tmp sentinel monitor mymaster 192.168.10.19 6379 2 sentinel down-after-milliseconds mymaster 30000 acllog-max-len 128 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes SENTINEL resolve-hostnames no SENTINEL announce-hostnames no
3.1.3、sentinel_26381.conf
]# cat sentinel_26381.conf | egrep -v '^$|^#' bind 192.168.10.19 port 26381 daemonize yes pidfile /var/run/redis-sentinel.pid logfile "/usr/local/redis-6.2.5/log/sentinel_26381.log" dir /tmp sentinel monitor mymaster 192.168.10.19 6379 2 sentinel down-after-milliseconds mymaster 30000 acllog-max-len 128 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes SENTINEL resolve-hostnames no SENTINEL announce-hostnames no
3.2、启动哨兵并且检查状态
3.2.1、启动哨兵
redis-sentinel /usr/local/redis-6.2.5/etc/sentinel_26379.conf redis-sentinel /usr/local/redis-6.2.5/etc/sentinel_26380.conf redis-sentinel /usr/local/redis-6.2.5/etc/sentinel_26381.conf
3.2.2、查询端口是否开启
]# netstat -tunlp | grep sen tcp 0 0 192.168.10.19:26379 0.0.0.0:* LISTEN 29170/redis-sentine tcp 0 0 192.168.10.19:26380 0.0.0.0:* LISTEN 29177/redis-sentine tcp 0 0 192.168.10.19:26381 0.0.0.0:* LISTEN 29183/redis-sentine
3.3、查询启动日志
3.3.1、sentinel_26379.log
]# cat sentinel_26379.log 29170:X 16 May 2023 18:09:13.559 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 29170:X 16 May 2023 18:09:13.559 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=29170, just started 29170:X 16 May 2023 18:09:13.559 # Configuration loaded 29170:X 16 May 2023 18:09:13.577 * monotonic clock: POSIX clock_gettime 29170:X 16 May 2023 18:09:13.577 * Running mode=sentinel, port=26379. 29170:X 16 May 2023 18:09:13.578 # Sentinel ID is 4454006c07746963438acf4bb2a6bc45abed9332 29170:X 16 May 2023 18:09:13.578 # +monitor master mymaster 192.168.10.19 6379 quorum 2 29170:X 16 May 2023 18:09:13.579 * +slave slave 192.168.10.19:6380 192.168.10.19 6380 @ mymaster 192.168.10.19 6379 29170:X 16 May 2023 18:09:13.580 * +slave slave 192.168.10.19:6381 192.168.10.19 6381 @ mymaster 192.168.10.19 6379 29170:X 16 May 2023 18:09:24.626 * +sentinel sentinel aefeef40e1435d815ade585d958159e44ebb6152 192.168.10.19 26380 @ mymaster 192.168.10.19 6379 29170:X 16 May 2023 18:09:27.387 * +sentinel sentinel b2cd40043a818171575865fed4c31195af78f5d7 192.168.10.19 26381 @ mymaster 192.168.10.19 6379
3.3.2、sentinel_26380.log
]# cat sentinel_26380.log 29177:X 16 May 2023 18:09:22.598 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 29177:X 16 May 2023 18:09:22.598 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=29177, just started 29177:X 16 May 2023 18:09:22.598 # Configuration loaded 29177:X 16 May 2023 18:09:22.598 * monotonic clock: POSIX clock_gettime 29177:X 16 May 2023 18:09:22.599 * Running mode=sentinel, port=26380. 29177:X 16 May 2023 18:09:22.600 # Sentinel ID is aefeef40e1435d815ade585d958159e44ebb6152 29177:X 16 May 2023 18:09:22.600 # +monitor master mymaster 192.168.10.19 6379 quorum 2 29177:X 16 May 2023 18:09:22.600 * +slave slave 192.168.10.19:6380 192.168.10.19 6380 @ mymaster 192.168.10.19 6379 29177:X 16 May 2023 18:09:22.601 * +slave slave 192.168.10.19:6381 192.168.10.19 6381 @ mymaster 192.168.10.19 6379 29177:X 16 May 2023 18:09:23.672 * +sentinel sentinel 4454006c07746963438acf4bb2a6bc45abed9332 192.168.10.19 26379 @ mymaster 192.168.10.19 6379 29177:X 16 May 2023 18:09:27.386 * +sentinel sentinel b2cd40043a818171575865fed4c31195af78f5d7 192.168.10.19 26381 @ mymaster 192.168.10.19 6379
3.3.3、sentinel_26381.log
]# cat sentinel_26381.log 29183:X 16 May 2023 18:09:25.383 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 29183:X 16 May 2023 18:09:25.384 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=29183, just started 29183:X 16 May 2023 18:09:25.384 # Configuration loaded 29183:X 16 May 2023 18:09:25.384 * monotonic clock: POSIX clock_gettime 29183:X 16 May 2023 18:09:25.384 * Running mode=sentinel, port=26381. 29183:X 16 May 2023 18:09:25.386 # Sentinel ID is b2cd40043a818171575865fed4c31195af78f5d7 29183:X 16 May 2023 18:09:25.386 # +monitor master mymaster 192.168.10.19 6379 quorum 2 29183:X 16 May 2023 18:09:25.386 * +slave slave 192.168.10.19:6380 192.168.10.19 6380 @ mymaster 192.168.10.19 6379 29183:X 16 May 2023 18:09:25.387 * +slave slave 192.168.10.19:6381 192.168.10.19 6381 @ mymaster 192.168.10.19 6379 29183:X 16 May 2023 18:09:25.695 * +sentinel sentinel 4454006c07746963438acf4bb2a6bc45abed9332 192.168.10.19 26379 @ mymaster 192.168.10.19 6379 29183:X 16 May 2023 18:09:26.728 * +sentinel sentinel aefeef40e1435d815ade585d958159e44ebb6152 192.168.10.19 26380 @ mymaster 192.168.10.19 6379
3.4、查询sentinel状态
]# redis-cli -h 192.168.10.19 -p 26379 192.168.10.19: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=192.168.10.19:6379,slaves=2,sentinels=3
4、模拟故障
4.1、将6379下掉
redis-cli -p 6379 shutdown
4.2、查看哨兵状态是否切换新的master
]# redis-cli -h 192.168.10.19 -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=192.168.10.19:6381,slaves=2,sentinels=3 # 已切换新节点
4.3、查询角色
]# redis-cli -p 6380 info replication | grep role role:slave
]# redis-cli -p 6381 info replication | grep role role:master
4.4、恢复原来的节点
4.4.1、恢复6379节点
redis-server /usr/local/redis-6.2.5/etc/6379.conf
4.4.2、发现原来的节点,已经变为从节点
]# redis-cli -p 6379 info replication | grep role role:slave
5、故障恢复
优先级在redis.conf中默认:slave-priority 100,值越小优先级越高
偏移量是指获得原主机数据最全的
每个redis实例启动后都会随机生成一个40位的runid