Redis3.2.11主從集羣模式

本文一切安裝、測試、開發均以上一穩定版本3.2.11爲準,不建議採用Windows版本,因爲Redis官方沒有任何聲明支持Windows版本,市面上的Windows版本爲微軟XX開源小組維護。

單機安裝

安裝

安裝環境:CentOS 6.5 x86_64版本,理論上CentOS6.x/RHEL 6.x的安裝方式一樣。但極可能不適用於CentOS 7.x/RHEL 7.x版本。

執行命令如下
tar -zxvf redis-3.2.11.tar.gz
cd redis-3.2.11
make
#下面這一句最好執行,意思是測試,驗證環境是否正確
make test
執行命令完成之後,會在src目錄下生產redis-server 和redis-cli文件.
注意:如果執行命令期間出現錯誤(請見下文常見錯誤),請修復錯誤之後,刪除redis-3.2.11目錄,重新解壓安裝包,重新執行上述步驟。

基礎配置

配置文件目錄默認爲 Redis解壓目錄/redis.conf。如 redis-3.2.11/redis.conf (注意沒有目錄src)。
請使用FTP將上述文件下載到本地,然後用記事本或EditPlus打開,
修改以下兩行(修改之前注意備份)
#前面增加#,即將此行註釋掉
bind 127.0.0.1   
去掉前面的# ,修改爲requirepass solution(設置redis連接密碼)
#requirepass foobared   
#修改爲 daemonize yes   以使redis在後臺以守護進程的方式運行
daemonize no 
#在“”中填入日誌文件路徑,如 
#logfile “/home/peter/redis/redis-3.2.11/log/redis.log” 注意保證路徑存在
logfile ""   
修改保存後,通過FTP上傳覆蓋掉原有文件。

啓動

#啓動redis-server
cd src
./redis-server ../redis.conf
#啓動客戶端工具 ,可以重新打開一個terminal窗口
#cd 到 redis-3.2.11/src 目錄下
./redis-cli
SET name redis-server
GET name

啓動結果校驗
tail -f log/redis.log
可以看到啓動日誌

停止

在啓動命令窗口 ctrl + c  或者直接殺掉進程,如下
ps -aux|grep redis
#通過上述命令查看進程ID
kill -9 進程ID

常見錯誤

1.make[2]: cc: Command not found  或者 gcc: Command not found
異常原因:沒有安裝gcc
解決方案:yum install gcc

2.  couldn't execute "tclsh8.5": no such file or directory
異常原因:沒有安裝tcl
解決方案:yum install tcl。

3.
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error "Newer version of jemalloc required"
將make命令 替換爲 make MALLOC=libc 

啓動報警的解決

啓動Redis常見一些警告信息,這些警告的解決辦法請參考如下鏈接
http://blog.csdn.net/a491857321/article/details/52006376
筆者Redis的實際項目經驗也不豐富,這些警告的嚴重後果不得而知,建議按照上文連接修改。有經驗的朋友歡迎指教。


主從集羣模式

三臺虛擬機,IP分別爲192.168.137.200 ~ 202
其中200規劃爲Master,其餘兩個爲Slave
三臺虛擬機均作爲Redis Sentinel的節點,即整體結構如下:
 

Redis Server配置和啓動

Redis-3.2.11/redis.conf文件,在基礎配置之上,增加如下配置:
masterauth
#設置Master訪問密碼,因爲failover之後,Master也會作爲Slave,所以Master\Slave均需配置此項
masterauth solution   
slaveof
#在兩個Slave的redis.conf中,配置Slave項
slaveof 192.168.137.200 6379

按照啓動中的說明,依次啓動Master、Slave節點。注意必須先啓動Master節點。Slave節點彼此之間無順序要求。

Redis Sentinel配置

在Master/slave01/slave02三臺機器上,均修改如下文件
安裝路徑/Redis-3.2.11/sentinel.conf.

Monitor配置

查找配置項 
sentinel monitor mymaster 127.0.0.1 6379 2
修改爲(僅變化IP,注意mymaster不需要變,一旦修改,需要同時修改若干關聯配置)
sentinel monitor mymaster 192.168.137.200 6379 2

配置說明如下
https://redis.io/topics/sentinel   (Configuring Sentinel)
Master密碼配置
# sentinel auth-pass <master-name> <password>
取消註釋#,修改爲(配置Master訪問密碼)
sentinel auth-pass mymaster solution
關閉保護模式(默認本地才能訪問,不關閉則多臺機器之間Sentinel無法通信,無法選舉,無法發生failover)
protected-mode no
日誌和守護進程
增加如下配置項,意思等同於Redis-Server的配置,不做贅述
daemonize yes
logfile "/usr/local/redis-3.2.11/log/sentinel.log"
其餘配置項採用默認配置即可。

然後在三臺機器上(不分先後順序),分別執行以下命令以啓動哨兵進程

# cd 到redis安裝目錄
#啓動哨兵進程
src/redis-sentinel sentinel.conf
#查看啓動日誌
tail -f log/sentinel.log

主備集羣搭建完畢!

驗證

1. 停掉master節點,可直接殺掉進程 kill -9 xxxx
2. 查看任意Sentinel的日誌  tail -f log/sentinel.log ,裏面會有+sdown/+odown/failover信息
3. 查看原slave01/slave02的節點信息  src/redis-cli info, 找到Replication段落,查看Role屬性,是否其中一臺變成了Master
4. 重新啓動原master節點, src/redis-server redis.conf
5. 查看任意Sentinel的日誌,tail -f log/sentinel.log
6. 查看剛啓動的原master節點信息,src/redis-cli info, 找到Replication段落,查看Role屬性,值應該爲Slave

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