1. 程式人生 > >redis 的一主二從三哨兵模式

redis 的一主二從三哨兵模式

概述

在部署redis 的時候,如果redis宕機,快取將不可用,redis提供了哨兵模式保證redis實現高可用。

即一臺主機兩臺從機,三臺哨兵主機,如果主例項宕機,哨兵將將一臺從機升級為主機。實現高可用。

配置方法

1.IP地址配置如下

主 127.0.0.1 6001

從 127.0.0.1 6002

從 127.0.0.1 6003

哨兵

127.0.0.1 16001

127.0.0.1 16002

127.0.0.1 16002

2.修改配置

將 redis.con 拷貝兩份 redis1.conf redis2.conf

修改配置如下:

編輯 redis.conf

bind 192.168.1.88 127.0.0.1

protected-mode no

port 6001

pidfile "/var/run/redis_6001.pid"

編輯 redis1.conf

bind 192.168.1.88 127.0.0.1

protected-mode no

port 6002

pidfile "/var/run/redis_6002.pid"

slaveof 127.0.0.1 6001

編輯 redis2.conf 

bind 192.168.1.88 127.0.0.1

protected-mode no

port 6003

pidfile "/var/run/redis_6003.pid"

slaveof 127.0.0.1 6001

 

編輯 哨兵檔案

將哨兵檔案拷貝兩份

sentinel.conf sentinel1.conf sentinel2.conf

編輯 sentinel.conf

port 16001

daemonize yes

sentinel monitor mymaster 127.0.0.1 6001 2

編輯 sentinel1.conf

port 16002

daemonize yes

sentinel monitor mymaster 127.0.0.1 6001 2

編輯 sentinel2.conf

port 16003

daemonize yes

sentinel monitor mymaster 127.0.0.1 6001 2

配置完成

 

3.啟動 redis

./bin/redis-server etc/redis.conf 

./bin/redis-server etc/redis1.conf

./bin/redis-server etc/redis2.conf  

啟動哨兵

./bin/redis-sentinel ./etc/sentinel.conf 

./bin/redis-sentinel ./etc/sentinel1.conf 

./bin/redis-sentinel ./etc/sentinel2.conf 

 

 

驗證

新開一個命令列視窗進入redis的src目錄,用redis-cli工具登入其中一個哨兵

./bin/redis-cli -p 16001

連線成功後執行如下命令

sentinel master mymaster

我們可以看到主機埠為 6001

我們手工關閉 6001的例項。

可以看到 6001 斷開了。

可以看到6002 變為主機了。可以看到 6002 的配置檔案 slaveof 127.0.0.1 6001 沒有了。

測試設定資料:

連線到 6002

./bin/redis-cli -p 6002

set name redis

連線到6001

get name 

可以獲取到資料 redis

我們連線到6003 ,設定資料,我們可以從機是不能設定資料的。

sentinel 作用

A、Master 狀態監測

B、如果Master 異常,則會進行Master-slave 轉換,將其中一個Slave作為Master,將之前的Master作為Slave 

C、Master-Slave切換後,redis.conf、redis1.conf和redis2.conf,sentinel.conf 的內容都會發生改變,sentinel.conf的監控目標會隨之調換 

sentinel monitor mymaster 127.0.0.1 6002 2