1.Reids安裝包裡有個叢集工具,要複製到/usr/local/bin裡去

[[email protected] ~]# ll

[[email protected] ~]# cd redis-3.2.9/src

[[email protected] src]# ll


[[email protected] src]# cp redis-trib.rb /usr/local/bin

[[email protected] ~]# cd /usr/local/bin;ll

2.建立節點,修改配置檔案

我們現在要搞六個節點,三主三從,

埠規定分別是7001,7002,7003,7004,7005,7006

我們先在root目錄下新建一個redis_cluster目錄,然後該目錄下再建立6個目錄,

分別是7001,7002,7003,7004,7005,7006,用來存在redis配置檔案;

這裡我們要使用redis叢集,要先修改redis的配置檔案redis.conf

具體步驟如下:

[[email protected] ~]# mkdir redis_cluster

[[email protected] ~]# cd redis_cluster/

[[email protected] redis_cluster]# mkdir 7001 7002 7003 7004 7005 7006

先複製一份配置檔案到7001目錄下

[[email protected] redis_cluster]# cd

[[email protected] ~]# cp redis-3.2.9/redis.conf redis_cluster/7001/

[[email protected] ~]# vim redis_cluster/7001/redis.conf 

修改此配置檔案裡面的一下幾項數值

#bind 127.0.0.1  #註釋掉,使其他機器也可訪問

protected-mode no #關閉保護模式

port 7001  #六個節點配置檔案分別是7001-7006

daemonize yes        #redis後臺執行

pidfile /var/run/redis_7001.pid   #pidfile檔案對應7001-7006

cluster-enabled yes   #開啟叢集

cluster-config-file nodes_7001.conf  #儲存節點配置,自動建立,自動更新對應7001-7006

cluster-node-timeout 5000    /#叢集超時時間,節點超過這個時間沒反應就斷定是宕機

7001下的修改完後,我們把7001下的配置分別複製到7002-7006 ,只需修改port 、pidfile、cluster-config-file 即可

[[email protected] ~]# cp redis_cluster/7001/redis.conf redis_cluster/7002/

[[email protected] ~]# cp redis_cluster/7001/redis.conf redis_cluster/7003/

[[email protected] ~]# cp redis_cluster/7001/redis.conf redis_cluster/7004/

[[email protected] ~]# cp redis_cluster/7001/redis.conf redis_cluster/7005/

[[email protected] ~]# cp redis_cluster/7001/redis.conf redis_cluster/7006/

3.分別啟動上述六個節點

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf 


出現以上說明這六個節點已經啟動成功,接下來需要建立叢集

4.建立叢集

這裡需要用到第一步複製到usr/local/bin裡面的redis-trib.rb工具

建立叢集之前需要安裝rubby,以及redis和rubby的連線 

[[email protected] ~]# yum -y install ruby ruby-devel rubygems rpm-build

[[email protected] ~]# gem install redis

會提示


說明redis 需要ruby的版本在2.2.2以上。我們可以看到目前的版本為2.0.0

[[email protected] ~]# yum list ruby


[[email protected] ~]# yum remove ruby  


提示是否刪除依賴,這裡選擇y


採用 rvm來更新ruby

首先需要安裝RVM 具體步驟如下:

1.   [[email protected] ~]# gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3      會下載金鑰,如果連線失敗,多嘗試幾次。

2.   [email protected] ~]# curl -L get.rvm.io | bash -s stable

3.  [[email protected] ~]# find / -name rvm -print

4.  [[email protected] ~]# source /usr/local/rvm/scripts/rvm

5.  [[email protected] ~]#  rvm list known


6.[[email protected] ~]# rvm install 2.3.3   安裝ruby2.3.3此過程可能較慢,


7.  [[email protected] ~]# ruby -v                #檢視當前ruby版本,為2.3.3

8.   [[email protected] ~]# gem install redis    #重新安裝redis即可

redis安裝完成之後即可建立叢集

[[email protected] ~]# redis-trib.rb create --replicas 1  127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006

這一步可能會提示

>>> Creating cluster
[ERR] Node 127.0.0.1:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

類似的錯誤。

其解決方案如下:

1. [[email protected] ~]#  ps -ef | grep redis  #檢視目前redis的程序 殺掉節點的程序


2.到 使用者目錄下 刪除下圖檔案。


3.重新啟動各個節點即可。

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf 

4.以上步驟可迴圈嘗試

建立叢集

[[email protected] ~]# redis-trib.rb create --replicas 1  127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006


從執行結果看 主節點就是7001 7002 7003 從節點分別是7004 7005 7006 


以上說明叢集已經配置成功!

5.測試:

[[email protected] ~]# /usr/local/redis/bin/redis-cli -c -p 7002   #登入節點7002

設定age

登入其他節點獲取,能夠成功獲取到值


6.叢集宕機測試

這裡我們幹掉一個節點,比如7003

[[email protected] ~]#  ps -ef|grep redis
root       9606      1  0 10:50 ?        00:00:06 /usr/local/redis/bin/redis-server *:7001 [cluster]
root       9614      1  0 10:50 ?        00:00:06 /usr/local/redis/bin/redis-server *:7002 [cluster]
root       9623      1  0 10:51 ?        00:00:06 /usr/local/redis/bin/redis-server *:7003 [cluster]
root       9635      1  0 10:51 ?        00:00:06 /usr/local/redis/bin/redis-server *:7004 [cluster]
root       9642      1  0 10:51 ?        00:00:06 /usr/local/redis/bin/redis-server *:7005 [cluster]
root       9649      1  0 10:51 ?        00:00:06 /usr/local/redis/bin/redis-server *:7006 [cluster]
root      10370  10209  0 11:31 pts/4    00:00:00 grep --color=auto redis
[[email protected] ~]#  ps -ef|grep redis
root       9606      1  0 10:50 ?        00:00:06 /usr/local/redis/bin/redis-server *:7001 [cluster]
root       9614      1  0 10:50 ?        00:00:06 /usr/local/redis/bin/redis-server *:7002 [cluster]
root       9635      1  0 10:51 ?        00:00:06 /usr/local/redis/bin/redis-server *:7004 [cluster]
root       9642      1  0 10:51 ?        00:00:06 /usr/local/redis/bin/redis-server *:7005 [cluster]
root       9649      1  0 10:51 ?        00:00:06 /usr/local/redis/bin/redis-server *:7006 [cluster]
root      10381  10209  0 11:32 pts/4    00:00:00 grep --color=auto redis

檢視目前叢集的情況

[[email protected] ~]# redis-trib.rb check 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: ce53acdd719273cf6af746da9760243cf70860b1 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: d155427a31c295179642c22b6936fd5acb31e6a8 127.0.0.1:7006
   slots:10923-16383 (5461 slots) master
   0 additional replica(s)
S: e39fb46a37e63f737b6802f13458b8a1978f7d05 127.0.0.1:7004
   slots: (0 slots) slave
   replicates ce53acdd719273cf6af746da9760243cf70860b1
S: 3aeb4b14c31f1c3f7858236ed1e705d62b824b11 127.0.0.1:7005
   slots: (0 slots) slave
   replicates dacc614b13d4f927cf80a557a74ddc7d737ccf23
M: dacc614b13d4f927cf80a557a74ddc7d737ccf23 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

我們看到 7006本來是從節點,但是7003這個主節點掛了,7006就變成了主節點

同時顯示[OK] All 16384 slots covered. 表明,叢集還可用。

現在我們幹掉7006 

[[email protected] ~]#  ps -ef|grep redis
root       9606      1  0 10:50 ?        00:00:08 /usr/local/redis/bin/redis-server *:7001 [cluster]
root       9614      1  0 10:50 ?        00:00:08 /usr/local/redis/bin/redis-server *:7002 [cluster]
root       9635      1  0 10:51 ?        00:00:08 /usr/local/redis/bin/redis-server *:7004 [cluster]
root       9642      1  0 10:51 ?        00:00:08 /usr/local/redis/bin/redis-server *:7005 [cluster]
root       9649      1  0 10:51 ?        00:00:08 /usr/local/redis/bin/redis-server *:7006 [cluster]
root      10436  10209  0 11:37 pts/4    00:00:00 grep --color=auto redis
[[email protected] ~]# kill -9 9649
[[email protected] ~]# redis-trib.rb check 127.0.0.1:7001
[ERR] Sorry, can't connect to node 127.0.0.1:7006
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: ce53acdd719273cf6af746da9760243cf70860b1 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: e39fb46a37e63f737b6802f13458b8a1978f7d05 127.0.0.1:7004
   slots: (0 slots) slave
   replicates ce53acdd719273cf6af746da9760243cf70860b1
S: 3aeb4b14c31f1c3f7858236ed1e705d62b824b11 127.0.0.1:7005
   slots: (0 slots) slave
   replicates dacc614b13d4f927cf80a557a74ddc7d737ccf23
M: dacc614b13d4f927cf80a557a74ddc7d737ccf23 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.

最後發現 Not all 16384 slots are covered by nodes. 沒有完全覆蓋,叢集不能正常使用

 如需重新啟用,將已經掛掉的節點重新啟動即可

如:命令如下:

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf 

[[email protected] ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf 


叢集還可正常使用