sentinel是运行在特殊模式下的redis服务器,redis3.0以前的集群一般都是借助哨兵sentinel工具来监控master节点的状态,如果master节点一场,则会进行 主备切换,将slave节点升级为master节点,性能和高可用方面表现一般,特别是在主从切换瞬间
| sentinel对节点的判断
主观下线:
某个sentinel节点认为(info命令)某个redis节点为不可用状态
客观下线:
多个sentinel认为某个redis节点为主观下线状态,将redis节点判断为客观下线,并发起一次针对主服务器的故障转移操作
| 工作原理
多主多从架构图

环境准备
| 环境介绍

| redis配置文件
# 与redis-master112节点部署cat > /oldboyedu/softwares/redis/redis.conf <<EOF port 6379daemonize yes pidfile "/oldboyedu/data/redis/redis.pid"loglevel notice logfile "/oldboyedu/data/redis/redis.log"dbfilename "dump.rdb"dir "/oldboyedu/data/redis"requirepass "oldboyedu"masterauth "oldboyedu"bind 10.0.0.112 127.0.0.1# 配置RDB持久化策略save 900 1save 300 10save 60 10000# 配置AOF持久化appendonly yes appendfsync everysec EOF# 与redis-slave113节点部署cat > /oldboyedu/softwares/redis/redis.conf <<EOF port 6379daemonize yes pidfile "/oldboyedu/data/redis/redis.pid"loglevel notice logfile "/oldboyedu/data/redis/redis.log"dbfilename "dump.rdb"dir "/oldboyedu/data/redis"requirepass "oldboyedu"masterauth "oldboyedu"bind 10.0.0.113 127.0.0.1# 配置RDB持久化策略save 900 1save 300 10save 60 10000# 配置AOF持久化appendonly yes appendfsync everysec EOF# 与redis-slave114节点部署cat > /oldboyedu/softwares/redis/redis.conf <<EOF port 6379daemonize yes pidfile "/oldboyedu/data/redis/redis.pid"loglevel notice logfile "/oldboyedu/data/redis/redis.log"dbfilename "dump.rdb"dir "/oldboyedu/data/redis"requirepass "oldboyedu"masterauth "oldboyedu"bind 10.0.0.114 127.0.0.1# 配置RDB持久化策略save 900 1save 300 10save 60 10000# 配置AOF持久化appendonly yes appendfsync everysec EOF
| 启动服务
# 全部节点执行redis-server /oldboyedu/softwares/redis/redis.conf# slave节点执行redis-cli -p 6379 -a oldboyedu SLAVEOF 10.0.0.112 6379
部署多sentinel实例
| 部署sentinel实例
# 全部节点执行mkdir -pv /oldboyedu/softwares/redis26379 mkdir -pv /oldboyedu/data/redis26379 mkdir -pv /oldboyedu/logs/redis26379
| 创建配置文件
redis-master112主机
cat > /oldboyedu/softwares/redis26379/sentinel.conf <<EOF bind 10.0.0.112 127.0.0.1port 26379dir /oldboyedu/data/redis26379 sentinel monitor mymaster 10.0.0.112 6379 2sentinel down-after-milliseconds mymaster 5000sentinel auth-pass mymaster oldboyedu EOF
redis-slave113
cat > /oldboyedu/softwares/redis26379/sentinel.conf <<EOF bind 10.0.0.113 127.0.0.1port 26379dir /oldboyedu/data/redis26379 sentinel monitor mymaster 10.0.0.112 6379 2sentinel down-after-milliseconds mymaster 5000sentinel auth-pass mymaster oldboyedu EOF
redis-slave114
cat > /oldboyedu/softwares/redis26379/sentinel.conf <<EOF bind 10.0.0.114 127.0.0.1port 26379dir /oldboyedu/data/redis26379 sentinel monitor mymaster 10.0.0.112 6379 2sentinel down-after-milliseconds mymaster 5000sentinel auth-pass mymaster oldboyedu EOF
| 启动sentinel进程
# 全部节点执行redis-sentinel /oldboyedu/softwares/redis26379/sentinel.conf &> /oldboyedu/logs/redis26379/sentinel.log &
| 启动sentinel进程

验证Redis是否能够自动切换
| 手动停止主库运行,模拟主库宕机
主库上查看当前主从状态# redis-cli -p 6379 -a oldboyedu INFO REPLICATIONWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# Replicationrole:master connected_slaves:2slave0:ip=10.0.0.113,port=6379,state=online,offset=84,lag=0slave1:ip=10.0.0.114,port=6379,state=online,offset=70,lag=1master_failover_state:no-failover master_replid:ba904226d8ed537c4e1b187e0db1deb5df9b68f8 master_replid2:0000000000000000000000000000000000000000master_repl_offset:84second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:84停止主库运行# redis-cli -p 6379 -a oldboyedu SHUTDOWN
| 当主库宕机后,会有其他salve来接管
redis-slave113服务器# redis-cli -p 6379 -a oldboyedu INFO REPLICATIONWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# Replicationrole:master connected_slaves:0master_failover_state:no-failover master_replid:94676467400ce842b85705c7ee808db640008085master_replid2:e703f7e7877e9b78659b236caa9eeedc457c8f83 master_repl_offset:3710second_repl_offset:3711repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:3710redis-slave114服务器# redis-cli -p 6379 -a oldboyedu INFO REPLICATIONWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# Replicationrole:slave master_host:10.0.0.113master_port:6379master_link_status:up master_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:4543slave_priority:100slave_read_only:1replica_announced:1connected_slaves:0master_failover_state:no-failover master_replid:94676467400ce842b85705c7ee808db640008085master_replid2:e703f7e7877e9b78659b236caa9eeedc457c8f83 master_repl_offset:4543second_repl_offset:3711repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:4543
| 恢复master112
会自动加入集群并成为slave角色
# redis-server /oldboyedu/softwares/redis/redis.conf# redis-cli -p 6379 -a oldboyedu INFO REPLICATIONWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# Replicationrole:slave master_host:10.0.0.113master_port:6379master_link_status:up master_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:15166slave_priority:100slave_read_only:1replica_announced:1connected_slaves:0master_failover_state:no-failover master_replid:94676467400ce842b85705c7ee808db640008085master_replid2:0000000000000000000000000000000000000000master_repl_offset:15166second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:13929repl_backlog_histlen:1238
验证sentinel是否为高可用
手动停掉任意一个sentinel实例,再次验证主从结构能否正常使用
| 将redis-master112的sentinel实例停用
# netstat -untalp | grep 26379 | grep LISTENtcp 0 0 127.0.0.1:26379 0.0.0.0:* LISTEN 1162/redis-sentinel tcp 0 0 10.0.0.112:26379 0.0.0.0:* LISTEN 1162/redis-sentinel# kill -9 1162[1]+ Killed redis-sentinel /oldboyedu/softwares/redis26379/sentinel.conf &>/oldboyedu/logs/redis26379/sentinel.log
| 发现依旧是可以正常切换主从的
因为做上面的实验时,master节点转移到slave113服务器上了,所以以下操作为在slave113服务器执行# redis-cli -p 6379 -a oldboyedu INFO REPLICATIONWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# Replicationrole:master connected_slaves:2slave0:ip=10.0.0.114,port=6379,state=online,offset=64400,lag=1slave1:ip=10.0.0.112,port=6379,state=online,offset=64414,lag=0master_failover_state:no-failover master_replid:94676467400ce842b85705c7ee808db640008085master_replid2:e703f7e7877e9b78659b236caa9eeedc457c8f83 master_repl_offset:64414second_repl_offset:3711repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:64414# redis-cli -p 6379 -a oldboyedu SHUTDOWNWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# redis-cli -p 6379 -a oldboyedu INFO REPLICATIONWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.Could not connect to Redis at 127.0.0.1:6379: Connection refused
| master节点转移到master112服务器上了
在master112服务器上执行# redis-cli -p 6379 -a oldboyedu INFO REPLICATIONWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# Replicationrole:master connected_slaves:1slave0:ip=10.0.0.114,port=6379,state=online,offset=94729,lag=1master_failover_state:no-failover master_replid:46266625c5a45935c25e0dc3a6121e481cc23e77master_replid2:94676467400ce842b85705c7ee808db640008085master_repl_offset:94864second_repl_offset:78055repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:13929repl_backlog_histlen:80936
| 恢复slave113节点
redis-server /oldboyedu/softwares/redis/redis.conf
| 额外停掉一台sentinel,发现不能正常切换主从
因为目前只有一个sentinel存活,尽管它能进行投票也只能投递1票
# netstat -untalp | grep 26379 | grep LISTENtcp 0 0 127.0.0.1:26379 0.0.0.0:* LISTEN 1178/redis-sentinel tcp 0 0 10.0.0.113:26379 0.0.0.0:* LISTEN 1178/redis-sentinel# kill -9 1178[1]+ Killed redis-sentinel /oldboyedu/softwares/redis26379/sentinel.conf &>/oldboyedu/logs/redis26379/sentinel.log
发现不能正常切换主从
因为目前只有一个sentinel存活,尽管它能进行投票也只能投递1票,无法我们在配置文件中定义的"sentinel monitor mymaster 10.0.0.112 6379 2"中的"2"
由此可以看出,主节点并未发送改变
停掉redis-master112节点的redis运行# redis-cli -p 6379 -a oldboyedu SHUTDOWNWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.于redis-slave113节点查看# redis-cli -p 6379 -a oldboyedu INFO REPLICATIONWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# Replicationrole:slave master_host:10.0.0.112master_port:6379master_link_status:down master_last_io_seconds_ago:-1master_sync_in_progress:0slave_repl_offset:148032master_link_down_since_seconds:55slave_priority:100slave_read_only:1replica_announced:1connected_slaves:0master_failover_state:no-failover master_replid:46266625c5a45935c25e0dc3a6121e481cc23e77master_replid2:0000000000000000000000000000000000000000master_repl_offset:148032second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:124049repl_backlog_histlen:23984于redis-slave114节点查看# redis-cli -p 6379 -a oldboyedu INFO REPLICATIONWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# Replicationrole:slave master_host:10.0.0.112master_port:6379master_link_status:down master_last_io_seconds_ago:-1master_sync_in_progress:0slave_repl_offset:148032master_link_down_since_seconds:58slave_priority:100slave_read_only:1replica_announced:1connected_slaves:0master_failover_state:no-failover master_replid:46266625c5a45935c25e0dc3a6121e481cc23e77master_replid2:94676467400ce842b85705c7ee808db640008085master_repl_offset:148032second_repl_offset:78055repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:148032
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69952968/viewspace-3103176/ ,如需转载,请注明出处,否则将追究法律责任。