Redis 哨兵機制以及災難演練

哨兵都採用這個配置便可

一、修改sentinel.conf配置文件

image

二、禁止保護模式 protected-mode no

  protected-mode參數是爲了禁止外網訪問redis,若是啓用了,則只可以經過lookback ip(127.0.0.1)訪問Redis,若是外網訪問redis,會報出異常

  注意:
若是redis實例配置文件中禁用了bind參數,並將protected-mode設置爲no後,外網訪問redis依然報上述錯誤,由於 sentinel 實例的配置文件中須要增長參數 protected-mode no

image
redis

三、配置監控的服務器配置

image

  解釋:配置監聽的主服務器,這裏sentinel monitor表明監控,mymaster表明服務器的名稱,能夠自定義,172.16.178.2表明監控的主服務器,6379表明端口,2表明只有兩個或兩個以上的哨兵認爲主服務器不可用的時候,纔會進行failover操做

quorum的解釋以下:

 (1)至少多少個哨兵要一致贊成,master進程掛掉了,或者slave進程掛掉了,或者要啓動一個故障轉移操做

 (2)quorum是用來識別故障的,真正執行故障轉移的時候,仍是要在哨兵集羣執行選舉,選舉一個哨兵進程出來執行故障轉移操做

 (3)假設有5個哨兵,quorum設置了2,那麼若是5個哨兵中的2個都認爲master掛掉了; 2個哨兵中的一個就會作一個選舉,選舉一個哨兵出來,執行故障轉移; 若是5個哨兵中有3個哨兵都是運行的,那麼故障轉移就會被容許執行服務器

# 語法:sentinel monitor <master-name> <ip> <redis-port> <quorum>
# Tells Sentinel to monitor this master, and to consider it in O_DOWN
# (Objectively Down) state only if at least <quorum> sentinels agree.
# 告訴Sentinel監視這個master,而且考慮它O_DOWN 
# (僅在客觀上O_DOWN) 狀態 只有在至少 quorum 這個數量的哨兵贊成才能夠。
# Note that whatever is the O_DOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
# 請注意,不管quorum數是多少,哨兵都須要這樣作由大多數已知的哨兵
# 選出來啓動故障轉移,所以不能在少數狀況下執行故障轉移。
# Replicas are auto-discovered, so you don't need to specify replicas in
# any way. Sentinel itself will rewrite this configuration file adding
# the replicas using additional configuration options.
# 從機是自動發現的,所以不須要在任何方式指定。哨兵本身將重寫配置文件添加
# 到從機使用的配置文件的配置選項上。
# Also note that the configuration file is rewritten when a
# replica is promoted to master.
# 注意當從機被指定主機的時候將重寫配置文件
# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".
#  注意:主機名稱不該該包含特殊字符或空格。有效的字符集是A-z 0-9和三個字符「.-_」。
四、其餘的配置

  (1)、down-after-milliseconds,超過多少毫秒跟一個redis實例斷了鏈接,哨兵就可能認爲這個redis實例掛了

  (2)、parallel-syncs,新的master別切換以後,同時有多少個slave被切換到去鏈接新master,從新作同步,數字越低,花費的時間越多
假設你的redis是1個master,4個slave,而後master宕機了,4個slave中有1個切換成了master,剩下3個slave就要掛到新的master上面去這個時候,若是parallel-syncs是1,那麼3個slave,一個一個地掛接到新的master上面去,1個掛接完,並且重新的master sync完數據以後,再掛接下一個,若是parallel-syncs是3,那麼一次性就會把全部slave掛接到新的master上去

  (3)、failover-timeout,執行故障轉移的timeout超時時長
ide

五、設置密碼權限
# Set the password to use to authenticate with the master and replicas.
# Useful if there is a password set in the Redis instances to monitor.
# 設置用於主服務器和從機進行身份驗證的密碼。
# 若是要監視的Redis實例中設置了密碼,則很是有用
# Note that the master password is also used for replicas, so it is not
# possible to set a different password in masters and replicas instances
# if you want to be able to monitor these instances with Sentinel.
# 注意,主密碼也用於從機,所以它不可能在主機和從機實例中設置不一樣的密碼
# 若是你想用哨兵來監控這些實例。
# However you can have Redis instances without the authentication enabled
# mixed with Redis instances requiring the authentication (as long as the
# password set is the same for all the instances requiring the password) as
# the AUTH command will have no effect in Redis instances with authentication
# switched off.
# 並且您能夠在不啓用身份驗證的狀況下使用Redis實例與須要身份驗證的Redis實例混合(只要
# 對於須要密碼的全部實例,密碼集都是相同的在使用身份驗證的Redis實例中),AUTH命令將不起做用。
# 語法:sentinel auth-pass mymaster MySUPER--secret-0123passw0rd

image

  設置成redis.conf中配置的密碼:

image

  上一篇文章已經介紹了主從的搭建這裏就不介紹了,有需求查看上一篇文章
ui

六、直接啓動三個服務器的redis服務

image

七、查看一下redis服務啓動的日誌

image

  啓動成功而且鏈接到了主機
this

八、接下來分別啓動三個哨兵從主機開始啓動

  經過下圖可知:主機是172.16.178.2 從機是 172.16.178.3 、172.16.178.4

image
spa

九、啓動三個哨兵:成功啓動

image

十、查看主機的sentinel.conf 配置可知,該哨兵在監控兩個從機和配置的主機,以及另外兩個哨兵

image

十一、同理另外兩個哨兵也是監控着兩個從機和配置的主機,以及另外兩個哨兵

image

十二、鏈接配置好的哨兵

image

1三、輸入 info 命令

image

  看到哨兵正在監控着一個主機兩個從機以及三個哨兵
3d

1四、故障演練:

 1)、先查詢主機的進程ID

image

 2)、kill -9 70817

image

  此時當前節點只有哨兵了

 3)、鏈接當前哨兵,輸入info

image

  這是最小的哨兵配置,若是發生了master-slave故障轉移,或者新的哨兵進程加入哨兵集羣,那麼哨兵會自動更新本身的配置文件

  此時主機節點變成了 172.16.178.4 了

 4)、鏈接172.16.178.4 上的redis

image

  當前節點爲主機節點,172.16.178.3 爲從節點

 5)、從新啓動172.16.178.2主機上的redis

image

 6)、此時再一次鏈接主機

image

 7)、主機set 一個值

image

 8)、1號從機get 主機中設置的key

image

 9)、2號從機get 主機中設置的key

image
日誌

相關文章
相關標籤/搜索