天天看點

redis3 cluster 叢集簡介和部署搭建

1 cluster模式簡介

redis有Redis Sentinel、Redis Cluster兩種叢集方式,從redis3.0開始才支援Redis Cluster模式,常見部署方式為3主3從。3個從redis與三個主redis一一對應,叢集内置資料自動分片機制,叢集内部将所有的key經過hash後映射到16384個Slot中,叢集中的每個Redis Instance負責其中的一部分的Slot的讀寫。從下圖中可以看到資料叢集中的資料走向。

redis3 cluster 叢集簡介和部署搭建

2 環境搭建

2.1 部署環境

由于環境有限,在同一台機器上搭建僞叢集,部署如下:

master slave
192.168.8.16:7000 192.168.8.16:7003
192.168.8.16:7001 192.168.8.16:7004
192.168.8.16:7002 192.168.8.16:7005

2.2 redis 安裝:

# wget http://download.redis.io/releases/redis-3.2.6.tar.gz

# tar xzf redis-3.2.6.tar.gz

# cd redis-3.2.6

# make

# make isntall
           

2.3 配置并啟動redis

1、在redis-3.2.6目錄下建立cluster/7000、cluster/7001、cluster/7002、cluster/7003、cluster/7004、cluster/7005目錄

2、将redis-3.2.6目錄下的redis.conf拷貝到cluster/7000目錄下,配置做如下調整:

        bind 192.168.8.16 127.0.0.1

        port 7000

        pidfile /var/run/redis_7000.pid

        appendonly yes

        cluster-enabled yes

        cluster-config-file /root/home/redis-3.2.6/cluster/7000/nodes.conf

        dir /root/home/redis-3.2.6/cluster/7000/

        cluster-node-timeout 15000

3、cluster/7001、cluster/7002、cluster/7003、cluster/7004、cluster/7005目錄的redis.conf可以參考上面的方式做調整

4、分别啟動6個執行個體:./redis-server /root/home/redis-3.2.6/cluster/7000/redis.conf

cluster 相關參數說明:

1、cluster-enabled yes  #開啟叢集

2、cluster-config-file nodes-6379.conf  #叢集配置資訊檔案,由Redis自行更新,不用手動配置。每個節點都有一個叢集配置檔案用于持久化儲存叢集資訊,需確定與運作中執行個體的配置檔案名    不沖突。

3、cluster-node-timeout 15000  #節點互連逾時時間,毫秒為機關

4、cluster-slave-validity-factor 10  #在進行故障轉移的時候全部slave都會請求申請為master,但是有些slave可能與master斷開連接配接一段時間了導緻資料過于陳舊,不應該被提升為master。該參數就是用來判斷slave節點與master斷線的時間是否過長。判斷方法是:比較slave斷開連接配接的時間和(node-timeout * slave-validity-factor)+ repl-ping-slave-period如果節點逾時時間為三十秒, 并且slave-validity-factor為10,假設預設的repl-ping-slave-period是10秒,即如果超過310秒slave将不會嘗試進行故障轉移

5、cluster-migration-barrier 1  #master的slave數量大于該值,slave才能遷移到其他孤立master上,如這個參數被設為2,那麼隻有當一個主節點擁有2個可工作的從節點時,它的一個從節點才會嘗試遷移。

6、cluster-require-full-coverage yes  #叢集全部的slot有節點負責,叢集狀态才為ok并提供服務。設定為no可以在slot沒有全部配置設定的時候提供服務。不建議打開該配置,這樣會造成分區的時候,小分區的master一直在接受寫請求,而造成很長時間資料不一緻。

7、Appendonly是一種能夠提供非常好的持久化的模式,例如使用預設的Fsync方案,Redis能在發生伺服器電源故障或作業系統仍然正常運作但Redis程序莫名挂掉的情況下,隻丢失1秒的資料

2.4 安裝ruby和rubygems

安裝ruby

[[email protected] local]# tar zxvf  ruby-2.4.0.tar.gz

[[email protected] local]# cd ruby-2.4.0

[[email protected] local]#./configure && make && make install

[[email protected] local]# ruby --version

ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]
           

安裝rubygems

[[email protected]]# tar zxvf rubygems-3.0.2.tgz

[[email protected] ]# cd rubygems-3.0.2

[[email protected] rubygems-3.0.2]# ruby setup.rb

[[email protected] rubygems-3.0.2]# gem --version

3.0.2
[[email protected] local]# gem install redis -v 4.0.0.rc1 --pre
           

2.5 建立叢集

./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

這個指令就代表把這些個redis執行個體建立為一個叢集 上面指令中的1代表主節點和從節點的比值是多少,如果12個主節點,6個從節點那麼我們的比值就是2,那麼我們是3主3從,是以這個比值是1,而且前三個一定是主節點,redis就是這樣規定的。

[[email protected] 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
>>> Creating cluster
/usr/local/lib/ruby/gems/2.4.0/gems/redis-4.0.0.rc1/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated
>>> 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
Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
M: 446ece0004f762cde28d2637ce94718b51b50cf6 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: 70806e108250977c0e0b771fad60ff126ff1b4d0 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: 76bbacde830abfc73ada8635c57cae0426bf370a 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: d881644f76e1c164d2c90eacf150020d2133503e 127.0.0.1:7003
   replicates 446ece0004f762cde28d2637ce94718b51b50cf6
S: 123250d99c8f85fb3a762fdd7e73e13e01ecf370 127.0.0.1:7004
   replicates 70806e108250977c0e0b771fad60ff126ff1b4d0
S: 57003dbce6d92a056488fbcd3b6c0590b5e7d6cd 127.0.0.1:7005
   replicates 76bbacde830abfc73ada8635c57cae0426bf370a
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:7000)
M: 446ece0004f762cde28d2637ce94718b51b50cf6 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 57003dbce6d92a056488fbcd3b6c0590b5e7d6cd 192.168.8.16:7005
   slots: (0 slots) slave
   replicates 76bbacde830abfc73ada8635c57cae0426bf370a
S: d881644f76e1c164d2c90eacf150020d2133503e 192.168.8.16:7003
   slots: (0 slots) slave
   replicates 446ece0004f762cde28d2637ce94718b51b50cf6
M: 70806e108250977c0e0b771fad60ff126ff1b4d0 192.168.8.16:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 123250d99c8f85fb3a762fdd7e73e13e01ecf370 192.168.8.16:7004
   slots: (0 slots) slave
   replicates 70806e108250977c0e0b771fad60ff126ff1b4d0
M: 76bbacde830abfc73ada8635c57cae0426bf370a 192.168.8.16:7002
   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.
           

檢視叢集資訊:

127.0.0.1:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:39
cluster_my_epoch:34
cluster_stats_messages_sent:242027
cluster_stats_messages_received:151659

127.0.0.1:7001> cluster nodes
d881644f76e1c164d2c90eacf150020d2133503e 192.168.8.16:7003 slave 446ece0004f762cde28d2637ce94718b51b50cf6 0 1551946384970 38 connected
57003dbce6d92a056488fbcd3b6c0590b5e7d6cd 192.168.8.16:7005 slave 76bbacde830abfc73ada8635c57cae0426bf370a 0 1551946386991 39 connected
123250d99c8f85fb3a762fdd7e73e13e01ecf370 192.168.8.16:7004 slave 70806e108250977c0e0b771fad60ff126ff1b4d0 0 1551946383958 34 connected
70806e108250977c0e0b771fad60ff126ff1b4d0 192.168.8.16:7001 myself,master - 0 0 34 connected 5461-10922
76bbacde830abfc73ada8635c57cae0426bf370a 192.168.8.16:7002 master - 0 1551946385978 39 connected 10923-16383
446ece0004f762cde28d2637ce94718b51b50cf6 192.168.8.16:7000 master - 0 1551946382949 38 connected 0-5460
           

使用叢集的方式登入,并作測試:

[[email protected] src]# ./redis-cli -c -p 7001
127.0.0.1:7001> set test_50key test50value
-> Redirected to slot [1727] located at 192.168.8.16:7000
OK
192.168.8.16:7000> get test_50key
"test50value"
           

  Redis Cluster 管理和測試 

 2.6 可能碰到的問題

1、[[email protected] 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

/usr/local/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- redis (LoadError)

from /usr/local/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:54:in `require'

from ./redis-trib.rb:25:in `<main>'

缺少redis庫。需要安裝 redis-4.0.0.rc1.gem

2、[[email protected] src]# gem install -l redis-4.0.0.rc1.gem

ERROR: Could not find a valid gem 'redis-4.0.0.rc1.gem' (>= 0) in any repository

[[email protected] src]# gem sources -l

*** CURRENT SOURCES ***

[[email protected] src]# gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/

Error fetching https://gems.ruby-china.org/:

SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: tlsv1 alert protocol version (https://gems.ruby-china.org/specs.4.8.gz)

如果出現上述錯誤可以使用http的源

[[email protected] src]# gem sources --add http://gems.ruby-china.com/ --remove https://rubygems.org/

http://gems.ruby-china.com/ added to sources

source https://rubygems.org/ not present in cache

[[email protected] src]# gem sources -l

*** CURRENT SOURCES ***

http://gems.ruby-china.com/

3、[[email protected] src]# gem install -l redis-4.0.0.rc1.gem

ERROR: Could not find a valid gem 'redis-4.0.0.rc1.gem' (>= 0) in any repository

需要更新一下源即可

[[email protected] src]# gem sources -u

source cache successfully updated

[[email protected] src]# gem install redis -v 4.0.0.rc1 --pre

Fetching redis-4.0.0.rc1.gem

Successfully installed redis-4.0.0.rc1

Parsing documentation for redis-4.0.0.rc1

Installing ri documentation for redis-4.0.0.rc1

Done installing documentation for redis after 2 seconds

1 gem installed

4、Waiting for the cluster to join一直等待解決辦法

[[email protected] 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

>>> Creating cluster

/usr/local/lib/ruby/gems/2.4.0/gems/redis-4.0.0.rc1/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated

>>> 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

Adding replica 127.0.0.1:7003 to 127.0.0.1:7000

Adding replica 127.0.0.1:7004 to 127.0.0.1:7001

Adding replica 127.0.0.1:7005 to 127.0.0.1:7002

M: 9b58ef401db80a4311639e021f10dafd1424a94d 127.0.0.1:7000

slots:0-5460 (5461 slots) master

M: 6b0d5f7436fe4f085313edf68cd56fa3ae9c3852 127.0.0.1:7001

slots:5461-10922 (5462 slots) master

M: 91d6a1595d78aa9719933a0e620d50bd3e723ef5 127.0.0.1:7002

slots:10923-16383 (5461 slots) master

S: 01a0e9d786980ead9d7f737c2d88cf3195c724d8 127.0.0.1:7003

replicates 9b58ef401db80a4311639e021f10dafd1424a94d

S: 9b58ef401db80a4311639e021f10dafd1424a94d 127.0.0.1:7004

replicates 6b0d5f7436fe4f085313edf68cd56fa3ae9c3852

S: 9b58ef401db80a4311639e021f10dafd1424a94d 127.0.0.1:7005

replicates 91d6a1595d78aa9719933a0e620d50bd3e723ef5

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.......................................................................................................................................................................................

需要将所有叢集中的redis.conf中的bind

由 bind 127.0.0.1 修改為 bind 192.168.8.16 127.0.0.1

繼續閱讀