Redis 高可用:Redis Sentinel 主從複製容錯移轉

來源:互聯網
上載者:User

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


剛好有合適的兩台伺服器 Centos 6.4 ,已安裝 Redis 3.2 和 Redis主從複製。

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

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


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



# 222 配置 redis 啟動參數

[root@centos222 ~]# vi /etc/redis/6379.confbind 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 啟動參數

[root@centos224 ~]# vi /etc/redis/6379.confbind 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 配置

[root@centos222 ~]# vi /etc/redis/sentinel.conf#ip 192.168.1.222port 26379bind 0.0.0.0daemonize yeslogfile "/var/log/sentinel_log.log"#myredis222sentinel monitor myredis222 192.168.1.222 6379 1sentinel down-after-milliseconds myredis222 10000sentinel failover-timeout myredis222 900000sentinel parallel-syncs myredis222 1sentinel auth-pass myredis222 654321#myredis224sentinel monitor myredis224 192.168.1.224 6379 1sentinel down-after-milliseconds myredis224 10000sentinel failover-timeout myredis224 900000sentinel parallel-syncs myredis224 1sentinel auth-pass myredis224 654321

# 224 redis sentinel 配置

[root@centos224 ~]#  vi /etc/redis/sentinel.conf#ip 192.168.1.224port 26379bind 0.0.0.0daemonize yeslogfile "/var/log/sentinel_log.log"#myredis222sentinel monitor myredis222 192.168.1.222 6379 1sentinel down-after-milliseconds myredis222 10000sentinel failover-timeout myredis222 900000sentinel parallel-syncs myredis222 1sentinel auth-pass myredis222 654321#myredis224sentinel monitor myredis224 192.168.1.224 6379 1sentinel down-after-milliseconds myredis224 10000sentinel failover-timeout myredis224 900000sentinel parallel-syncs myredis224 1sentinel auth-pass myredis224 654321

# 伺服器 222 和 224 防火牆添加入站規則

iptables -I INPUT -p tcp --dport 26379 -j ACCEPT

# 啟動 sentinel 服務兩種方法:

redis-sentinel /etc/redis/sentinel.confredis-server /etc/redis/sentinel.conf --sentinel

# 222 啟動 sentinel 服務

[root@centos222 ~]# redis-sentinel /etc/redis/sentinel.conf[root@centos222 ~]# cat /var/log/sentinel_log.log24630:X 26 Nov 18:00:50.496 # Sentinel ID is b0c01ccf191b07bc7f8f42c395b1f45ec384925824630:X 26 Nov 18:00:50.496 # +monitor master myredis224 192.168.1.224 6379 quorum 124630:X 26 Nov 18:00:50.496 # +monitor master myredis222 192.168.1.222 6379 quorum 124630: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 服務

[root@centos224 ~]# redis-sentinel /etc/redis/sentinel.conf[root@centos224 ~]# cat /var/log/sentinel_log.log29098:X 26 Nov 07:00:33.795 # Sentinel ID is 2303678ad7c6df83ce9c938ed9341fc33170f84329098:X 26 Nov 07:00:33.795 # +monitor master myredis222 192.168.1.222 6379 quorum 129098:X 26 Nov 07:00:33.795 # +monitor master myredis224 192.168.1.224 6379 quorum 129098:X 26 Nov 07:00:33.797 * +slave slave 192.168.1.224:6379 192.168.1.224 6379 @ myredis222 192.168.1.222 637929098:X 26 Nov 07:00:34.560 * +sentinel sentinel b0c01ccf191b07bc7f8f42c395b1f45ec3849258 192.168.1.222 26379 @ myredis222 192.168.1.222 637929098:X 26 Nov 07:00:34.706 * +sentinel sentinel b0c01ccf191b07bc7f8f42c395b1f45ec3849258 192.168.1.222 26379 @ myredis224 192.168.1.224 6379

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

# 串連 Sentinel ,查看資訊(以224用戶端串連為例)

[root@centos224 ~]# redis-cli -p 26379127.0.0.1:26379> info Sentinel# Sentinelsentinel_masters:2sentinel_tilt:0sentinel_running_scripts:0sentinel_scripts_queue_length:0sentinel_simulate_failure_flags:0master0:name=myredis222,status=ok,address=192.168.1.222:6379,slaves=1,sentinels=2master1:name=myredis224,status=odown,address=192.168.1.224:6379,slaves=0,sentinels=2127.0.0.1:26379> 

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


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

[root@centos222 ~]# redis-cli127.0.0.1:6379> auth 654321OK127.0.0.1:6379> info Replication# Replicationrole:masterconnected_slaves:1slave0:ip=192.168.1.224,port=6379,state=online,offset=2088480,lag=1master_repl_offset:2088623repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1040048repl_backlog_histlen:1048576127.0.0.1:6379> 

# 在224 伺服器串連redis:redis 角色為 slave

[root@centos224 ~]# redis-cli127.0.0.1:6379> auth 654321OK127.0.0.1:6379> info Replication# Replicationrole:slavemaster_host:192.168.1.222master_port:6379master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:2087322slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_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 秒,看是否主從複製角色能否進行容錯移轉。

[root@centos222 ~]# redis-cli -p 6379 -a 654321 DEBUG sleep 15OK[root@centos222 ~]# 

執行結束後,查看各複製狀態。

# 222 角色由 master 變為了 slave

127.0.0.1:6379> auth 654321OK127.0.0.1:6379> info Replication# Replicationrole:slavemaster_host:192.168.1.224master_port:6379master_link_status:upmaster_last_io_seconds_ago:0master_sync_in_progress:0slave_repl_offset:2422339slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:1357484repl_backlog_histlen:1048576

# 224 角色由 slave 變為了 master

[root@centos224 ~]# redis-cli127.0.0.1:6379> auth 654321OK127.0.0.1:6379> info Replication# Replicationrole:masterconnected_slaves:1slave0:ip=192.168.1.222,port=6379,state=online,offset=2396492,lag=1master_repl_offset:2396637repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:2384854repl_backlog_histlen:11784

自動切換了。。。get 和 set 同步正常,224 變成了唯讀執行個體。切換成功。。。。


# 現在試試強制容錯移轉。即把master轉移到 myredis222。

127.0.0.1:26379> SENTINEL failover myredis222

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



參考:Redis Sentinel Documentation

Redis高可用部署及監控

redis sentinel 主從切換(failover)解決方案,詳細配置




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.