1. 程式人生 > >Redis Cluster 官方集群搭建指南

Redis Cluster 官方集群搭建指南

config remove 集群搭建 rep fig nbsp 功能 zxvf redirect

安裝ruby環境
因為官方提供的創建集群的工具是用ruby寫的,需要ruby2.2.2+版本支持,ruby安裝需要指定openssl。

安裝openssl

$ wget https://www.openssl.org/source/openssl-1.0.2m.tar.gz
$ tar -zxvf openssl-1.0.2m.tar.gz
$ cd openssl-1.0.2m
$ ./config --prefix=/usr/local/openssl
$ ./config -t
$ make
$ make install
$ openssl version

安裝ruby

$ yum remove ruby
$ wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.2.tar.gz
$ tar -zxvf ruby-2.4.2.tar.gz
$ cd ruby-2.4.2
$ ./configure --with-openssl-dir=/usr/local/openssl
$ make
$ make install
$ sudo ln -s /usr/local/bin/ruby /usr/bin/ruby

安裝rubygems

$ wget https://rubygems.org/rubygems/rubygems-2.3.0.tgz
$ tar -zxvf rubygems-2.3.0.tgz
$ cd rubygems-2.3.0
$ ruby setup.rb

安裝zlib

$ vi /ruby-2.4.2/ext/zlib/Makefile
zlib.o: $(top_srcdir)/include/ruby.h
修改為:
zlib.o: ../../include/ruby.h
$ yum install zlib*
$ cd /ruby-2.4.2/ext/zlib
$ ruby extconf.rb
$ make
$ make install

安裝redis庫
$ gem install redis


ERROR: While executing gem ... (Gem::Exception)
Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources
報這個錯,安裝openssl-devel,並重新編譯ruby即可。
參考:https://www.larshaendler.com/2015/05/20/unable-to-require-openssl-install-openssl-and-rebuild-ruby-on-ubuntu-14-lts/


搭建集群
1、創建集群目錄
首先進入一個新目錄,創建六個以端口號為名字的子目錄。

$ mkdir redis-cluster
$ cd redis-cluster
$ mkdir 9001 9002 9003 9004 9005 9006

2、添加集群配置文件
在文件夾9001~9006中各建一個redis.conf文件,修改對應文件夾的端口,內容如下:

port 9001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
cluster-enabled:用於開實例的集群模式

cluster-conf-file:設定了保存節點配置文件的路徑,默認值為nodes.conf,節點配置文件無須人為修改,它由 Redis集群在啟動時創建, 並在有需要時自動進行更新。

要讓集群正常運作至少需要三個主節點,不過在剛開始試用集群功能時, 強烈建議使用六個節點: 其中三個為主節點, 而其余三個則是各個主節點的從節點。

3、添加redis服務文件
把編譯好的redis-server文件復制到redis-cluster文件夾中。


4、啟動集群實例
進入到9001~90066每個目錄下,啟動每個實例:

$ cd 9001
$ ../redis-server ./redis.conf


5、創建集群
現在我們已經有了六個正在運行中的Redis實例,接下來我們需要使用這些實例來創建集群,並為每個節點編寫配置文件。通過使用Redis集群命令行工具redis-trib,編寫節點配置文件的工作可以非常容易地完成:redis-trib位於Redis源碼的src文件夾中,它是一個Ruby程序,這個程序通過向實例發送特殊命令來完成創建新集群,檢查集群,或者對集群進行重新分片(reshared)等工作。

$ ./redis-trib.rb create --replicas 1 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003 127.0.0.1:9004 127.0.0.1:9005 127.0.0.1:9006
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:9001
127.0.0.1:9002
127.0.0.1:9003
Adding replica 127.0.0.1:9004 to 127.0.0.1:9001
Adding replica 127.0.0.1:9005 to 127.0.0.1:9002
Adding replica 127.0.0.1:9006 to 127.0.0.1:9003
M: bd330d41ffcc57a5a5d32e3f738ddf82c48cfed0 127.0.0.1:9001
   slots:0-5460 (5461 slots) master
M: 688b8cdbdc38fe6b9e81b410aae2f1c048f5907c 127.0.0.1:9002
   slots:5461-10922 (5462 slots) master
M: 33b757db6091e486af2032f1463d1fb07e8e89a7 127.0.0.1:9003
   slots:10923-16383 (5461 slots) master
S: b00b464e4deb93a661755923641d36cadf648fcd 127.0.0.1:9004
   replicates bd330d41ffcc57a5a5d32e3f738ddf82c48cfed0
S: b3ec3a9c125cf168807231a16bacab946974d563 127.0.0.1:9005
   replicates 688b8cdbdc38fe6b9e81b410aae2f1c048f5907c
S: 06a207f7a4dd3023f88e01fad8635cb471d004eb 127.0.0.1:9006
   replicates 33b757db6091e486af2032f1463d1fb07e8e89a7
Can I set the above configuration? (type ‘yes‘ to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join....
>>> Performing Cluster Check (using node 127.0.0.1:9001)
M: bd330d41ffcc57a5a5d32e3f738ddf82c48cfed0 127.0.0.1:9001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 06a207f7a4dd3023f88e01fad8635cb471d004eb 127.0.0.1:9006
   slots: (0 slots) slave
   replicates 33b757db6091e486af2032f1463d1fb07e8e89a7
S: b00b464e4deb93a661755923641d36cadf648fcd 127.0.0.1:9004
   slots: (0 slots) slave
   replicates bd330d41ffcc57a5a5d32e3f738ddf82c48cfed0
M: 688b8cdbdc38fe6b9e81b410aae2f1c048f5907c 127.0.0.1:9002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: b3ec3a9c125cf168807231a16bacab946974d563 127.0.0.1:9005
   slots: (0 slots) slave
   replicates 688b8cdbdc38fe6b9e81b410aae2f1c048f5907c
M: 33b757db6091e486af2032f1463d1fb07e8e89a7 127.0.0.1:9003
   slots:10923-16383 (5461 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.

這表示集群中的 16384 個槽都有至少一個主節點在處理, 集群運作正常。

6、查看集群節點

192.168.10.38:9001> cluster nodes
bd330d41ffcc57a5a5d32e3f738ddf82c48cfed0 127.0.0.1:9001@19001 myself,master - 0 1511774435000 1 connected 0-5460
06a207f7a4dd3023f88e01fad8635cb471d004eb 127.0.0.1:9006@19006 slave 33b757db6091e486af2032f1463d1fb07e8e89a7 0 1511774436000 6 connected
b00b464e4deb93a661755923641d36cadf648fcd 127.0.0.1:9004@19004 slave bd330d41ffcc57a5a5d32e3f738ddf82c48cfed0 0 1511774436557 4 connected
688b8cdbdc38fe6b9e81b410aae2f1c048f5907c 127.0.0.1:9002@19002 master - 0 1511774436557 2 connected 5461-10922
b3ec3a9c125cf168807231a16bacab946974d563 127.0.0.1:9005@19005 slave 688b8cdbdc38fe6b9e81b410aae2f1c048f5907c 0 1511774436657 5 connected
33b757db6091e486af2032f1463d1fb07e8e89a7 127.0.0.1:9003@19003 master - 0 1511774436000 3 connected 10923-16383

連接使用集群

$ ./redis-cli -c -h 192.168.1.8 -p 9002 -a 123456

-c:cluster,連接到集群模式,否則key不落在本實例將會報錯。
-h:host,指定連接主機。
-p:port,指定連接端口。
-a:auth,指定密碼,集群模式需要指定,不然移動會認證失敗。

$ ./redis-cli -c -h 192.168.1.8 -p 9002 -a 123456
127.0.0.1:9002> set hnad 21233
-> Redirected to slot [2114] located at 127.0.0.1:9001
OK

如上,鍵hnad被轉移到實例9001。

主從復制不能同步問題解決
搭建一個3主3從的Redis Cluster集群發現從實例不能同步主實例的數據,但確認搭建的步驟和參數都沒啥問題啊,官網也沒有給出對應的問題解決方案。

解決方案
後來查各種資料發現,是因為主實例設置了密碼,從實例配置中需要配置主實例的連接密碼才能實現主從復制同步。

就是下面這個配置:

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth <master-password>

翻譯:
如果master是密碼保護的,下面的配置就是可以告訴從實例在啟動集群同步復制進程之前要經過認證,否則主實例會拒絕從實例的請求。
解決流程
所以,要解決不同步問題,先停止6個從實例,然後在每個實例的redis.conf文件中加入對應主備實例的認證密碼,然後再啟動各個從實例。如:

masterauth 123456

然後主實例上的數據實時變化都會同步到從實例,問題解決。

Redis Cluster 官方集群搭建指南