redis部署主從
redis Master&Slave
什么是主從
master主機數(shù)據(jù)自動同步到備份機slave
master主要是寫入數(shù)據(jù)
slave是讀取數(shù)據(jù)
有什么用
1糊探、讀寫分離
2凭戴、快速恢復
如何部署
要求三臺redis應用
master 127.0.0.1:6379 (主)
slave 127.0.0.1:6378 (從)
slave 127.0.0.1:6377 (從)
image.png
檢驗
master里set一個key然后在slave里能獲取到即可
image.png
sentinel 配置及說明
相關配置和說明:
關系圖:
image.png
在redis下有sentinel.conf 配置
bind綁定0.0.0.0
bind 0.0.0.0 當前服務器上所有綁定的iP
bind 127.0.0.1 只能本機訪問
bind (ipconfig 網(wǎng)卡的綁定的ip)
port
默認26379
sentinel monitor mymaster 127.0.0.1 6379 2
mymaster:監(jiān)控的集群名稱
監(jiān)控 127.0.0.1 6379 這個redis
`2` 投票數(shù)
sentinel auth-pass mymaster passwd
設置連接master和slave時的密碼,注意的是sentinel不能分別為master和slave設置不同的密碼屁置,因此master和slave的密碼應該設置相同擎椰。
sentinel down-after-milliseconds mymster 10000
這個配置項指定了需要多少失效時間秉沼,一個master才會被這個sentinel主觀地認為是不可用的州刽。 單位是毫秒呕寝,默認為30秒
因此再測試的時候 停止一臺機器后勋眯,等一會查看是否切換了主從
sentinel parallel-syncs mymaster 1
這個配置項指定了在發(fā)生failover主備切換時最多可以有多少個slave同時對新的master進行同步,這個數(shù)字越小下梢,完成failover所需的時間就越長客蹋,但是如果這個數(shù)字越大,就意味著越多的slave因為replication而不可用孽江⊙扰鳎可以通過將這個值設為1來保證每次只有一個slave處于不能處理命令請求的狀態(tài)。
sentinel failover-timeout mymaster 20000
1.同一個sentinel對同一個master兩次failover之間的間隔時間岗屏。
2.當一個slave從一個錯誤的master那里同步數(shù)據(jù)開始計算時間辆琅。直到slave被糾正為向正確的master那里同步數(shù)據(jù)時。
3.當想要取消一個正在進行的failover所需要的時間这刷。
4.當進行failover時婉烟,配置所有slaves指向新的master所需的最大時間。不過暇屋,即使過了這個超時似袁,slaves依然會被正確配置為指向master,但是就不按parallel-syncs所配置的規(guī)則來了咐刨。
啟動
./src/redis-sentinel sentinel.conf
三個都啟動
./src/redis-cli -p 26397
進入redis終端輸入下面查看
sentinel master mymaster
sentinel slaves mymaster
sentinel sentinels mymaster
sentinel get-master-addr-by-name mymaster
查看當前主機的ip和端口
注意:測試時關閉主機時需要 等一會再查看 是否切換昙衅,主機切換需要時間(自己設置或者默認 sentinel down-after-milliseconds)。
測試
image.png
image.png