iT邦幫忙

2021 iThome 鐵人賽

DAY 22
0
Software Development

Redis還在學系列 第 22

Day22 Redis架構實戰-高可用性使用Sentinel機制

Redis高可用性

# 高可用配置
                           --->   Redis (Replica)
client ---> Redis (Master)
                           --->   Redis (Replica)

# 當Master掛掉時,會把Replica切換為Master提供用戶端服務


Client ----------------------->   Redis (Master)
Client                                  | 同步
            Redis (X)                   V
                                  Redis (Replica)
  • 依照以上的配置,當Master掛掉時,會把Replica切換為Master提供用戶端服務,如下驗證一下此情境是否如期切換。
# 關閉Redis Server (Master) 6379

# 確認Redis Server (Replica) 6380 角色
# role:slave
./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:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:2590
master_link_down_since_seconds:327
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:943718468a346457e58ad233607f464998e6159c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2590
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2590

# 確認Redis Server (Replica) 6381 角色
# role:slave
./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:2590
master_link_down_since_seconds:355
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:943718468a346457e58ad233607f464998e6159c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2590
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:2576

https://ithelp.ithome.com.tw/upload/images/20211007/20111658LemgqToA56.png

https://ithelp.ithome.com.tw/upload/images/20211007/20111658Ro6NEi8Mcf.png

  • 依照以上的情境驗證看起來2台Replica並沒有自動選定一台轉換為Master,這樣跟情境預期不一致。主要是因為Redis Server並非透過自動透過多數成員投票機制進行選取Master,此時可以透過手動自己進行切換。
# 將Redis Server (Replica) 6381 手動轉換為Master
./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> replicaof no one
OK
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:458e0f8b3f40adb50a3dca8cc90e76b96937107b
master_replid2:943718468a346457e58ad233607f464998e6159c
master_repl_offset:2590
second_repl_offset:2591
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:2576

# 將Redis Server (Replica) 6380 與 6381建立Replication
./redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> replicaof 127.0.0.1 6381
OK
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:9
master_sync_in_progress:0
slave_repl_offset:2604
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:458e0f8b3f40adb50a3dca8cc90e76b96937107b
master_replid2:943718468a346457e58ad233607f464998e6159c
master_repl_offset:2604
second_repl_offset:2591
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2604

# 確認目前架構 Redis Server (Master) 6381 與 Redis Server (Replica) 6380
./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=2632,lag=1
master_failover_state:no-failover
master_replid:458e0f8b3f40adb50a3dca8cc90e76b96937107b
master_replid2:943718468a346457e58ad233607f464998e6159c
master_repl_offset:2632
second_repl_offset:2591
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:2618
  • 當原有異常的Master修復後重新加入高可用時,需要轉換為現有Master的Replica,不影響用戶端服務操作。
# 啟動服務 6379
$ ./redis-server redis_6379.conf

# Redis Server (Master) 6379 目前角色為Master
 ./redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:0f0546acb0aecd970497a0dac52e71539d3b2f16
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# 將 Redis Server (Master) 6379 角色轉換為 Replica
127.0.0.1:6379> replicaof 127.0.0.1 6381
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:4018
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:458e0f8b3f40adb50a3dca8cc90e76b96937107b
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:4018
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:4019
repl_backlog_histlen:0

# 確認目前高可用架構
$ ./redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6380,state=online,offset=4046,lag=0
slave1:ip=127.0.0.1,port=6379,state=online,offset=4046,lag=0
master_failover_state:no-failover
master_replid:458e0f8b3f40adb50a3dca8cc90e76b96937107b
master_replid2:943718468a346457e58ad233607f464998e6159c
master_repl_offset:4046
second_repl_offset:2591
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:4032

Redis 高可用使用Sentinel機制

  • 透過人為手動機制雖然可以達成情境所需要的結果,但是實務上還是會有Redis服務操作中斷的問題,故我們希望的高可用架構是可以不中斷服務操作的,此時需要經由第三方sentinel機制來達成自動切換的方式,不需要人為介入就能將某個Replica轉換成為新的Master.

  • Sentinel功能

    • 監控
    • 通知
    • 失效自動切換
    • 組態設定提供
  • sentinel組態設定

cp /home/redis/redis-6.2.5/sentinel.conf sentinel_26379.conf

vi sentinel_26379.conf

# 修改以下項目組態設定
port 26379
daemonize yes
logfile "/tmp/redis-sentinel_26379.log"
dir /tmp
sentinel monitor mymaster 127.0.0.1 6379 1
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000

https://ithelp.ithome.com.tw/upload/images/20211007/20111658Slbsnyzx4U.png

https://ithelp.ithome.com.tw/upload/images/20211007/20111658OQeSjI4svI.png

https://ithelp.ithome.com.tw/upload/images/20211007/201116582taTcpnNfp.png

https://ithelp.ithome.com.tw/upload/images/20211007/20111658As1oDtKG7y.png

https://ithelp.ithome.com.tw/upload/images/20211007/201116584S7QGXolNf.png

https://ithelp.ithome.com.tw/upload/images/20211007/20111658jJlXZ48Pji.png

https://ithelp.ithome.com.tw/upload/images/20211007/201116580hVnsegV3I.png

https://ithelp.ithome.com.tw/upload/images/20211007/201116588AATxuHWsf.png

  • 啟動sentinel
./redis-sentinel sentinel_26379.conf 
# 確認是否已經啟動sentinel
ps -ef|grep redis-s

https://ithelp.ithome.com.tw/upload/images/20211007/20111658S5Ngp9mGEZ.png

  • 關閉Redis Server (Master)確認是否自動切換
# 關閉Redis Server (Master) 6379

# 連線至Redis Server (Replica) 6380 確認角色
./redis-cli -h 127.0.0.1 -p 6380

# 確認Redis Server (Replica) 6380 已自動切換Redis Server (Master) 6380
# 當Redis Server (Master) 6379 修復後會轉為Redis Server (Replica) 6379
127.0.0.1:6380> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6381,state=online,offset=23418,lag=0
slave1:ip=127.0.0.1,port=6379,state=online,offset=23551,lag=0
master_failover_state:no-failover
master_replid:ea3bc11d99568d87b0496fb83c6f2ccc58425baa
master_replid2:a30878092cdd31e404af76eaa7a2d024c8ae843e
master_repl_offset:23551
second_repl_offset:19299
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:71
repl_backlog_histlen:23481

上一篇
Day21 Redis架構實戰-高可用性
下一篇
Day23 Redis架構實戰-Sentinel組態檔設定
系列文
Redis還在學30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言