02.Redis主從集群的Sentinel配置


 

1.集群環境

1.Linux服務器列表
使用4台CentOS Linux服務器搭建環境,其IP地址如下:
  1. 192.168.110.100
  2. 192.168.110.101
  3. 192.168.110.102
  4. 192.168.110.103
2.Redis服務部署環境
192.168.110.100
啟動多個Redis sentinel服務,構成Redis sentinel集群
192.168.110.101
啟動Redis服務,設置成主節點
192.168.110.102
啟動Redis服務,設置成192.168.110.101的從節點
192.168.110.103
啟動Redis服務,設置成192.168.110.101的從節點

2.配置並啟動Redis主從集群

1.修改redis.conf配置文件
主節點的redis配置文件使用默認的配置文件就可以了, 從節點的redis配置文件修改如下:
  1. # Master-Slave replication. Use slaveof to make a Redis instance a copy of
  2. # another Redis server. A few things to understand ASAP about Redis replication.
  3. #
  4. # 1) Redis replication is asynchronous, but you can configure a master to
  5. # stop accepting writes if it appears to be not connected with at least
  6. # a given number of slaves.
  7. # 2) Redis slaves are able to perform a partial resynchronization with the
  8. # master if the replication link is lost for a relatively small amount of
  9. # time. You may want to configure the replication backlog size (see the next
  10. # sections of this file) with a sensible value depending on your needs.
  11. # 3) Replication is automatic and does not need user intervention. After a
  12. # network partition slaves automatically try to reconnect to masters
  13. # and resynchronize with them.
  14. #
  15. # 主從同步。通過 slaveof 配置來實現Redis實例的備份。
  16. # 注意,這里是本地從遠端復制數據。也就是說,本地可以有不同的數據庫文件、綁定不同的IP、監聽不同的端口。
  17. #
  18. # slaveof <masterip> <masterport>
  19. slaveof 192.168.110.1016379
注意:兩台從節點都要改。
2.啟動Redis主從集群
先啟動192.168.110.101主節點,使用默認配置,腳本:
  1. [wch@localhost bin]$ ./redis-server
再啟動192.168.110.102和 192.168.110.103 從節點,使用剛才的配置,腳本:
  1. ./redis-server redis.conf
3.查看集群
192.168.110.101主節點 Replication 信息
  1. [wch@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication
  2. # Replication
  3. role:master
  4. connected_slaves:2
  5. slave0:ip=192.168.110.102,port=6379,state=online,offset=659,lag=1
  6. slave1:ip=192.168.110.103,port=6379,state=online,offset=659,lag=0
  7. master_repl_offset:659
  8. repl_backlog_active:1
  9. repl_backlog_size:1048576
  10. repl_backlog_first_byte_offset:2
  11. repl_backlog_histlen:658
192.168.110.102 從節點 Replication 信息
  1. [wch@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication
  2. # Replication
  3. role:slave
  4. master_host:192.168.110.101
  5. master_port:6379
  6. master_link_status:up
  7. master_last_io_seconds_ago:3
  8. master_sync_in_progress:0
  9. slave_repl_offset:701
  10. slave_priority:100
  11. slave_read_only:1
  12. connected_slaves:0
  13. master_repl_offset:0
  14. repl_backlog_active:0
  15. repl_backlog_size:1048576
  16. repl_backlog_first_byte_offset:0
  17. repl_backlog_histlen:0
192.168.110.103 從節點 Replication 信息
  1. [wch@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication
  2. # Replication
  3. role:slave
  4. master_host:192.168.110.101
  5. master_port:6379
  6. master_link_status:up
  7. master_last_io_seconds_ago:9
  8. master_sync_in_progress:0
  9. slave_repl_offset:715
  10. slave_priority:100
  11. slave_read_only:1
  12. connected_slaves:0
  13. master_repl_offset:0
  14. repl_backlog_active:0
  15. repl_backlog_size:1048576
  16. repl_backlog_first_byte_offset:0
  17. repl_backlog_histlen:0
此時,存儲到 192.168.110.101主節點的數據,在從節點中都可以查詢到。從節點會備份主節點的數據。

3.配置sentinel集群並啟動

1.創建sentinel.conf配置文件
  1. port 26379
  2. # sentinel announce-ip <ip>
  3. # sentinel announce-port <port>
  4. dir /tmp
  5. ################################# master001 #################################
  6. sentinel monitor master001 192.168.110.10163792
  7. # sentinel auth-pass <master-name> <password>
  8. sentinel down-after-milliseconds master001 30000
  9. sentinel parallel-syncs master001 1
  10. sentinel failover-timeout master001 180000
  11. # sentinel notification-script <master-name> <script-path>
  12. # sentinel client-reconfig-script <master-name> <script-path>
  13. # 可以配置多個master節點
  14. ################################# master002 #################################
配置文件說明:
1. port :當前 Sentinel服務運行的端口
 
2. dir :  Sentinel服務運行時使用的臨時文件夾
 
3.sentinel monitor master001 192.168.110.10163792 : Sentinel去監視一個名為 master001 的主redis實例,這個主實例的IP地址為本機地址 192.168 . 110.101 ,端口號為 6379 ,而將這個主實例判斷為失效至少需要 2 個 Sentinel進程的同意,只要同意Sentinel的數量不達標,自動failover就不會執行
 
4.sentinel down-after-milliseconds master001 30000 : 指定了Sentinel認為Redis實例已經失效所需的毫秒數。 當實例超過該時間沒有返回PING,或者直接返回錯誤,那么Sentinel將這個實例標記為主觀下線。只有一個 Sentinel進程將實例標記為主觀下線並不一定會引起實例的自動故障遷移:只有在足夠數量的Sentinel都將一個實例標記為主觀下線之后,實例才會被標記為客觀下線,這時自動故障遷移才會執行
 
5.sentinel parallel-syncs master001 1 指定了在執行故障轉移時,最多可以有多少個從Redis實例在同步新的主實例,在從Redis實例較多的情況下這個數字越小,同步的時間越長,完成故障轉移所需的時間就越長
 
6.sentinel failover-timeout master001 180000 如果在該時間(ms)內未能完成failover操作,則認為該failover失敗
 
7.sentinel notification-script <master-name> <script-path> 指定sentinel檢測到該監控的redis實例指向的實例異常時,調用的報警腳本。該配置項可選,但是很常用
2.啟動 sentinel集群
創建3個sentinel.conf配置文件: sentinel001.conf、 sentinel002.conf、 sentinel003.conf並修改端口號分別為: 26379 36379 46379 ,並啟動服務:
  1. ./redis-sentinel sentinel001.conf
  2. ./redis-sentinel sentinel002.conf
  3. ./redis-sentinel sentinel003.conf
啟動三個 sentinel 服務后會在其控制台看到如下信息:
./ redis - sentinel sentinel001 . conf ,端口: 26379
  1. [7743]01Oct06:20:38.162# Sentinel runid is ba6c42e1accc31290e11d5876275e1562564295d
  2. [7743]01Oct06:20:38.162# +monitor master master001 192.168.110.101 6379 quorum 2
  3. [7743]01Oct06:20:39.110*+slave slave 192.168.110.102:6379192.168.110.1026379@ master001 192.168.110.1016379
  4. [7743]01Oct06:20:39.111*+slave slave 192.168.110.103:6379192.168.110.1036379@ master001 192.168.110.1016379
  5. [7743]01Oct06:25:07.595*+sentinel sentinel 192.168.110.100:36379192.168.110.10036379@ master001 192.168.110.1016379
  6. [7743]01Oct06:26:11.170*+sentinel sentinel 192.168.110.100:46379192.168.110.10046379@ master001 192.168.110.1016379
./ redis - sentinel sentinel002 . conf ,端口: 36379
  1. [7795]01Oct06:25:05.538# Sentinel runid is 52c14768b15837fb601b26328acf150c6bd30682
  2. [7795]01Oct06:25:05.538# +monitor master master001 192.168.110.101 6379 quorum 2
  3. [7795]01Oct06:25:06.505*+slave slave 192.168.110.102:6379192.168.110.1026379@ master001 192.168.110.1016379
  4. [7795]01Oct06:25:06.515*+slave slave 192.168.110.103:6379192.168.110.1036379@ master001 192.168.110.1016379
  5. [7795]01Oct06:25:07.557*+sentinel sentinel 192.168.110.100:26379192.168.110.10026379@ master001 192.168.110.1016379
  6. [7795]01Oct06:26:11.168*+sentinel sentinel 192.168.110.100:46379192.168.110.10046379@ master001 192.168.110.1016379
./ redis - sentinel sentinel003 . conf ,端口: 46379
  1. [7828]01Oct06:26:09.076# Sentinel runid is c8509594be4a36660b2122b3b81f4f74060c9b04
  2. [7828]01Oct06:26:09.076# +monitor master master001 192.168.110.101 6379 quorum 2
  3. [7828]01Oct06:26:10.063*+slave slave 192.168.110.102:6379192.168.110.1026379@ master001 192.168.110.1016379
  4. [7828]01Oct06:26:10.071*+slave slave 192.168.110.103:6379192.168.110.1036379@ master001 192.168.110.1016379
  5. [7828]01Oct06:26:11.516*+sentinel sentinel 192.168.110.100:26379192.168.110.10026379@ master001 192.168.110.1016379
  6. [7828]01Oct06:26:11.674*+sentinel sentinel 192.168.110.100:36379192.168.110.10036379@ master001 192.168.110.1016379
每個sentinel服務能知道其他所有的服務!

4.測試sentinel集群

1.停止192.168.110.101主節點
停止192.168.110.101Redis主節點后,在查看 Replication 信息如下:
  1. [wch@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication
  2. Could not connect to Redis at 192.168.110.101:6379:Connection refused
  3. [wch@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication
  4. # Replication
  5. role:slave
  6. master_host:192.168.110.103
  7. master_port:6379
  8. master_link_status:up
  9. master_last_io_seconds_ago:1
  10. master_sync_in_progress:0
  11. slave_repl_offset:29128
  12. slave_priority:100
  13. slave_read_only:1
  14. connected_slaves:0
  15. master_repl_offset:0
  16. repl_backlog_active:0
  17. repl_backlog_size:1048576
  18. repl_backlog_first_byte_offset:0
  19. repl_backlog_histlen:0
  20. [wch@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication
  21. # Replication
  22. role:master
  23. connected_slaves:1
  24. slave0:ip=192.168.110.102,port=6379,state=online,offset=30456,lag=1
  25. master_repl_offset:30456
  26. repl_backlog_active:1
  27. repl_backlog_size:1048576
  28. repl_backlog_first_byte_offset:2
  29. repl_backlog_histlen:30455
  30. [wch@localhost bin]$
發現 192.168.110.101Redis主節點已經不能連接, 192.168 . 110.103 成了主節點!
2.再啟動 192.168.110.101主節點
再啟動192.168.110.101Redis主節點后,在查看 Replication 信息如下:
  1. ### 啟動腳本,仍然使用默認配置
  2. [wch@localhost bin]$ ./redis-server
  3. [wch@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication
  4. # Replication
  5. role:slave
  6. master_host:192.168.110.103
  7. master_port:6379
  8. master_link_status:up
  9. master_last_io_seconds_ago:1
  10. master_sync_in_progress:0
  11. slave_repl_offset:57657
  12. slave_priority:100
  13. slave_read_only:1
  14. connected_slaves:0
  15. master_repl_offset:0
  16. repl_backlog_active:0
  17. repl_backlog_size:1048576
  18. repl_backlog_first_byte_offset:0
  19. repl_backlog_histlen:0
  20.  
  21. [wch@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication
  22. # Replication
  23. role:slave
  24. master_host:192.168.110.103
  25. master_port:6379
  26. master_link_status:up
  27. master_last_io_seconds_ago:0
  28. master_sync_in_progress:0
  29. slave_repl_offset:60751
  30. slave_priority:100
  31. slave_read_only:1
  32. connected_slaves:0
  33. master_repl_offset:0
  34. repl_backlog_active:0
  35. repl_backlog_size:1048576
  36. repl_backlog_first_byte_offset:0
  37. repl_backlog_histlen:0
  38.  
  39. [wch@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication
  40. # Replication
  41. role:master
  42. connected_slaves:2
  43. slave0:ip=192.168.110.102,port=6379,state=online,offset=63247,lag=1
  44. slave1:ip=192.168.110.101,port=6379,state=online,offset=63247,lag=1
  45. master_repl_offset:63393
  46. repl_backlog_active:1
  47. repl_backlog_size:1048576
  48. repl_backlog_first_byte_offset:2
  49. repl_backlog_histlen:63392
  50. [wch@localhost bin]$
發現 192.168 . 110.101 節點啟動后還再集群中,只不過成了從節點, 192.168 . 110.103 仍然是主節點,但是現在又有兩個從節點了!
3.只留下一個sentinel服務,再停止192.168.110.103主節點,查看Redis集群是否出現新的主節點
停止sentinel服務, 只留下一個sentinel服務,再停止Redis主節點, 查看 Replication 信息如下:
  1. [wch@localhost bin]$ ./redis-cli -h 192.168.110.101 info Replication
  2. # Replication
  3. role:slave
  4. master_host:192.168.110.103
  5. master_port:6379
  6. master_link_status:down
  7. master_last_io_seconds_ago:-1
  8. master_sync_in_progress:0
  9. slave_repl_offset:184231
  10. master_link_down_since_seconds:43
  11. slave_priority:100
  12. slave_read_only:1
  13. connected_slaves:0
  14. master_repl_offset:0
  15. repl_backlog_active:0
  16. repl_backlog_size:1048576
  17. repl_backlog_first_byte_offset:0
  18. repl_backlog_histlen:0
  19. [wch@localhost bin]$ ./redis-cli -h 192.168.110.102 info Replication
  20. # Replication
  21. role:slave
  22. master_host:192.168.110.103
  23. master_port:6379
  24. master_link_status:down
  25. master_last_io_seconds_ago:-1
  26. master_sync_in_progress:0
  27. slave_repl_offset:184231
  28. master_link_down_since_seconds:52
  29. slave_priority:100
  30. slave_read_only:1
  31. connected_slaves:0
  32. master_repl_offset:0
  33. repl_backlog_active:0
  34. repl_backlog_size:1048576
  35. repl_backlog_first_byte_offset:0
  36. repl_backlog_histlen:0
  37. [wch@localhost bin]$ ./redis-cli -h 192.168.110.103 info Replication
  38. Could not connect to Redis at 192.168.110.103:6379:Connection refused
發現 192.168 . 110.103 主節點已經不能連接了,也 不存在Redis主節點,集群中無主節點了!!!分析原因是:sentinel.conf 配置的 sentinel monitor master001 192.168 . 110.101 6379 2 最后一個參數是2導致,若是但節點此配置的最后一個參數要使用是1。(此原因我已證實)
注意: 在生產環境下建議 sentinel節點的數量能在3個以上,並且最好不要在同一台機器上(使用同一網卡)。
-------------------------------------------------------------------------------------------------------------------------------






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM