应用开发

Redis多哨兵模式详解!Linux运维培训班

时间:2010-12-5 17:23:32  作者:应用开发   来源:系统运维  查看:  评论:0
内容摘要:哨兵sentinel介绍sentinel是运行在特殊模式下的redis服务器,redis3.0以前的集群一般都是借助哨兵sentinel工具来监控master节点的状态,如果master节点一场,则会

  哨兵sentinel介绍

  sentinel是哨兵运行在特殊模式下的redis服务器,redis3.0以前的模式集群一般都是借助哨兵sentinel工具来监控master节点的状态,如果master节点一场,详解训班则会进行主备切换,维培将slave节点升级为master节点,哨兵性能和高可用方面表现一般,模式特别是详解训班在主从切换瞬间

  | sentinel对节点的判断

  主观下线:

  某个sentinel节点认为(info命令)某个redis节点为不可用状态

  客观下线:

  多个sentinel认为某个redis节点为主观下线状态,将redis节点判断为客观下线,维培并发起一次针对主服务器的哨兵故障转移操作

  | 工作原理

  sentinel读入用户指定的IT技术网配置文件,为每个要被监视的模式redis节点创建相应的实例结构,并创建连向sentinel的详解训班命令连接(用于sentinel连接redis,向redis发送命令请求)和订阅连接(用于接收sentinel:hello频道消息)   每10秒每个sentinel对redis master和slave执行info replication命令,维培该命令用来确认主从关系和发现slave节点   每2秒每个sentinel向redis节点的哨兵sentinel:hello频道发送消息,并根据这些信息为其他sentinel创建相应的模式实例结构,以及命令连接   每1秒每个sentinel对其他sentinel和redis执行ping命令,详解训班用于心跳检测

  多主多从架构图

  环境准备

  | 环境介绍

  | redis配置文件

# 与redis-master112节点部署 cat > /oldboyedu/softwares/redis/redis.conf <<EOF port 6379 daemonize 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 1 save 300 10 save 60 10000 # 配置AOF持久化 appendonly yes appendfsync everysec EOF # 与redis-slave113节点部署 cat > /oldboyedu/softwares/redis/redis.conf <<EOF port 6379 daemonize 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 1 save 300 10 save 60 10000 # 配置AOF持久化 appendonly yes appendfsync everysec EOF # 与redis-slave114节点部署 cat > /oldboyedu/softwares/redis/redis.conf <<EOF port 6379 daemonize 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 1 save 300 10 save 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.1 port 26379 dir /oldboyedu/data/redis26379 sentinel monitor mymaster 10.0.0.112 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel auth-pass mymaster oldboyedu EOF

  redis-slave113

cat > /oldboyedu/softwares/redis26379/sentinel.conf <<EOF bind 10.0.0.113 127.0.0.1 port 26379 dir /oldboyedu/data/redis26379 sentinel monitor mymaster 10.0.0.112 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel auth-pass mymaster oldboyedu EOF

  redis-slave114

cat > /oldboyedu/softwares/redis26379/sentinel.conf <<EOF bind 10.0.0.114 127.0.0.1 port 26379 dir /oldboyedu/data/redis26379 sentinel monitor mymaster 10.0.0.112 6379 2 sentinel down-after-milliseconds mymaster 5000 sentinel 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 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.113,port=6379,state=online,offset=84,lag=0 slave1:ip=10.0.0.114,port=6379,state=online,offset=70,lag=1 master_failover_state:no-failover master_replid:ba904226d8ed537c4e1b187e0db1deb5df9b68f8 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:84 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:84 停止主库运行 # redis-cli -p 6379 -a oldboyedu SHUTDOWN

  | 当主库宕机后,会有其他salve来接管

redis-slave113服务器 # redis-cli -p 6379 -a oldboyedu 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:0 master_failover_state:no-failover master_replid:94676467400ce842b85705c7ee808db640008085 master_replid2:e703f7e7877e9b78659b236caa9eeedc457c8f83 master_repl_offset:3710 second_repl_offset:3711 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:3710 redis-slave114服务器 # redis-cli -p 6379 -a oldboyedu INFO REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. # Replication role:slave master_host:10.0.0.113 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:4543 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:94676467400ce842b85705c7ee808db640008085 master_replid2:e703f7e7877e9b78659b236caa9eeedc457c8f83 master_repl_offset:4543 second_repl_offset:3711 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:4543

  | 恢复master112

  会自动加入集群并成为slave角色

# redis-server /oldboyedu/softwares/redis/redis.conf # redis-cli -p 6379 -a oldboyedu INFO REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. # Replication role:slave master_host:10.0.0.113 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:15166 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:94676467400ce842b85705c7ee808db640008085 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:15166 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:13929 repl_backlog_histlen:1238

  验证sentinel是网站模板否为高可用

  手动停掉任意一个sentinel实例,再次验证主从结构能否正常使用

  | 将redis-master112的sentinel实例停用

# netstat -untalp | grep 26379 | grep LISTEN tcp 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 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.114,port=6379,state=online,offset=64400,lag=1 slave1:ip=10.0.0.112,port=6379,state=online,offset=64414,lag=0 master_failover_state:no-failover master_replid:94676467400ce842b85705c7ee808db640008085 master_replid2:e703f7e7877e9b78659b236caa9eeedc457c8f83 master_repl_offset:64414 second_repl_offset:3711 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:64414 # redis-cli -p 6379 -a oldboyedu SHUTDOWN Warning: Using a password with -a or -u option on the command line interface may not be safe. # redis-cli -p 6379 -a oldboyedu INFO REPLICATION Warning: 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 REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. # Replication role:master connected_slaves:1 slave0:ip=10.0.0.114,port=6379,state=online,offset=94729,lag=1 master_failover_state:no-failover master_replid:46266625c5a45935c25e0dc3a6121e481cc23e77 master_replid2:94676467400ce842b85705c7ee808db640008085 master_repl_offset:94864 second_repl_offset:78055 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:13929 repl_backlog_histlen:80936

  | 恢复slave113节点

redis-server /oldboyedu/softwares/redis/redis.conf

  | 额外停掉一台sentinel,发现不能正常切换主从

  因为目前只有一个sentinel存活,尽管它能进行投票也只能投递1票

# netstat -untalp | grep 26379 | grep LISTEN tcp 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 SHUTDOWN Warning: 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 REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. # Replication role:slave master_host:10.0.0.112 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_repl_offset:148032 master_link_down_since_seconds:55 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:46266625c5a45935c25e0dc3a6121e481cc23e77 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:148032 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:124049 repl_backlog_histlen:23984 于redis-slave114节点查看 # redis-cli -p 6379 -a oldboyedu INFO REPLICATION Warning: Using a password with -a or -u option on the command line interface may not be safe. # Replication role:slave master_host:10.0.0.112 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:0 slave_repl_offset:148032 master_link_down_since_seconds:58 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:46266625c5a45935c25e0dc3a6121e481cc23e77 master_replid2:94676467400ce842b85705c7ee808db640008085 master_repl_offset:148032 second_repl_offset:78055 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:148032b2b信息网
copyright © 2025 powered by 编程之道  滇ICP备2023006006号-34sitemap