Day25 Redis架构实战-Sentinel选取Replica机制

Replica选择切换机制

  • 先剔除不健康的Replica

    • Replica与Master失去连线时间,或是资料落後Master太久的都会优先背剔除
  • 依照Replica-priority进行排序

    • 依照Redis.conf中设定的组态,越小越优先
  • 资料同步复制处理之同步差

    • 如果Replica-priority相同,则会选择与Master资料同步差异最小的优先选择
  • RunID

    • 如果Replica-priority相同且资料同步差也相同,则选择RunID最小的.
    • RunID越小代表Redis Server越先启动的.
  • 在开始测试Redis Server异常自动切换Master前,先确认目前Sentinel丛集状态.

# 连线到sentinel丛集
# 目前监控的redis server master是mymaster中的 IP: 127.0.0.1 Port: 6380
# Sentinel 26379 状态
./redis-cli -h 127.0.0.1 -p 26379
127.0.0.1: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=127.0.0.1:6380,slaves=2,sentinels=3

# Sentinel 26380 状态
./redis-cli -h 127.0.0.1 -p 26380
127.0.0.1:26380> 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=127.0.0.1:6380,slaves=2,sentinels=3

# Sentinel 26381 状态
./redis-cli -h 127.0.0.1 -p 26381
127.0.0.1:26381> 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=127.0.0.1:6380,slaves=2,sentinels=3
  • 连线到目前Redis Server Master确认目前状态.
# 连线至目前Redis Server (Master) 6380
./redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=3204281,lag=0
slave1:ip=127.0.0.1,port=6379,state=online,offset=3204281,lag=0
master_failover_state:no-failover
master_replid:ea3bc11d99568d87b0496fb83c6f2ccc58425baa
master_replid2:a30878092cdd31e404af76eaa7a2d024c8ae843e
master_repl_offset:3204281
second_repl_offset:19299
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2155706
repl_backlog_histlen:1048576
  • 找出目前Redis Server Master并让其异常终止.
# 
ps -ef|grep redis-server

# kill
kill -9 409413
  • 终止目前Redis Server Master Process

https://ithelp.ithome.com.tw/upload/images/20211010/20111658yraKWkA8Rr.png

  • Redis Server Replica 6379 log显示内容

https://ithelp.ithome.com.tw/upload/images/20211010/201116580bUrL8VSL7.png

  • Redis Server Replica 6381 log显示内容

https://ithelp.ithome.com.tw/upload/images/20211010/20111658SNUh1nCKvv.png

  • Sentinel 26379 log 显示侦测到Redis Server Master异常并进行Replica选举,切换Replica为Master.

https://ithelp.ithome.com.tw/upload/images/20211010/20111658NGVilW0EuL.png

  • Sentinel 已将Redis Server Replica 6381切换为Master.
# 原Redis Server Master 6380 已侦测到异常
+sdown master mymaster 127.0.0.1 6380

# 投票选取Sentinel 此次进行切换的Leader
+vote-for-leader 6337212980ee71b6db0222c50ad525c94c6c48a8 1

# 选取Sentinel 26380为此次进行切换的Leader
+config-update-from sentinel 6337212980ee71b6db0222c50ad525c94c6c48a8 127.0.0.1 26380 @ mymaster 127.0.0.1 6380

# 切换Redis Server Master 6381为新的Master取代原有的
switch-master mymaster 127.0.0.1 6380 127.0.0.1 6381

https://ithelp.ithome.com.tw/upload/images/20211010/20111658jwVmlyVCZp.png

  • Redis Server Master 6381 显示已成为Master.

https://ithelp.ithome.com.tw/upload/images/20211010/20111658vGluwurhKw.png

  • Redis Server Replica 6379 与新的Redis Server Master 6381 建立同步.

https://ithelp.ithome.com.tw/upload/images/20211010/20111658nD5SlecJLf.png

  • 将Redis Server 6380 复原重新加入丛集.
Connecting to MASTER 127.0.0.1:6381
Full resync from master: c2f5749e8daef12058d98addb48e954afbc92698:1306638

https://ithelp.ithome.com.tw/upload/images/20211010/20111658RhnTTa0Qor.png

  • Sentinel 会将 Redis Server 6380角色切换为Replica.

https://ithelp.ithome.com.tw/upload/images/20211010/20111658CjI9ohoYam.png

  • 确认结果是否符合异常自动切换的高可用情境.
# 确认Redis Server 6380角色为Replica
./redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:1377655
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c2f5749e8daef12058d98addb48e954afbc92698
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1377655
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1306639
repl_backlog_histlen:71017

# 确认Sentinel与Redis Server都已恢复
./redis-cli -h 127.0.0.1 -p 26379
127.0.0.1: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=127.0.0.1:6381,slaves=2,sentinels=3

<<:  Day-26 : Model 一对一

>>:  [Day 25] vue-router路由传送参数 props

Day15 Sideproject(作品集) from 0 to 1 -刻画面

昨天把整个专案架起来了 今天我们就可以开始来刻画面了 这边也要提一下之前我们其实是直接开写 先从会员...

铁人赛 Day4 -- XAMPP到底有多神 & MySQL的安全性设定

让我告诉你XAMPP有多神 完全免费 容易安装,下载->解压缩->启动->完成 提...

Day-28 手把手的手写辨识模型 0x3:CNN is the end?模型大哉问

快到结尾了,再让笔者水一篇 XDD,今天我们来聊聊 CNN 会不会是深度学习领域的最後呢? CNN...

引导x教练这回事

决定了,就用这个主题当铁人赛30天的终点! 从起始点‧带着满满困惑学习这篇说起吧,文末提到as a ...

How to convert RAW to NTFS file system without losing data?

What should you do if the partition on your extern...