Redis哨兵機制,一主二從

注:Redis哨兵切換,建議一主多從

(查看Redis信息和狀態,https://my.oschina.net/tongyufu/blog/405612)html

1、一主二從

  教程步驟:https://my.oschina.net/u/2331760/blog/2243505redis

2、哨兵配置(sentinel.conf)

  cd /usr/local/redis-ms/  vim

  1.建立哨兵目錄

    mkdir -p ./sentinel/26001/tmp測試

    cp /usr/local/redis/sentinel.conf ./sentinel/26001/spa

  2.修改./sentinel/.conf

    cd /usr/local/redis-ms/sentinel/.net

    vim ./26001/sentinel.conf命令行

    #修改以下鍵值對rest

    # bind 127.0.0.1 192.168.1.1#註釋掉或者值爲0.0.0.0server

    protected-mode no#關閉保護模式htm

    port 26001#端口號

    daemonize yes#後臺運行

    dir /usr/local/redis-ms/sentinel/26001/tmp#解除掛載信息目錄

    sentinel monitor mymaster 192.168.194.131 6381 1#設置 主名稱 ip地址 端口號 參入選舉的哨兵數

    sentinel down-after-milliseconds mymaster 3000#sentinel心跳檢測主3秒內無響應,視爲掛掉,開始切換其餘從爲主

    sentinel parallel-syncs mymaster 1#每次最多能夠有1個從同步主。一個從同步結束,另外一個從開始同步。

    sentinel failover-timeout mymaster 18000#主從切換超時時間

3、啓動哨兵配置

  1.先啓主從redis

    /usr/local/redis/src/redis-server /usr/local/redis-ms/6381/redis.conf

    /usr/local/redis/src/redis-server /usr/local/redis-ms/6382/redis.conf

    /usr/local/redis/src/redis-server /usr/local/redis-ms/6383/redis.conf

    ps -ef|grep redis

    如圖已經啓動

    

    且信息正確

    /usr/local/redis/src/redis-cli -p 6381

    info

    

   2.啓動哨兵

    /usr/local/redis/src/redis-sentinel /usr/local/redis-ms/sentinel/26001/sentinel.conf 

    ps -ef|grep redis

    如圖已經啓動

    

    且信息正確

    /usr/local/redis/src/redis-cli -p 26001

    info

     

  3.測試

    1)主6381停掉後變成從,原有的兩個從,有一個升級爲主。

      /usr/local/redis/src/redis-cli -p 6381 shutdown

      /usr/local/redis/src/redis-cli -p 26001

      info

      

    2)6381再次啓動後,依然是從

      /usr/local/redis/src/redis-server /usr/local/redis-ms/6381/redis.conf

      /usr/local/redis/src/redis-cli -p 6381

      info

      

5、哨兵集羣

  cd /usr/local/redis-ms/sentinel/

  1.複製26001

    cp -r 26001 26002

  2.修改26002/sentinel.conf

    vim 26002/sentinel.conf

    #將文本中26001替換爲26002

    :%s/26001/26002/g

  3.啓動

    /usr/local/redis/src/redis-sentinel /usr/local/redis-ms/sentinel/26002/sentinel.conf

    info

    /usr/local/redis/src/redis-cli -p 26002

    

 

6.哨兵開機啓動

  注:redis開機啓動參照https://www.cnblogs.com/zwcry/p/9046207.html  

  1.建立sh腳本目錄

    mkdir /usr/local/redis-ms/sentinel/script

    cd /usr/local/redis-ms/sentinel/script/

  2.編輯start.sh

    vim start.sh   

#!/bin/sh
/usr/local/redis/src/redis-sentinel /usr/local/redis-ms/sentinel/26001/sentinel.conf
/usr/local/redis/src/redis-sentinel /usr/local/redis-ms/sentinel/26002/sentinel.conf

  3.編輯stop.sh

    vim stop.sh

#!/bin/sh
/usr/local/redis/src/redis-cli -p 26001 shutdown
/usr/local/redis/src/redis-cli -p 26002 shutdown

  4.編輯restart.sh

    vim restart.sh   

#!/bin/sh
systemctl stop redis-sentinel
systemctl start redis-sentinel

  5.編寫redis-sentinel.service

    cd /usr/lib/systemd/system/

    vim redis-sentinel.service

[Unit]
Description=redis-sentinel
After=redis-ms.service
[Service]
Type=forking
ExecStart=/usr/local/redis-ms/sentinel/script/start.sh
ExecStop=/usr/local/redis-ms/sentinel/script/stop.sh
ExecReload=/usr/local/redis-ms/sentinel/script/restart.sh
[Install]
WantedBy=multi-user.target

#建議啓動redis服務後,再啓動哨兵  

  6.命令行

    改變權限

      chmod 777 redis-sentinel.service

      chmod 777 /usr/local/redis-ms/sentinel/script/*

    進程服務重加載

      systemctl daemon-reload

    開機啓動哨兵

      systemctl enable redis-sentinel.service

    啓動哨兵

      systemctl start redis-sentinel.service

    關閉哨兵

      systemctl stop redis-sentinel.service

    重啓哨兵

      systemctl restart redis-sentinel.service

 須要注意的是:

        若是測試sentinel的時候,始終沒有在sentinel的窗口中看到slave,宕掉master,亦不能進行主從切換,那麼可能的緣由是:redis.conf中配置了密碼,而sentinel裏面沒有配置密碼項,致使監測不到redis,這個時候必須加上密碼受權配置,

而且此sentinel monitor <master-name> <ip> <redis-port> <quorum>

需放在sentinel auth-pass <mymaster> <0123passw0rd>的前面。

備註:哨兵集羣服務自主關聯響應,若是須要更多哨兵集羣,按照26002的方式複製26003...26006便可

相關文章
相關標籤/搜索