Redis集羣使用總結1

Redis集羣使用總結(一):

隨着企業數據量的增多,redis不論作爲數據存儲或是緩存,它的數據量也會逐漸增多,雖然Redis的速度非常可觀,但隨着其中的數據量的龐大,並且僅僅在一個設備或是一個Redis實例中,其存取速度也會大打折扣,所以我們需要在不同的設備或服務器上,搭建多個Redis實例倉庫,將原來的Redis的所有的keys分發到各個服務器的Redis上,這就是現在所謂的Redis集羣(Redis Cluster)。

 

·     原理

·     實施

·     注意

·     問題

 

一、原理

1、數據共享

 

Redis提供多個節點實例間的數據共享,也就是Redis A,B,C,D彼此之間的數據是同步的,同樣彼此之間也可以通信,而對於客戶端操作的keys是由Redis系統自行分配到各個節點中。

 

2、主從複製

 

Redis的多個實例間通信時,一旦其中的一個節點故障,那麼Redis集羣就不能繼續正常工作,所以需要一種複製機制(Master-Slave)機制,做到一旦節點A故障了,那麼其從節點A1和A2就可以接管並繼續提供與A同樣的工作服務,當然如果節點A,A1,A2節點都出現問題,那麼同樣這個集羣不會繼續保持工作,但是這種情況比較罕見,即使出現了,也會及時發現並修復使用。

建議:部署主從複製機制(Master-Slave)。

 

3、哈希槽值

 

Redis集羣中使用哈希槽來存儲客戶端的keys,而在Redis中,目前存在16384個哈希槽,它們被全部分配給所有的節點,正如上圖所示,所有的哈希槽值被節點A,B,C分配完成了。

 

二、實施

1、創建集羣

A、輔助工具安裝

在搭建Redis Cluster之前,請先確保系統已經安裝了zlib和ruby(含rubygems)軟件依賴包,請自行安裝。

 

B、安裝redis-cluster

在Redis的源碼路徑下,位於src目錄中的redis-trib.rb文件是用來搭建和維護集羣的工具,我們需要將其放入與redis-server和redis-cli同樣的指令環境下,如下:

sudo cp /redis/redis-3.0.7/src/redis-trib.rb  /redis/bin

sudo cp /redis/redis-3.0.7/src/redis-server  /redis/bin

sudo cp /redis/redis-3.0.7/src/redis-cli  /redis/bin

 

C、配置redis-cluster

文件結構:

 

通用配置:

#generate configs

daemonize no

tcp-backlog 511

timeout 2000

tcp-keepalive 0

loglevel notice

databases 16

slave-serve-stale-data yes

slave-read-only yes

repl-disable-tcp-nodelay yes

slave-priority 100

#open the aof persistence

appendonly yes

#config aof mode for everysec

appendfsync everysec

#while rewrite op then close the aof write mode

no-appendfsync-on-rewrite yes

auto-aof-rewrite-min-size 64mb

aof-rewrite-incremental-fsync yes

#limit the time of lua scripts executes

lua-time-limit 5000

#open redis cluster switch

cluster-enabled yes

#timeout of nodes connections

cluster-node-timeout 15000

cluster-migration-barrier 1

slowlog-log-slower-than 10000

slowlog-max-len 128

notify-keyspace-events “”

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-entries 512

list-max-ziplist-value 64

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

#open the online rehash

activerehashing yes

 

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb 60

client-output-buffer-limit pubsub 32mb 8mb 60

 

hz 10

 

特殊配置:

這裏以6379端口爲例說明,其它的特殊配置只需修改對應的端口即可:

#common configs

include /redis/etc/redis-common.conf

#listen tcp port

port 6379

#memory cahce max size

maxmemory 100m

maxmemory-policy allkeys-lru

#aof filename

appendfilename “appendonly-6379.aof”

#rdb file,only use in the slave handle

dbfilename dump-6379.rdb

dir /redis/db-6379

#cluster config that auto create

cluster-config-file nodes-6379.conf

#log path

logfile /redis/logs/6379/log-6379.log

 

#in the same computer redis,then give the limit

#fork all redis processes done rewrite,using big memory

auto-aof-rewrite-percentage 80-100

 

D、搭建redis-cluster

在這裏,我們使用Redis自帶的ruby工具redis-trib.rb來創建和管理集羣。本人共創建了6個redis節點,分別啓動之後,使用./redis-trib.rb create –replicas配置和生成3個主節點和對應每個節點的3個從節點。

首先,啓動6個節點實例:

$redis-server /redis/etc/redis-6379.conf

$redis-server  /redis/etc/redis-6380.conf

$redis-server  /redis/etc/redis-6381.conf

$redis-server  /redis/etc/redis-7379.conf

$redis-server  /redis/etc/redis-7380.conf

$redis-server  /redis/etc/redis-7381.conf

 

其次,創建分配主從節點:

$redis-trib.rb create –replicas 1

127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:7379 127.0.0.1:7380127.0.0.1:7381

 

執行的結果:

 

NOTE:

–replicas 1代表創建1個從節點,創建的從節點的順序是按照6379-7379,6380-7380及6381-7381的順序創建配置的。

 

從上圖知道,已經創建和配置了3個Master和對應的3個Sub子節點,同時會提示您是否同意這個配置生成,當你回覆yes之後,顯示如下:

 

NOTE:

從上圖知道,Redis集羣已經創建和配置成功了,並且redis的16384個哈希槽已經全部分配完成。

 

2、集羣驗證

驗證可以使用redis-rb-cluster或是redis-cli來驗證,這裏以最簡單的方式redis-cli來驗證使用,意在說明集羣的使用和校驗。

A、哈希槽分配

$redis-cli -c -p 6379

127.0.0.1:6379> set mykey “hello”

-> Redirected to slot [14687] located at 127.0.0.1:6381

 

$redis-cli -c -p 6380

127.0.0.1:6380> set mykey2 “hello world”

-> Redirected to slot [14119] located at 127.0.0.1:6381

OK

 

$redis-cli -c -p 6381

127.0.0.1:6381> set mykey3 “hello”

-> Redirected to slot [9990] located at 127.0.0.1:6380

OK

 

B、集羣驗證

$redis-trib.rb  127.0.0.1:7379

 

結果:

 

右上圖,可以知道集羣配置沒問題,繼續往下驗證下數據的異步同步。

 

C、集羣數據共享

$redis-cli -c -p 6379

127.0.0.1:6379> set mykey “hello”

-> Redirected to slot [14687] located at 127.0.0.1:6381

OK

127.0.0.1:6381> get mykey2

“hello world”

127.0.0.1:6381> get mykey3

-> Redirected to slot [9990] located at 127.0.0.1:6380

“hello”

 

NOTE:

從上面可以看出,我實例子6379中可以訪問同一個集羣內的節點數據,訪問的機制是根據set時分配的哈希槽,例如:在6379中,使用get mykey3,那麼自動定位到6380。  

 

三、注意

1、主從複製

Redis集羣支持主從複製功能,也就是主節點對應的從節點,但是不需要在從節點中加入slaveof <server>:<port>,否則會報錯哦。

 

2、主從配置

一般情況下,從節點的配置和對應的主節點的配置類似,但是一般從節點的大小要小於主節點的配置大小,這主要考慮內存和性能均衡方面,請在實際使用時留意下。

 

3、實例通信

Redis集羣中的節點實例間的數據共享機制是通過定位哈希槽(set時的鍵值分配的哈希),不會區分主從節點或是普通節點的通信。

 

四、問題

遇到問題:

custom_require.rb:36:in `require’: cannot load such file – redis(LoadError)

from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’

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

解決辦法:

sudo gem install redis來安裝ruby和redis的接口包即可

 

 

 

 

 

 

 

 

技術討論羣:

489451956(新)

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章