天天看點

手把手redis叢集的搭建

一:redis-cluster架構圖

手把手redis叢集的搭建

架構細節:

(1)所有的redis節點彼此互聯(PING-PONG機制),内部使用二進制協定優化傳輸速度和帶寬.

(2)節點的fail是通過叢集中超過半數的節點檢測失效時才生效.

(3)用戶端與redis節點直連,不需要中間proxy層.用戶端不需要連接配接叢集所有節點,連接配接叢集中任何一個可用節點即可

(4)redis-cluster把所有的實體節點映射到[0-16383]slot上,cluster 負責維護node<->slot<->value

Redis 叢集中内置了 16384 個哈希槽,當需要在 Redis 叢集中放置一個 key-value 時,redis 先對 key 使用 crc16 算法算出一個結果,然後把結果對 16384 求餘數,這樣每個 key 都會對應一個編号在 0-16383 之間的哈希槽,redis 會根據節點數量大緻均等的将哈希槽映射到不同的節點

二:Redis叢集的搭建

2.1. 叢集搭建環境

、使用ruby腳本搭建叢集。需要ruby的運作環境。
安裝ruby
yum install ruby
yum install rubygems

、安裝ruby腳本運作使用的包。
[[email protected] ~]# gem install redis-3.0.0.gem 
Successfully installed redis-
 gem installed
Installing ri documentation for redis-...
Installing RDoc documentation for redis-...
[[email protected] ~]# 

[[email protected] ~]# cd redis-3.0.0/src
[[email protected] src]# ll *.rb
-rwxrwxr-x.  root root  Apr     redis-trib.rb
           

2.2. 搭建步驟

需要6台redis伺服器。搭建僞分布式。

需要6個redis執行個體。

需要運作在不同的端口7001-7006

第一步:建立6個redis執行個體,每個執行個體運作在不同的端口。需要修改redis.conf配置檔案。配置檔案中還需要把cluster-enabled yes前的注釋去掉。

手把手redis叢集的搭建

第二步:啟動每個redis執行個體。

第三步:使用ruby腳本搭建叢集。

建立關閉叢集的腳本:
[[email protected] redis-cluster]# vim shutdow-all.sh
redis01/redis-cli -p 7001 shutdown
redis01/redis-cli -p 7002 shutdown
redis01/redis-cli -p 7003 shutdown
redis01/redis-cli -p 7004 shutdown
redis01/redis-cli -p 7005 shutdown
redis01/redis-cli -p 7006 shutdown
[[email protected] redis-cluster]# chmod u+x shutdow-all.sh 

[[email protected] redis-cluster]# ./redis-trib.rb create --replicas  : : : : :  :
>>> Creating cluster
Connecting to node :: OK
Connecting to node :: OK
Connecting to node :: OK
Connecting to node :: OK
Connecting to node :: OK
Connecting to node :: OK
>>> Performing hash slots allocation on  nodes...
Using  masters:
:
:
:
Adding replica : to :
Adding replica : to :
Adding replica : to :
M: ae301e9c32b04a7d4d92e15e98e78de8c1f3 :
   slots:- ( slots) master
M: cd93a9a943b4ef851af6a03edd699a6061ace01 :
   slots:- ( slots) master
M: d83f20b1253d7f43dae32aab9744e6 :
   slots:- ( slots) master
S: f9d9706f848471583929fc8bbde3c8e99e211b :
   replicates ae301e9c32b04a7d4d92e15e98e78de8c1f3
S: cc9e25ebb19dda92591364c1df4b3a518b795b :
   replicates cd93a9a943b4ef851af6a03edd699a6061ace01
S: b1b11d509d29659c2831e7a9f6469c060dfcd39 :
   replicates d83f20b1253d7f43dae32aab9744e6
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 :)
M: ae301e9c32b04a7d4d92e15e98e78de8c1f3 :
   slots:- ( slots) master
M: cd93a9a943b4ef851af6a03edd699a6061ace01 :
   slots:- ( slots) master
M: d83f20b1253d7f43dae32aab9744e6 :
   slots:- ( slots) master
M: f9d9706f848471583929fc8bbde3c8e99e211b :
   slots: ( slots) master
   replicates ae301e9c32b04a7d4d92e15e98e78de8c1f3
M: cc9e25ebb19dda92591364c1df4b3a518b795b :
   slots: ( slots) master
   replicates cd93a9a943b4ef851af6a03edd699a6061ace01
M: b1b11d509d29659c2831e7a9f6469c060dfcd39 :
   slots: ( slots) master
   replicates d83f20b1253d7f43dae32aab9744e6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All  slots covered.
[[email protected] redis-cluster]# 

           

繼續閱讀