redis 哨兵模式配置

redis 哨兵的安裝

一、配置主從

下載源碼:
wget http://download.redis.io/releases/redis-4.0.11.tar.gz
解壓和安裝
tar -xf redis-4.0.11.tar.gz -C /usr/local/
cd /usr/local/
ln -s redis-4.0.11/ redis
cd /usr/local/redis
make MALLOC=libc && make && make install
配置主從: 1主192.168.203.100 2從配置好 192.168.203.101 192.168.203.102
主配置文件:
bind 192.168.203.100 127.0.0.1 #不想監聽0.0.0.0 且,又希望監聽127.0.0.1和本機ip
protected-mode no #無保護模式,如果不打算設置密碼,且使用主從,這個地方一定要設置no
daemonize yes #配置後臺啓動
logfile "./redis.log" #配置redis運行的日誌路徑
pidfile "/var/run/redis_6379.pid" # 默認無需更改

192.168.203.101這個從節點配置文件:
bind 192.168.203.101 127.0.0.1 #不想監聽0.0.0.0 且,又希望監聽127.0.0.1和本機ip
protected-mode no #無保護模式,如果不打算設置密碼,且使用主從,這個地方一定要設置no
daemonize yes #配置後臺啓動
logfile "./redis.log" #配置redis運行的日誌路徑
pidfile "/var/run/redis_6379.pid" # 默認無需更改
slaveof 192.168.203.100 6379

啓動
/usr/local/redis/src/redis-server /usr/local/redis/redis.conf
關閉
redis-cli shutdown
檢查:
netstat -tulnp |grep 6379

配置slave:
slaveof 192.168.203.100 6379

驗證主從正常
redis-cli info replication
其中 master_link_status:up 正常,且主從數據測試能夠同步

二、配置哨兵(三臺機器配置完全一樣)

bind 0.0.0.0
protected-mode no #默認爲no
sentinel monitor mymaster 192.168.203.100 6379 2
dir "/tmp" #默認即可, sentinel的chroot目錄
logfile "./sentinel.log" #需要添加這行配置,否則看不到sentinel的日誌
sentinel down-after-milliseconds mymaster 5000
daemonize yes #後臺啓動

啓動哨兵
/usr/local/redis/src/redis-sentinel /usr/local/redis/sentinel.conf
檢查哨兵狀態
redis-cli -p 26379 INFO Sentinel
關閉哨兵
redis-cli -p 26379 shutdown
查看哨兵日誌
tail -f /tmp/sentinel.log

在哨兵模式下,這兩個文件會被自動控制,並且自動修改的。
redis.conf sentinel.conf

三、如果需要添加認證

添加認證
redis.conf添加的配置
masterauth "123456"
requirepass "123456"
protected-mode yes #當然此時設置了爲no,測試也沒有影響,需要密碼

sentinel.conf 配置文件修改

注意: 一定要搞清楚masterauth 和 requirepass 區別
https://www.cnblogs.com/hexiaohui003/p/10885928.html

四、驗證master 被kill掉後,master會被切換

1 查看日誌:tail -f /tmp/sentinel.log
2 查看狀態: redis-cli -p 26379 INFO Sentinel

五、問題:

  1. master kill掉以後,一直不切換
    2259:X 02 Sep 01:29:47.537 # +elected-leader master mymaster 192.168.203.100 6379
    2259:X 02 Sep 01:29:47.537 # +failover-state-select-slave master mymaster 192.168.203.100 6379
    2259:X 02 Sep 01:29:47.628 # -failover-abort-no-good-slave master mymaster 192.168.203.100 6379
    2259:X 02 Sep 01:29:47.712 # Next failover delay: I will not start a failover before Mon Sep 2 01:35:47 2019

原因:
slave 沒有良好的,原因,是bind沒有監聽本機端口,只監聽了127.0.0.1 。 這是屬於master-slave階段就錯誤了

  1. 認證報錯
    主從同步報錯:
    7710:S 02 Sep 04:15:09.247 # Unable to AUTH to MASTER: -ERR Client sent AUTH, but no password is set

requirepass 在redis.conf中忘了加,requirepass 和 masterauth 需要配套使用

  1. 啓動報錯:
    FATAL CONFIG FILE ERROR
    Reading the configuration file, at line 72

    'sentinel auth-pass mymaster "123456"'
    No such master with specified name.

原因:
sentinel monitor mymaster 這行配置應該在sentinel.conf配置文件中其他sentinel的配置的前面。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章