共計 12656 個字符,預計需要花費 32 分鐘才能閱讀完成。
丸趣 TV 小編給大家分享一下 Redis Sentinel 安裝配置的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
Redis Sentinel 概述
我們知道 Redis 類似 MySQL 數據庫自帶主從復制結構,產品環境中,如果一旦 master 發生 crash, 我們希望 slave 可以立即自動提升為主,接替業務提供服務,如何實現這個功能呢?redis sentinel 集群可以幫助我們實現這個功能;
Redis Sentinel 是 Redis 官方原生高可用解決方案,Redis Sentinel 部署架構主要包括兩部分:Redis Sentinel 集群和 Redis master-slave 集群,其中 Redis Sentinel 集群是由若干 Sentinel 節點組成的分布式集群;
可以實現故障發現、故障轉移、配置中心和客戶端通知。Redis Sentinel 的節點數量要滿足 2n+1(n =1)的奇數個 (官方建議至少 3 個)。
Redis Sentinel 特點
(1)master 與 slave 之間的 failover 是通過 sentinel 來監控,如果共有 5 個 sentinel,配置參數中設置只要有 2 個 sentinel 認為 master crash 了,就會進行 failover, 但是進行 failover 的那個 sentinel 必須先獲得至少 3 個 sentinel 的授權才能實行 failover;
(2)sentinel 集群不會同一時間多個 sentinel 并發執行 failover, 如果第一個進行 failover 的 sentinel 失敗了,另外一個將會在一定時間內重新進行 failover, 以此類推;
(3) 當 failover 后,sentinel 會獲得 master 的最新的一個配置版本號,然后在廣播給其他 sentinel, 所以一個能夠互相通信的 sentinel 集群最終會采用版本號最高且相同的配置;
(4)Redis Sentinel version1 開始于 Redis2.6, Redis Sentinel version 2 開始于 Redis 2.8, 建議使用 Sentinel 2;
(5)Redis-Sentinel 是 Redis 官方推薦的高可用性 (HA) 解決方案,Redis-sentinel 本身也是一個獨立運行的進程,它能監控多個 master-slave 集群,發現 master 宕機后能進行自動切換。Sentinel 可以監視任意多個主服務器(復用),以及主服務器屬下的從服務器,并在被監視的主服務器下線時,自動執行故障轉移操作。
SDOWN 和 ODOWN
SDOWN(主觀宕機) 是 sentinel 自己主觀檢測到 master 的狀態是 down;
ODOWN(客觀宕機) 需要大多數的 sentinel 都認為 master 宕機;
從 SDOWN 切換到 ODOWN 不需要任何一致性算法,只需要一個 gossip 協議,如果一個 sentinel 收到了足夠多的 sentinel 發來消息告訴它某個 master 已經 down 掉了,SDOWN 狀態就會變成 ODOWN 狀態。如果之后 master 可用了,這個狀態就會相應地被清理掉。
Sentinel.conf 相關參數
port 26379
#sentinel 的端口號
sentinel monitor mymaster 127.0.0.1 6379 2
#sentinel 監控的 master 名稱默認是 mymaster,最后數字 2 表示如果有兩個 sentinel 認為 master 掛了,則這個 master 即認為不可用;
注意:我們可以通過配置不同的 master 名稱,讓一套 Sentinel Cluster 監控多個 Redis master-slave 集群;
sentinel down-after-milliseconds mymaster 30000
# 默認 30 秒,sentinel 會通過 ping 來判斷 master 是否存活,如果在 30 秒內 master 返回 pong 給 sentinel, 則認為 master 是好的, 否則 sentinel 認為 master 不可用;
sentinel parallel-syncs mymaster 1
# 當 Sentinel 節點集合對主節點故障判定達成一致時,Sentinel 領導者節點會做故障轉移操作,選出新的主節點,原來的從節點會向新的主節點發起復制操作,限制每次向新的主節點發起復制操作的從節點個數為 1
sentinel failover-timeout mymaster 180000
# 故障轉移超時時間為 3min
Redis Sentinel 中的身份驗證
當一個 master 配置為需要密碼才能連接時,客戶端和 slave 在連接時都需要提供密碼;
master 通過 requirepass 設置自身的密碼,不提供密碼無法連接到這個 master;
slave 通過 masterauth 來設置訪問 master 時的密碼;
但是當使用了 sentinel 時,由于一個 master 可能會變成一個 slave,一個 slave 也可能會變成 master,所以需要同時設置上述兩個配置項。
安裝 Redis Sentinel
(1)Redis sentinel 架構圖和節點環境
RoelHostIPPortSentinel1sht-sgmhadoopnn-01172.16.101.5426379Sentinel2sht-sgmhadoopnn-01172.16.101.5526379Sentinel3sht-sgmhadoopnn-02172.16.101.5626379Mastersht-sgmhadoopdn-01172.16.101.586379Slave1sht-sgmhadoopdn-02172.16.101.596379Slave2sht-sgmhadoopdn-03172.16.101.606379
(2) 配置 Redis 主從復制
[root@sht-sgmhadoopdn-01 redis]# vim redis.conf
bind 172.16.101.58
[root@sht-sgmhadoopdn-01 redis]# src/redis-server redis.conf
[root@sht-sgmhadoopdn-02 redis]# vim redis.conf
bind 172.16.101.59
slaveof 172.16.101.58 6379
[root@sht-sgmhadoopdn-02 redis]# src/redis-server redis.conf
[root@sht-sgmhadoopdn-03 redis]# vim redis.conf
bind 172.16.101.60
slaveof 172.16.101.58 6379
[root@sht-sgmhadoopdn-03 redis]# src/redis-server redis.conf
檢查主從復制的設置
[root@sht-sgmhadoopdn-01 redis]# src/redis-cli -h 172.16.101.58
172.16.101.58:6379 client list
id=3 addr=172.16.101.59:35718 fd=7 name= age=26 idle=0 flags=S db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=replconf
id=4 addr=172.16.101.60:33986 fd=8 name= age=22 idle=0 flags=S db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=replconf
id=5 addr=172.16.101.58:38875 fd=9 name= age=4 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
172.16.101.58:6379 info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.16.101.59,port=6379,state=online,offset=57,lag=0
slave1:ip=172.16.101.60,port=6379,state=online,offset=57,lag=0
master_repl_offset:57
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:56
(3) 配置 sentinel 集群
三個 sentinel 節點的 sentinel.conf 文件配置一樣,如果是在同一個主機上,則需要使用不同的端口號
[root@sht-sgmhadoopcm-01 redis]# vim sentinel.conf
port 26379
daemonize yes
protected-mode no
logfile sentinel.log
dir /usr/local/redis
sentinel monitor mymaster 172.16.101.58 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel 節點有兩種啟動方法:
src/redis-sentinel sentinel.conf
src/redis-server sentinel.conf --sentinel
[root@sht-sgmhadoopcm-01 redis]# src/redis-sentinel sentinel.conf
[root@sht-sgmhadoopnn-01 redis]# src/redis-sentinel sentinel.conf
[root@sht-sgmhadoopnn-02 redis]# src/redis-sentinel sentinel.conf
[root@sht-sgmhadoopcm-01 redis]# ps -ef|grep redis|grep -v grep
root 7541 1 0 22:33 ? 00:00:00 src/redis-sentinel *:26379 [sentinel]
(4) 檢查整個集群的狀態
[root@sht-sgmhadoopcm-01 redis]# src/redis-cli -h 172.16.101.54 -p 26379
172.16.101.54:26379 client list
id=3 addr=172.16.101.55:43182 fd=13 name=sentinel-ab45fe6c-cmd age=138 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ping
id=4 addr=172.16.101.56:60016 fd=15 name=sentinel-e32f20c0-cmd age=136 idle=1 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ping
id=5 addr=172.16.101.54:35342 fd=17 name= age=26 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
172.16.101.54: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=172.16.101.58:6379,slaves=2,sentinels=3
[root@sht-sgmhadoopdn-01 redis]# src/redis-cli -h 172.16.101.58 -p 6379
172.16.101.58:6379 client list
id=16 addr=172.16.101.54:56510 fd=10 name=sentinel-30393e76-pubsub age=326 idle=0 flags=N db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
id=17 addr=172.16.101.54:56508 fd=11 name=sentinel-30393e76-cmd age=326 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ping
id=18 addr=172.16.101.55:57444 fd=12 name=sentinel-ab45fe6c-cmd age=177 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=publish
id=19 addr=172.16.101.55:57446 fd=13 name=sentinel-ab45fe6c-pubsub age=177 idle=0 flags=N db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
id=3 addr=172.16.101.59:35718 fd=7 name= age=3936 idle=0 flags=S db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=replconf
id=4 addr=172.16.101.60:33986 fd=8 name= age=3932 idle=0 flags=S db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=replconf
id=20 addr=172.16.101.56:55648 fd=14 name=sentinel-e32f20c0-cmd age=173 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=ping
id=21 addr=172.16.101.56:55650 fd=15 name=sentinel-e32f20c0-pubsub age=173 idle=0 flags=N db=0 sub=1 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=subscribe
id=5 addr=172.16.101.58:38875 fd=9 name= age=3914 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
當我們啟動主從節點和 sentinel 節點后,sentinel.conf 配置文件會自動添加或修改參數
[root@sht-sgmhadoopcm-01 redis]# cat sentinel.conf
sentinel myid 30393e76e002cb64db92fb8bcb88d79f2d85a82b
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
# Generated by CONFIG REWRITE
sentinel known-slave mymaster 172.16.101.60 6379
sentinel known-slave mymaster 172.16.101.59 6379
sentinel known-sentinel mymaster 172.16.101.55 26379 ab45fe6c0f010473ce3b7b4d2120e1a83776b736
sentinel known-sentinel mymaster 172.16.101.56 26379 e32f20c0f315e712c9921371f15729246f3816a0
sentinel current-epoch 0
[root@sht-sgmhadoopnn-01 redis]# cat sentinel.conf
sentinel myid ab45fe6c0f010473ce3b7b4d2120e1a83776b736
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
# Generated by CONFIG REWRITE
sentinel known-slave mymaster 172.16.101.60 6379
sentinel known-slave mymaster 172.16.101.59 6379
sentinel known-sentinel mymaster 172.16.101.56 26379 e32f20c0f315e712c9921371f15729246f3816a0
sentinel known-sentinel mymaster 172.16.101.54 26379 30393e76e002cb64db92fb8bcb88d79f2d85a82b
sentinel current-epoch 0
[root@sht-sgmhadoopnn-02 redis]# cat sentinel.conf
sentinel myid e32f20c0f315e712c9921371f15729246f3816a0
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
# Generated by CONFIG REWRITE
sentinel known-slave mymaster 172.16.101.60 6379
sentinel known-slave mymaster 172.16.101.59 6379
sentinel known-sentinel mymaster 172.16.101.54 26379 30393e76e002cb64db92fb8bcb88d79f2d85a82b
sentinel known-sentinel mymaster 172.16.101.55 26379 ab45fe6c0f010473ce3b7b4d2120e1a83776b736
sentinel current-epoch 0
測試自動 failover
[root@sht-sgmhadoopdn-01 redis]# ps -ef|grep redis
root 15128 1 0 21:17 ? 00:00:05 src/redis-server 172.16.101.58:6379
[root@sht-sgmhadoopdn-01 redis]# kill -9 15128
[root@sht-sgmhadoopcm-01 redis]# tail -f sentinel.log
7541:X 05 Aug 22:55:48.052 # +sdown master mymaster 172.16.101.58 6379 #sentinel 主觀認為 master crash;
7541:X 05 Aug 22:55:48.143 # +odown master mymaster 172.16.101.58 6379 #quorum 2/2 # 只要有兩個 sentinel 節點認為 master crash, 則客觀認為 master crash
7541:X 05 Aug 22:55:48.143 # +new-epoch 1
7541:X 05 Aug 22:55:48.143 # +try-failover master mymaster 172.16.101.58 6379
7541:X 05 Aug 22:55:48.165 # +vote-for-leader 30393e76e002cb64db92fb8bcb88d79f2d85a82b 1
7541:X 05 Aug 22:55:48.166 # ab45fe6c0f010473ce3b7b4d2120e1a83776b736 voted for ab45fe6c0f010473ce3b7b4d2120e1a83776b736 1
7541:X 05 Aug 22:55:48.173 # e32f20c0f315e712c9921371f15729246f3816a0 voted for ab45fe6c0f010473ce3b7b4d2120e1a83776b736 1
7541:X 05 Aug 22:55:48.544 # +config-update-from sentinel ab45fe6c0f010473ce3b7b4d2120e1a83776b736 172.16.101.55 26379 @ mymaster 172.16.101.58 6379
7541:X 05 Aug 22:55:48.544 # +switch-master mymaster 172.16.101.58 6379 172.16.101.60 6379
7541:X 05 Aug 22:55:48.545 * +slave slave 172.16.101.59:6379 172.16.101.59 6379 @ mymaster 172.16.101.60 6379
7541:X 05 Aug 22:55:48.545 * +slave slave 172.16.101.58:6379 172.16.101.58 6379 @ mymaster 172.16.101.60 6379 # 從這一步到下一步執行 failover 成功之間需要等待 30s,這是由于參數 sentinel down-after-milliseconds mymaster 控制,master 30s 之內沒有響應 sentinel 才會真正的 failover;
7541:X 05 Aug 22:56:18.562 # +sdown slave 172.16.101.58:6379 172.16.101.58 6379 @ mymaster 172.16.101.60 6379
master 和 slave 發生了變化,IP60 成為新的 master,IP58 成為 slave
[root@sht-sgmhadoopcm-01 redis]# src/redis-cli -h 172.16.101.54 -p 26379
172.16.101.54:26379 sentinel masters
1) 1) name
2) mymaster
3) ip
4) 172.16.101.60
......
172.16.101.54:26379 sentinel slaves mymaster
1) 1) name
2) 172.16.101.58:6379
3) ip
4) 172.16.101.58
9) flags
10) s_down,slave,disconnected
2) 1) name
2) 172.16.101.59:6379
3) ip
4) 172.16.101.59
9) flags
10) slave
重啟修復好的舊 master 之后,會自動成為新 master 的從庫
[root@sht-sgmhadoopdn-01 redis]# src/redis-server redis.conf
[root@sht-sgmhadoopcm-01 redis]# tail -f sentinel.log
7541:X 05 Aug 23:11:10.556 # -sdown slave 172.16.101.58:6379 172.16.101.58 6379 @ mymaster 172.16.101.60 6379
7541:X 05 Aug 23:11:20.518 * +convert-to-slave slave 172.16.101.58:6379 172.16.101.58 6379 @ mymaster 172.16.101.60 6379
Failover 過程分析:
Each Sentinel detects the master is down with an +sdown event.
This event is later escalated to +odown, which means that multiple Sentinels agree about the fact the master is not reachable.
Sentinels vote a Sentinel that will start the first failover attempt.
The failover happens.
sentinel 節點會定期通過 ping 檢測 redis 的 master 是否存活,一旦 master crash,
首先 sentinel 自己會主觀認為 master crash, 然后三個 sentinel 之間彼此通信,只要有兩個 sentinel 節點認為 master crash, 則客觀認為 master crash,
接著三個 sentinel 節點會投票,得到兩票的一個 sentinel 會去執行 failover,
最后 master 30s 之內沒有響應 sentinel 才會真正的 failover;
一旦掛掉的舊 master 修復,重新啟動后,會作為新 master 的從庫存在;
FAQ
Error1:
[root@sht-sgmhadoopcm-01 redis]# src/redis-cli -h 172.16.101.54 -p 26379
172.16.101.54:26379 ping
(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions:
1) Just disable protected mode sending the command CONFIG SET protected-mode no from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent.
2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to no , and then restarting the server.
3) If you started the server manually just for testing, restart it with the --protected-mode no option.
4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
解決方法:[root@sht-sgmhadoopcm-01 redis]# vim sentinel.conf
protected-mode no
看完了這篇文章,相信你對“Redis Sentinel 安裝配置的示例分析”有了一定的了解,如果想了解更多相關知識,歡迎關注丸趣 TV 行業資訊頻道,感謝各位的閱讀!