Redis叢集環境安裝指南

銘凡發表於2016-06-07

環境

RHLinux-6.4-64-EN, 紅帽6.4 64位,英文正式釋出版。

 

Redis3.0.0

  • redis2.x版本還不支援叢集,3.0版本將會支援,現在3.0版本還在開發中,現在是beta-3版本(截止2014-5-8),但功能是可用的。
  • 下載Redis3.0.0 beta-3版本,點選這裡下載。
  • Redis3的安裝可以參照之前的單機安裝指南操作。

建立Redis叢集例項

  • 建立叢集節點的資料夾,先建立cluster-test資料夾,再以埠為名稱建立子資料夾。這裡我們要建立6個Redis例項,3個作為master,3個作為slave。
1
2
3
mkdir cluster-test
cd cluster-test
mkdir 7000 7001 7002 7003 7004 7005
  • 在每個資料夾下面建立建立Redis配置檔案,注意根據不同例項的埠號修改下面的配置檔案,nodes.conf檔案為Redis例項啟動時自動生成。
1
2
3
4
5
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
  • 開6個terminal視窗,分別啟動這6個Redis例項。
1
2
cd 7000
../redis-server ./redis.conf
  • 當成功啟動後,能看到每個terminal出現下面的字樣,是因為node.conf檔案不存在,所以給每個例項分配了一個新的ID。
1
[82462] 26 Nov 11:56:55.329 * No cluster configuration found, I`m 97a3a64667477371c4479320d683e4c8db5858b1

建立叢集

  • 現在Redis的6個例項都已經啟動了,現在來開始建立叢集。建立叢集用到了一個ruby檔案,放在redis3目錄的src子目錄下,找到後執行以下命令。這裡的-replicas 1表示每個master配備1個slave,後面的引數就是6個例項的ip加埠,以空格隔開。
1
2
3
cd redis-3.0.0-beta3/src
./redis-trib.rb create --replicas 1 127.0.0.1:7000 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
  • 執行命令後會提示你是否接受提示的配置資訊,預設的是前3臺作為master機器,後3臺作為slave機器,輸入yes,出現最後的資訊表示叢集已經建立好了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Creating cluster
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7003: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7005: OK >>>
Performing hash slots allocation on 6 nodes...
Using 3 masters: 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002
127.0.0.1:7000 replica #1 is 127.0.0.1:7003
127.0.0.1:7001 replica #1 is 127.0.0.1:7004
127.0.0.1:7002 replica #1 is 127.0.0.1:7005
M: 9991306f0e50640a5684f1958fd754b38fa034c9 127.0.0.1:7000 slots:0-5460 (5461 slots) master
M: e68e52cee0550f558b03b342f2f0354d2b8a083b 127.0.0.1:7001 slots:5461-10921 (5461 slots) master
M: 393c6df5eb4b4cec323f0e4ca961c8b256e3460a 127.0.0.1:7002 slots:10922-16383 (5462 slots) master
S: 48b728dbcedff6bf056231eb44990b7d1c35c3e0 127.0.0.1:7003
S: 345ede084ac784a5c030a0387f8aaa9edfc59af3 127.0.0.1:7004
S: 3375be2ccc321932e8853234ffa87ee9fde973ff 127.0.0.1:7005
Can I set the above configuration? (type `yes` to accept): yes
Nodes configuration updated >>>
Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join... >>>
Performing Cluster Check (using node 127.0.0.1:7000)
M: ebbb890e899244e4a2a6303826a29543ebf9885d 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: ebbb890e899244e4a2a6303826a29543ebf9885d 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: 73c22198cd1d0782ec24e3c9b03378030891c9a3 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
M: c785d85b95d7bdc28ec90384ab8a5885b289542c 127.0.0.1:7003
   slots: (0 slots) master
   replicates b949ea2d9c822ff069521a354b4ed48855ac6203
M: 570c472fe553ba3c9d0fb3ba16fcdb8579b4fc86 127.0.0.1:7004
   slots: (0 slots) master
   replicates ebbb890e899244e4a2a6303826a29543ebf9885d
M: f86667ec42cff41a5666162a912713173f5306d7 127.0.0.1:7005
   slots: (0 slots) master
   replicates 73c22198cd1d0782ec24e3c9b03378030891c9a3
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

PS: 安裝過程還發現RedHat系統上沒有安裝ruby,需要先安裝ruby,而且公司機器還不能上網下載安裝,只能通過離線的方式安裝ruby,這裡有離線安裝的文件可以參考一下:https://rvm.io/rvm/offline

驗證

可以使用Redis3.0的redis-cli客戶端進行驗證,需要加上-c引數,表示叢集的意思。

1
2
3
4
5
6
7
8
9
10
11
12
13
$ redis-cli -c -p 7000
redis 127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
redis 127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7000
OK
redis 127.0.0.1:7000> get foo
-> Redirected to slot [12182] located at 127.0.0.1:7002
"bar"
redis 127.0.0.1:7000> get hello
-> Redirected to slot [866] located at 127.0.0.1:7000
"world"

更多的資料可以參考這裡:http://redis.io/topics/cluster-tutorial


相關文章