1、准備工作
准備兩台以上已經安裝Redis的服務器並配置主從,這里以三台安裝了Redis5.0.9的Centos 7 為例子
Redis主從配置:https://www.cnblogs.com/chenppp/p/13443398.html
主節點:192.168.199.50
從節點:192.168.199.51
從節點:192.168.199.52
Redis Sentinel 是一個分布式系統, 你可以在一個架構中運行多個 Sentinel 進程(progress), 這些進程使用流言協議(gossip protocols)來接收關於主服務器是否下線的信息, 並使用投票協議(agreement protocols)來決定是否執行自動故障遷移, 以及選擇哪個從服務器作為新的主服務器。
哨兵有兩個作用
-
通過發送命令,讓Redis服務器返回監控其運行狀態,包括主服務器和從服務器。
-
當哨兵監測到master宕機,會自動將slave切換成master,然后通過發布訂閱模式通知其他的從服務器,修改配置文件,讓它們切換主機
2、配置哨兵模式
1、在編譯redis后的源碼文件中,復制相關配置文件文件到redis安裝目錄
[root@swarm-node1 redis-5.0.9]# cp sentinel.conf /usr/local/redis/conf/
2、配置sentinel.conf
三個redis節點都需要配置哨兵模式
port 26379 # 監聽端口 daemonize yes # 守護進程運行 pidfile "/usr/local/redis/logs/sentinel_26379.pid" # 指定pid文件 logfile "/usr/local/redis/logs/sentinel.log" # 指定log文件 sentinel monitor mymaster 192.168.199.50 6379 2 # 指定redis主節點
sentinel down-after-milliseconds mymaster 30000 # 多少秒內主節點沒有回應,將被認為節點下線,默認為30秒,單位為毫秒 sentinel auth-pass mymaster password # 指定redis主從節點密碼
[root@swarm-node3 bin]# ./redis-sentinel ../conf/sentinel.conf # 啟動進程 [root@swarm-node3 bin]# ps -ef |grep redis-sentinel root 7987 1 0 12:08 ? 00:00:00 ./redis-sentinel *:26379 [sentinel] root 7998 7777 0 12:08 pts/1 00:00:00 grep --color=auto redis-sentinel
啟動三個sentinel節點后查看日志,能看到redis主從信息已經獲取到了
3、驗證主從節點,測試主從切換
kill 掉主節點,查看redis主從會不會自動切換
kill 主節點后,哨兵服務已經檢測到主節點掛掉,已經將主節點切換到192.168.199.52
登錄到192.168.199.52的redis,確認狀態,當前狀態已為主節點狀態
重新啟動kill掉的節點,啟動后會重新接入,成為從節點