1. 程式人生 > >Redis 高可用:Redis Sentinel 主從複製故障轉移

Redis 高可用:Redis Sentinel 主從複製故障轉移

Redis Sentinel  為 Redis 提供了高可用,可對複製叢集中進行監控、通知、故障轉移。

伺服器名稱:Centos222 , ip :192.168.1.222 ,主從角色:master

伺服器名稱:Centos224 , ip :192.168.1.224 ,主從角色:slave

在複製的基礎上,現在配置 Redis Sentinel ,基本結構如下。


# 222 配置 redis 啟動引數

[[email protected] ~]# vi /etc/redis/6379.conf

bind 127.0.0.1 192.168.1.222	#redis伺服器地址
port 26379			#sentinel埠號
daemonize yes		#後臺啟動
protected-mode no	#關閉保護模式,禁止遠端直接訪問
requirepass 654321	#本地訪問需要密碼
masterauth 654321	#備端同步訪問需要密碼
slave-read-only yes	#備端只讀

# 224 配置redis 啟動引數
[[email protected] ~]# vi /etc/redis/6379.conf

bind 127.0.0.1 192.168.1.224	#redis伺服器地址
port 26379			#sentinel埠號
daemonize yes		#後臺啟動
slaveof 192.168.1.222 6379		#設定為192.168.1.222 6379的從庫
protected-mode no	#關閉保護模式,禁止遠端直接訪問
requirepass 654321	#本地訪問需要密碼
masterauth 654321	#備端同步訪問需要密碼
slave-read-only yes	#備端只讀

# 配置 Redis Sentinel ,只配置主例項的監控即可。Sentinel 可以通過 master 例項獲取從例項的資訊。

# 基本引數說明:

port 26379	#Sentinel 服務埠
sentinel monitor myredis 192.168.1.xxx 6379 1	#監控 人員組名/ip/埠/失效判定數(多少從端判斷不同則認為master斷開)
sentinel down-after-milliseconds myredis 10000	#ping 超過 10000 毫秒則認為宕機
sentinel failover-timeout myredis 900000		#主從切換超過 30000 毫秒則認為失敗
sentinel can-failover myredis yes				#master斷開後是否允許判斷進行故障轉移
sentinel parallel-syncs myredis 1				#從端繼續同步新master的數量


# 222 redis sentinel 配置

[[email protected] ~]# vi /etc/redis/sentinel.conf

#ip 192.168.1.222
port 26379
bind 0.0.0.0
daemonize yes
logfile "/var/log/sentinel_log.log"
#myredis222
sentinel monitor myredis222 192.168.1.222 6379 1
sentinel down-after-milliseconds myredis222 10000
sentinel failover-timeout myredis222 900000
sentinel parallel-syncs myredis222 1
sentinel auth-pass myredis222 654321
#myredis224
sentinel monitor myredis224 192.168.1.224 6379 1
sentinel down-after-milliseconds myredis224 10000
sentinel failover-timeout myredis224 900000
sentinel parallel-syncs myredis224 1
sentinel auth-pass myredis224 654321

# 224 redis sentinel 配置
[[email protected] ~]#  vi /etc/redis/sentinel.conf

#ip 192.168.1.224
port 26379
bind 0.0.0.0
daemonize yes
logfile "/var/log/sentinel_log.log"
#myredis222
sentinel monitor myredis222 192.168.1.222 6379 1
sentinel down-after-milliseconds myredis222 10000
sentinel failover-timeout myredis222 900000
sentinel parallel-syncs myredis222 1
sentinel auth-pass myredis222 654321
#myredis224
sentinel monitor myredis224 192.168.1.224 6379 1
sentinel down-after-milliseconds myredis224 10000
sentinel failover-timeout myredis224 900000
sentinel parallel-syncs myredis224 1
sentinel auth-pass myredis224 654321

# 伺服器 222 和 224 防火牆新增入站規則
iptables -I INPUT -p tcp --dport 26379 -j ACCEPT

# 啟動 sentinel 服務兩種方法:
redis-sentinel /etc/redis/sentinel.conf
redis-server /etc/redis/sentinel.conf --sentinel

# 222 啟動 sentinel 服務
[[email protected] ~]# redis-sentinel /etc/redis/sentinel.conf
[[email protected] ~]# cat /var/log/sentinel_log.log
24630:X 26 Nov 18:00:50.496 # Sentinel ID is b0c01ccf191b07bc7f8f42c395b1f45ec3849258
24630:X 26 Nov 18:00:50.496 # +monitor master myredis224 192.168.1.224 6379 quorum 1
24630:X 26 Nov 18:00:50.496 # +monitor master myredis222 192.168.1.222 6379 quorum 1
24630:X 26 Nov 18:00:50.497 * +slave slave 192.168.1.224:6379 192.168.1.224 6379 @ myredis222 192.168.1.222 6379

# 224 啟動 sentinel 服務

[[email protected] ~]# redis-sentinel /etc/redis/sentinel.conf
[[email protected] ~]# cat /var/log/sentinel_log.log
29098:X 26 Nov 07:00:33.795 # Sentinel ID is 2303678ad7c6df83ce9c938ed9341fc33170f843
29098:X 26 Nov 07:00:33.795 # +monitor master myredis222 192.168.1.222 6379 quorum 1
29098:X 26 Nov 07:00:33.795 # +monitor master myredis224 192.168.1.224 6379 quorum 1
29098:X 26 Nov 07:00:33.797 * +slave slave 192.168.1.224:6379 192.168.1.224 6379 @ myredis222 192.168.1.222 6379
29098:X 26 Nov 07:00:34.560 * +sentinel sentinel b0c01ccf191b07bc7f8f42c395b1f45ec3849258 192.168.1.222 26379 @ myredis222 192.168.1.222 6379
29098:X 26 Nov 07:00:34.706 * +sentinel sentinel b0c01ccf191b07bc7f8f42c395b1f45ec3849258 192.168.1.222 26379 @ myredis224 192.168.1.224 6379

# 出現類似上面的資訊,說明成功了。 

# 連線 Sentinel ,檢視資訊(以224客戶端連線為例)

[[email protected] ~]# redis-cli -p 26379
127.0.0.1:26379> info Sentinel
# Sentinel
sentinel_masters:2
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=myredis222,status=ok,address=192.168.1.222:6379,slaves=1,sentinels=2
master1:name=myredis224,status=odown,address=192.168.1.224:6379,slaves=0,sentinels=2
127.0.0.1:26379> 

# 可以看到 Sentinel 兩個有2個master,其中 192.168.1.22 有1個slave。此時分別連線到 222 和 224 上檢視複製資訊。

# 在222 伺服器連線redis:redis 角色為 master

[[email protected] ~]# redis-cli
127.0.0.1:6379> auth 654321
OK
127.0.0.1:6379> info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.224,port=6379,state=online,offset=2088480,lag=1
master_repl_offset:2088623
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1040048
repl_backlog_histlen:1048576
127.0.0.1:6379> 

# 在224 伺服器連線redis:redis 角色為 slave
[[email protected] ~]# redis-cli
127.0.0.1:6379> auth 654321
OK
127.0.0.1:6379> info Replication
# Replication
role:slave
master_host:192.168.1.222
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:2087322
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# 不急,先介紹 redis sentinel 相關命令
127.0.0.1:26379> SENTINEL masters			#列出所有master狀態資訊
127.0.0.1:26379> SENTINEL master myredis222	#列出某個master狀態資訊(myredis222為例)
127.0.0.1:26379> SENTINEL slaves myredis222 #列出某個master的所有slave和狀態資訊(myredis222為例)
127.0.0.1:26379> SENTINEL sentinels myredis222	#列出某個master的sentinels((myredis222為例)
127.0.0.1:26379> SENTINEL get-master-addr-by-name myredis222 #通過某個master獲取其IP及port(myredis222為例)
127.0.0.1:26379> SENTINEL ckquorum myredis222	#檢查某個master是達到投票及故障轉移條件(myredis222為例)
127.0.0.1:26379> SENTINEL flushconfig			#強制重寫 SENTINEL 配置到磁碟檔案中
127.0.0.1:26379> SENTINEL failover myredis222	#這個操作就注意了!強制進行故障轉移!慎操作!!!!!!!!!

# 現在測試超時故障轉移,因配置檔案設定 down-after-milliseconds 為10秒無法連線則認為伺服器斷開。
# 這裡設定連線 15 秒,看是否主從複製角色能否進行故障轉移。

[[email protected] ~]# redis-cli -p 6379 -a 654321 DEBUG sleep 15
OK
[[email protected] ~]# 

執行結束後,檢視各複製狀態。

# 222 角色由 master 變為了 slave

127.0.0.1:6379> auth 654321
OK
127.0.0.1:6379> info Replication
# Replication
role:slave
master_host:192.168.1.224
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:2422339
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1357484
repl_backlog_histlen:1048576

# 224 角色由 slave 變為了 master
[[email protected] ~]# redis-cli
127.0.0.1:6379> auth 654321
OK
127.0.0.1:6379> info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.222,port=6379,state=online,offset=2396492,lag=1
master_repl_offset:2396637
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2384854
repl_backlog_histlen:11784

自動切換了!!!get 和 set 同步正常,224 變成了只讀例項。切換成功!!!!

# 現在試試強制故障轉移。即把master轉移到 myredis222。

127.0.0.1:26379> SENTINEL failover myredis222

主從切換同樣成功!系統正常!測試到此結束!原理相關的網上搜索吧。