Redis 哨兵模式(故障自动切换)用户无感知
2022-08-07 本文已影响0人
无味wy
资源清单
#首先搭建一主两从复制环境、这里不做详细介绍 之前的文章有写过
主:10.0.0.100
从:10.0.0.101
从:10.0.0.102
三个节点分别启动一个哨兵
哨兵sentinel配置
vim /usr/local/redis/sentinel.conf
port 26379
daemonize yes
protected-mode no
pidfile "/var/run/redis-sentinel_26379.pid"
logfile "/data/logs/redis/26379.log"
#设定监控地址,为对应的主redis库的内网地址,mymaster为集群名字,可以根据情况自己修改
sentinel monitor mymaster 10.0.0.100 6379 2
#指定了Sentinel认为服务器已经断线所需的毫秒数。
sentinel down-after-milliseconds mymaster 3000
#设置集群从判断节点挂掉,到执行故障转移操作(即重新选举master节点)的时间
sentinel failover-timeout mymaster 10000
#主数据库密码,需要将配置放在sentinel monitor mymaster 10.0.0.100 6379 2下面
sentinel auth-pass mymaster 123
启动哨兵
10.0.0.100 redis-sentinel /usr/local/redis/sentinel.conf
10.0.0.101 redis-sentinel /usr/local/redis/sentinel.conf
10.0.0.102 redis-sentinel /usr/local/redis/sentinel.conf
查看进程
[root@redis01 redis]# redis-sentinel /usr/local/redis/sentinel.conf
[root@redis01 redis]# ps -ef|grep redis
root 12179 1 0 16:55 ? 00:00:00 redis-server 0.0.0.0:6379
root 12206 1 0 17:02 ? 00:00:00 redis-sentinel *:26379 [sentinel]
root 12212 11984 0 17:03 pts/0 00:00:00 grep --color=auto redis
查看哨兵状态
redis-cli -h 10.0.0.100 -p 26379 -a 123 info Sentinel
[root@redis01 redis]# redis-cli -h 10.0.0.100 -p 26379 -a 123 info Sentinel
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: AUTH failed
# 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.100:6379,slaves=2,sentinels=3
杀死主库进行故障测试
10.0.0.100 pkill redis-server
#30秒后再启动
10.0.0.100 redis-server /usr/local/redis/redis.conf
查看哨兵状态及主库的调度情况
[root@redis01 redis]# redis-cli -h 10.0.0.100 -p 26379 -a 123 info Sentinel
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: AUTH failed
# 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.101:6379,slaves=2,sentinels=3
#状态ok # IP已切换到10.0.0.101 #从库2个 #哨兵3个 !正常 !
测试期间带有一些key,过程也添加了一些、只要用默认的配置做RDB持久化数据不会丢失