1. 程式人生 > >全面解析redis cluster

全面解析redis cluster

全面剖析Redis Cluster原理和應用

1.Redis Cluster總覽

1.1 設計原則和初衷

官方文件Cluster Spec中,作者詳細介紹了Redis叢集為什麼要設計成現在的樣子。最核心的目標有三個:

  1. 效能:這是Redis賴以生存的看家本領,增加叢集功能後當然不能對效能產生太大影響,所以Redis採取了P2P而非Proxy方式、非同步複製、客戶端重定向等設計,而犧牲了部分的一致性、使用性。
  2. 水平擴充套件:叢集的最重要能力當然是擴充套件,文件中稱可以線性擴充套件到1000結點。
  3. 可用性:在Cluster推出之前,可用性要靠Sentinel保證。有了叢集之後也自動具有了Sentinel的監控和自動Failover能力。

1.2 架構變化與CAP理論

Redis Cluster叢集功能推出已經有一段時間了。在單機版的Redis中,每個Master之間是沒有任何通訊的,所以我們一般在Jedis客戶端或者Codis這樣的代理中做Pre-sharding。按照CAP理論來說,單機版的Redis屬於保證CP(Consistency & Partition-Tolerancy)而犧牲A(Availability),也就說Redis能夠保證所有使用者看到相同的資料(一致性,因為Redis不自動冗餘資料)和網路通訊出問題時,暫時隔離開的子系統能繼續執行(分割槽容忍性,因為Master之間沒有直接關係,不需要通訊),但是不保證某些結點故障時,所有請求都能被響應(可用性,某個Master結點掛了的話,那麼它上面分片的資料就無法訪問了)。

有了Cluster功能後,Redis從一個單純的NoSQL記憶體資料庫變成了分散式NoSQL資料庫,CAP模型也從CP變成了AP。也就是說,通過自動分片和冗餘資料,Redis具有了真正的分散式能力,某個結點掛了的話,因為資料在其他結點上有備份,所以其他結點頂上來就可以繼續提供服務,保證了Availability。然而,也正因為這一點,Redis無法保證曾經的強一致性了。這也是CAP理論要求的,三者只能取其二。

關於CAP理論的通俗講解,請參考我的譯文《可能是CAP理論的最好解釋 》。簡單分析了Redis在架構上的變化後,咱們就一起來體驗一下Redis Cluster功能吧!

2.Redis叢集初探

Redis的安裝很簡單,以前已經介紹過,就不詳細說了。關於Redis Cluster的基礎知識之前也有過整理,請參考《Redis叢集功能預覽》。如果需要全面的瞭解,那一定要看官方文件Cluster Tutorial,只看這一個就夠了!

2.1 叢集配置

要想開啟Redis Cluster模式,有幾項配置是必須的。此外為了方便使用和後續的測試,我還額外做了一些配置:

  • 繫結地址:bind 192.168.XXX.XXX。不能繫結到127.0.0.1或localhost,否則指導客戶端重定向時會報”Connection refused”的錯誤。
  • 開啟Cluster:cluster-enabled yes
  • 叢集配置檔案:cluster-config-file nodes-7000.conf。這個配置檔案不是要我們去配的,而是Redis執行時儲存配置的檔案,所以我們也不可以修改這個檔案。
  • 叢集超時時間:cluster-node-timeout 15000。結點超時多久則認為它宕機了。
  • 槽是否全覆蓋:cluster-require-full-coverage no。預設是yes,只要有結點宕機導致16384個槽沒全被覆蓋,整個叢集就全部停止服務,所以一定要改為no
  • 後臺執行:daemonize yes
  • 輸出日誌:logfile “./redis.log”
  • 監聽埠:port 7000

配置好後,根據我們的叢集規模,拷貝出來幾份同樣的配置檔案,唯一不同的就是監聽埠,可以依次改為7001、7002… 因為Redis Cluster如果資料冗餘是1的話,至少要3個Master和3個Slave,所以我們拷貝出6個例項的配置檔案。為了避免相互影響,為6個例項的配置檔案建立獨立的資料夾。

[[email protected]8gVm redis-3.0.4]# pwd
/root/Software/redis-3.0.4
[[email protected]8gVm redis-3.0.4]# tree -I "*log|nodes*" cfg-cluster/
cfg-cluster/
├── 7000
│   └── redis.conf.7000
├── 7001
│   └── redis.conf.7001
├── 7002
│   └── redis.conf.7002
├── 7003
│   └── redis.conf.7003
├── 7004
│   └── redis.conf.7004
└── 7005
    └── redis.conf.7005

6 directories, 6 files
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

2.2 redis-trib管理器

Redis作者應該是個Ruby愛好者,Ruby客戶端就是他開發的。這次叢集的管理功能沒有嵌入到Redis程式碼中,於是作者又順手寫了個叫做redis-trib的管理指令碼。redis-trib依賴Ruby和RubyGems,以及redis擴充套件。可以先用which命令檢視是否已安裝ruby和rubygems,用gem list –local檢視本地是否已安裝redis擴充套件。

最簡便的方法就是用apt或yum包管理器安裝RubyGems後執行gem install redis。如果網路或環境受限的話,可以手動安裝RubyGems和redis擴充套件(國外連結可能無法下載,可以從CSDN下載):

[[email protected]8gVm Software]# wget https://github.com/rubygems/rubygems/releases/download/v2.2.3/rubygems-2.2.3.tgz
[[email protected]8gVm Software]# tar xzvf rubygems-2.2.3.tgz 
[[email protected]8gVm Software]# cd rubygems-2.2.3
[[email protected]8gVm rubygems-2.2.3]# ruby setup.rb --no-rdoc --no-ri

[[email protected]8gVm Software]# wget https://rubygems.org/downloads/redis-3.2.1.gem
[[email protected]8gVm Software]# gem install redis-3.2.1.gem --local --no-rdoc --no-ri
Successfully installed redis-3.2.1
1 gem installed
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.3 叢集建立

首先,啟動我們配置好的6個Redis例項。

[[email protected]8gVm redis-3.0.4]# for ((i=0; i<6; ++i))
> do
> cd cfg-cluster/700$i && ../../src/redis-server redis.conf.700$i && cd -
> done
  • 1
  • 2
  • 3
  • 4

此時6個例項還沒有形成叢集,現在用redis-trb.rb管理指令碼建立起叢集。可以看到,redis-trib預設用前3個例項作為Master,後3個作為Slave。因為Redis基於Master-Slave做資料備份,而非像Cassandra或Hazelcast一樣不區分結點角色,自動複製並分配Slot的位置到各個結點

[[email protected] redis-3.0.4]# src/redis-trib.rb create --replicas 1 192.168.1.100:7000 192.168.1.100:7001 192.168.1.100:7002 192.168.1.100:7003 192.168.1.100:7004 192.168.1.100:7005
>>> Creating cluster
Connecting to node 192.168.1.100:7000: OK
Connecting to node 192.168.1.100:7001: OK
Connecting to node 192.168.1.100:7002: OK
Connecting to node 192.168.1.100:7003: OK
Connecting to node 192.168.1.100:7004: OK
Connecting to node 192.168.1.100:7005: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.100:7000
192.168.1.100:7001
192.168.1.100:7002
Adding replica 192.168.1.100:7003 to 192.168.1.100:7000
Adding replica 192.168.1.100:7004 to 192.168.1.100:7001
Adding replica 192.168.1.100:7005 to 192.168.1.100:7002
    ...
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 192.168.1.100:7000)
    ...
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  • 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

至此,叢集就已經建立成功了!“貼心”的Redis還在utils/create-cluster下提供了一個create-cluster指令碼,能夠創建出一個叢集,類似我們上面建立起的3主3從的叢集。

2.4 簡單測試

我們連線到叢集中的任意一個結點,啟動redis-cli時要加-c選項,存取兩個Key-Value感受一下Redis久違的叢集功能。

[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000
192.168.1.100:7000> set foo bar
-> Redirected to slot [12182] located at 192.168.1.100:7002
OK
192.168.1.100:7002> set hello world
-> Redirected to slot [866] located at 192.168.1.100:7000
OK
192.168.1.100:7000> get foo
-> Redirected to slot [12182] located at 192.168.1.100:7002
"bar"
192.168.1.100:7002> get hello
-> Redirected to slot [866] located at 192.168.1.100:7000
"world"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

仔細觀察能夠注意到,redis-cli根據指示,不斷在7000和7002結點之前重定向跳轉。如果啟動時不加-c選項的話,就能看到以錯誤形式顯示出的MOVED重定向訊息。

[[email protected]8gVm redis-3.0.4]# src/redis-cli -h 192.168.1.100 -p 7000
192.168.1.100:7000> get foo
(error) MOVED 12182 192.168.1.100:7002
  • 1
  • 2
  • 3

2.5 叢集重啟

目前redis-trib的功能還比較弱,需要重啟叢集的話先手動kill掉各個程序,然後重新啟動就可以了。這也有點太… 網上有人重啟後會碰到問題,我還比較幸運,這種“土鱉”的方式重啟試了兩次還沒發現問題。

[[email protected]8gVm redis-3.0.4]# ps -ef | grep redis | awk '{print $2}' | xargs kill
  • 1

3.高階功能嚐鮮

說是“高階功能”,其實在其他分散式系統中早就都有實現了,只不過在Redis世界裡是比較新鮮的。本部分主要試驗一下Redis Cluster中的資料遷移(Resharding)和故障轉移功能。

3.1 資料遷移

本小節我們體驗一下Redis叢集的Resharding功能!

3.1.1 建立測試資料

首先儲存foo1~10共10個Key-Value作為測試資料。

[[email protected]8gVm redis-3.0.4]# for ((i=0; i<10; ++i))
> do
> src/redis-cli -c -h 192.168.1.100 -p 7000 set foo$i bar
> done

[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000
192.168.1.100:7000> keys *
1) "foo6"
2) "foo7"
3) "foo3"
4) "foo2"
192.168.1.100:7000> get foo4
-> Redirected to slot [9426] located at 192.168.1.100:7001
"bar"
192.168.1.100:7001> keys *
1) "foo4"
2) "foo8"
192.168.1.100:7001> get foo5
-> Redirected to slot [13555] located at 192.168.1.100:7002
"bar"
192.168.1.100:7002> keys *
1) "foo5"
2) "foo1"
3) "foo10"
4) "foo9"
  • 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

3.1.2 啟動新結點

參照之前的方法新拷貝出兩份redis.conf配置檔案redis.conf.7010和7011,與之前結點的配置檔案做一下區分。啟動新的兩個Redis例項之後,通過redis-trib.rb指令碼新增新的Master和Slave到叢集中。

[[email protected]8gVm redis-3.0.4]# cd cfg-cluster/7010 && ../../src/redis-server redis.conf.7010 && cd -
[[email protected]8gVm redis-3.0.4]# cd cfg-cluster/7011 && ../../src/redis-server redis.conf.7011 && cd -
  • 1
  • 2

3.1.3 新增到叢集

使用redis-trib.rb add-node分別將兩個新結點新增到叢集中,一個作為Master,一個作為其Slave。

[[email protected]8gVm redis-3.0.4]# src/redis-trib.rb add-node 192.168.1.100:7010 192.168.1.100:7000
>>> Adding node 192.168.1.100:7010 to cluster 192.168.1.100:7000
Connecting to node 192.168.1.100:7000: OK
Connecting to node 192.168.1.100:7001: OK
Connecting to node 192.168.1.100:7002: OK
Connecting to node 192.168.1.100:7005: OK
Connecting to node 192.168.1.100:7003: OK
Connecting to node 192.168.1.100:7004: OK
>>> Performing Cluster Check (using node 192.168.1.100:7000)
    ...
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Connecting to node 192.168.1.100:7010: OK
>>> Send CLUSTER MEET to node 192.168.1.100:7010 to make it join the cluster.
[OK] New node added correctly.

[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
0d1f9c979684e0bffc8230c7bb6c7c0d37d8a5a9 192.168.1.100:7010 master - 0 1442452249525 0 connected
    ...

[[email protected]8gVm redis-3.0.4]# src/redis-trib.rb add-node --slave --master-id 0d1f9c979684e0bffc8230c7bb6c7c0d37d8a5a9 192.168.1.100:7011 192.168.1.100:7000
>>> Adding node 192.168.1.100:7011 to cluster 192.168.1.100:7000
Connecting to node 192.168.1.100:7000: OK
Connecting to node 192.168.1.100:7010: OK
Connecting to node 192.168.1.100:7001: OK
Connecting to node 192.168.1.100:7002: OK
Connecting to node 192.168.1.100:7005: OK
Connecting to node 192.168.1.100:7003: OK
Connecting to node 192.168.1.100:7004: OK
>>> Performing Cluster Check (using node 192.168.1.100:7000)
    ...
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Connecting to node 192.168.1.100:7011: OK
>>> Send CLUSTER MEET to node 192.168.1.100:7011 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.1.100:7010.
[OK] New node added correctly.
  • 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

3.1.4 Resharding

通過redis-trib.rb reshard可以互動式地遷移Slot。下面的例子將5000個Slot從7000~7002遷移到7010上。也可以通過./redis-trib.rb reshard <host>:<port> --from <node-id> --to <node-id> --slots --yes在程式中自動完成遷移。

[[email protected] redis-3.0.4]# src/redis-trib.rb reshard 192.168.1.100:7000
Connecting to node 192.168.1.100:7000: OK
Connecting to node 192.168.1.100:7010: OK
Connecting to node 192.168.1.100:7001: OK
Connecting to node 192.168.1.100:7002: OK
Connecting to node 192.168.1.100:7005: OK
Connecting to node 192.168.1.100:7011: OK
Connecting to node 192.168.1.100:7003: OK
Connecting to node 192.168.1.100:7004: OK
>>> Performing Cluster Check (using node 192.168.1.100:7000)
M: b2036adda128b2eeffa36c3a2056444d23b548a8 192.168.1.100:7000
   slots:0-5460 (4128 slots) master
   1 additional replica(s)
M: 0d1f9c979684e0bffc8230c7bb6c7c0d37d8a5a9 192.168.1.100:7010
   slots:0 (4000 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.
How many slots do you want to move (from 1 to 16384)? 5000
What is the receiving node ID? 0d1f9c979684e0bffc8230c7bb6c7c0d37d8a5a9
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1:all

[[email protected] redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
0d1f9c979684e0bffc8230c7bb6c7c0d37d8a5a9 192.168.1.100:7010 master - 0 1442455872019 7 connected 0-1332 5461-6794 10923-12255
b2036adda128b2eeffa36c3a2056444d23b548a8 192.168.1.100:7000 myself,master - 0 0 1 connected 1333-5460
b5ab302f5c2395e3c8194c354a85d02f89bace62 192.168.1.100:7001 master - 0 1442455875022 2 connected 6795-10922
0c565e207ce3118470fd5ed3c806eb78f1fdfc01 192.168.1.100:7002 master - 0 1442455874521 3 connected 12256-16383
    ...
  • 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

遷移完成後,檢視之前儲存的foo1~10的分佈情況,可以看到部分Key已經遷移到了新的結點7010上。

[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000 keys "*"
1) "foo3"
2) "foo7"
[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7001 keys "*"
1) "foo4"
2) "foo8"
3) "foo0"
[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7002 keys "*"
1) "foo1"
2) "foo9"
3) "foo5"
[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7010 keys "*"
1) "foo6"
2) "foo2"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3.2 故障轉移

在高可用性方面,Redis可算是能夠”Auto”一把了!Redis Cluster重用了Sentinel的程式碼邏輯,不需要單獨啟動一個Sentinel叢集,Redis Cluster本身就能自動進行Master選舉和Failover切換

下面我們故意kill掉7010結點,之後可以看到結點狀態變成了fail,而Slave 7011被選舉為新的Master。

[[email protected]8gVm redis-3.0.4]# kill 43637

[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
0d1f9c979684e0bffc8230c7bb6c7c0d37d8a5a9 192.168.1.100:7010 master,fail - 1442456829380 1442456825674 7 disconnected
b2036adda128b2eeffa36c3a2056444d23b548a8 192.168.1.100:7000 myself,master - 0 0 1 connected 1333-5460
b5ab302f5c2395e3c8194c354a85d02f89bace62 192.168.1.100:7001 master - 0 1442456848722 2 connected 6795-10922
0c565e207ce3118470fd5ed3c806eb78f1fdfc01 192.168.1.100:7002 master - 0 1442456846717 3 connected 12256-16383
5a3c67248b1df554fbf2c93112ba429f31b1d3d1 192.168.1.100:7005 slave 0c565e207ce3118470fd5ed3c806eb78f1fdfc01 0 1442456847720 6 connected
99bff22b97119cf158d225c2b450732a1c0d3c44 192.168.1.100:7011 master - 0 1442456849725 8 connected 0-1332 5461-6794 10923-12255
cd305d509c34842a8047e19239b64df94c13cb96 192.168.1.100:7003 slave b2036adda128b2eeffa36c3a2056444d23b548a8 0 1442456848220 4 connected
64b544cdd75c1ce395fb9d0af024b7f2b77213a3 192.168.1.100:7004 slave b5ab302f5c2395e3c8194c354a85d02f89bace62 0 1442456845715 5 connected
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

嘗試查詢之前儲存在7010上的Key,可以看到7011頂替上來繼續提供服務,整個叢集沒有受到影響。

[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000 get foo6
"bar"
[[email protected]8gVm redis-3.0.4]# 
[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000 get foo2
"bar"
  • 1
  • 2
  • 3
  • 4
  • 5

4.內部原理剖析

前面我們已經學習過,用Redis提供的redis-trib或create-cluster指令碼能幾步甚至一步就建立起一個Redis叢集。這一部分我們為了深入學習,所以要暫時拋開這些方便的工具,完全手動建立一遍上面的3主3從叢集。

4.1 叢集發現:MEET

最開始時,每個Redis例項自己是一個叢集,我們通過cluster meet讓各個結點互相“握手”。這也是Redis Cluster目前的一個欠缺之處:缺少結點的自動發現功能

[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
33c0bd93d7c7403ef0239ff01eb79bfa15d2a32c :7000 myself,master - 0 0 0 connected

[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000 cluster meet 192.168.1.100 7001
OK
    ...
[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000 cluster meet 192.168.1.100 7005
OK

[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
7b953ec26bbdbf67179e5d37e3cf91626774e96f 192.168.1.100:7003 master - 0 1442466369259 4 connected
5d9f14cec1f731b6477c1e1055cecd6eff3812d4 192.168.1.100:7005 master - 0 1442466368659 4 connected
33c0bd93d7c7403ef0239ff01eb79bfa15d2a32c 192.168.1.100:7000 myself,master - 0 0 1 connected
63162ed000db9d5309e622ec319a1dcb29a3304e 192.168.1.100:7001 master - 0 1442466371262 3 connected
45baa2cb45435398ba5d559cdb574cfae4083893 192.168.1.100:7002 master - 0 1442466372264 2 connected
cdd5b3a244761023f653e08cb14721f70c399b82 192.168.1.100:7004 master - 0 1442466370261 0 connecte
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

4.2 角色設定:REPLICATE

結點全部“握手”成功後,就可以用cluster replicate命令為結點指定角色了,預設每個結點都是Master。

[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7003 cluster replicate 33c0bd93d7c7403ef0239ff01eb79bfa15d2a32c
OK
[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7004 cluster replicate 63162ed000db9d5309e622ec319a1dcb29a3304e
OK
[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7005 cluster replicate 45baa2cb45435398ba5d559cdb574cfae4083893
OK

[[email protected]8gVm redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000 cluster nodes
7b953ec26bbdbf67179e5d37e3cf91626774e96f 192.168.1.100:7003 slave 33c0bd93d7c7403ef0239ff01eb79bfa15d2a32c 0 1442466812984 4 connected
5d9f14cec1f731b6477c1e1055cecd6eff3812d4 192.168.1.100:7005 slave 45baa2cb45435398ba5d559cdb574cfae4083893 0 1442466813986 5 connected
33c0bd93d7c7403ef0239ff01eb79bfa15d2a32c 192.168.1.100:7000 myself,master - 0 0 1 connected
63162ed000db9d5309e622ec319a1dcb29a3304e 192.168.1.100:7001 master - 0 1442466814987 3 connected
45baa2cb45435398ba5d559cdb574cfae4083893 192.168.1.100:7002 master - 0 1442466811982 2 connected
cdd5b3a244761023f653e08cb14721f70c399b82 192.168.1.100:7004 slave 63162ed000db9d5309e622ec319a1dcb29a3304e 0 1442466812483 3 connected
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4.3 槽指派:ADDSLOTS

設定好主從關係之後,就可以用cluster addslots命令指派16384個槽的位置了。有點噁心的是,ADDSLOTS命令需要在引數中一個個指明槽的ID,而不能指定範圍。這裡用Bash 3.0的特性簡化了,不然就得用Bash的迴圈來完成了:

[[email protected] redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7000 cluster addslots {0..5000}
OK
[[email protected] redis-3.0.4]# src/redis-cli -c -h 192.168.1.100 -p 7001 cluster addslots {5001..10000}
OK
[[email protected] redis-3.0.4]# src/redis-cli -c -h 192.168.1.10