1. 程式人生 > >Redis cluster 配置

Redis cluster 配置

安裝redis

tar zxvf redis-3.0.5.tar.gz
cd redis-3.0.5
make && make install

叢集配置

redis 叢集是一個提供在多個redis節點間共享資料的程式集
redis叢集不支援處理多個keys的命令,因為這需要在不同節點間移動資料,從而達不到像redis那樣的效能,在高負載情況下可能導致不可預料的錯誤
redis叢集通過分割槽來提供一定程度的可用性,在實際環境中當某個節點宕機或者不可達的
情況下繼續處理命令,redis叢集的優勢在於:
    自動分割資料到不同的節點上
    整個叢集的部分節點失敗或不可達情況下繼續處理命令

將redis-trib.rb 複製到/usr/local/bin

cd src/
cp redis-trib.rb /usr/local/bin

mkdir -p /usr/local/cluster-test
cd /usr/local/cluster-test/

新建6個節點 並將redis.conf 分別拷貝到這6個資料夾中

for i in {7001..7005};do mkdir $i &&  cp /etc/redis/6379.conf $i;done

修改成對應的埠號

for j in {7001..7005};do sed -i "s/7000/$j/g" /usr/local/cluster-test/$j/6379.conf;done

make sure that different nodes use different cluster configuration files.

for j in {7001..7005};do sed -i "s/nodes-6379.conf/nodes-$j.conf/g" /usr/local/cluster-test/$j/6379.conf;done

批量啟動

for k in {7000..7005};do redis-server ./$k/6379.conf;done

批量關閉

ps -ef|grep redis |grep -v grep|awk '{print $2}' |xargs -i kill -9 {}

檢視6個節點的啟動程序情況

ps -ef|grep redis

安裝ruby2.2以上

https://segmentfault.com/n/1330000014166676#articleHeader6

因為我們要新建叢集, 所以這裡使用create命令.

--replicas 1 引數表示為每個主節點建立一個從節點. 其他引數是例項的地址集合

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-trib.rb check 127.0.0.1:7000

>>> 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
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: 1f2a4d15b18a3215f1061ea3c710a086baa328d1 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: c3c753177d9a79eb77091c28a71d7ba0418bdaaf 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: 403fa0e59f7cb338442cf2f5f6c2d27d231dff81 127.0.0.1:7003
   replicates 1f2a4d15b18a3215f1061ea3c710a086baa328d1
S: e9657dd6211f637ea1898d7cfbe57471d3aede3c 127.0.0.1:7004
   replicates 70b04bac6e56084a1ed5cdc5a98f63154bd409ba
S: 1a6bf68a26d83d97b4c72acb417d8a0c833d8e00 127.0.0.1:7005
   replicates c3c753177d9a79eb77091c28a71d7ba0418bdaaf
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: 1f2a4d15b18a3215f1061ea3c710a086baa328d1 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: c3c753177d9a79eb77091c28a71d7ba0418bdaaf 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
M: 403fa0e59f7cb338442cf2f5f6c2d27d231dff81 127.0.0.1:7003
   slots: (0 slots) master
   replicates 1f2a4d15b18a3215f1061ea3c710a086baa328d1
M: e9657dd6211f637ea1898d7cfbe57471d3aede3c 127.0.0.1:7004
   slots: (0 slots) master
   replicates 70b04bac6e56084a1ed5cdc5a98f63154bd409ba
M: 1a6bf68a26d83d97b4c72acb417d8a0c833d8e00 127.0.0.1:7005
   slots: (0 slots) master
   replicates c3c753177d9a79eb77091c28a71d7ba0418bdaaf
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

叢集功能測試

redis cluster的特點是去中心化,每個節點都是對等的,所以,你連線哪個節點都可以獲取和設定資料

redis-cli是redis預設的客戶端工具,啟動時加上`-c`引數,就可以連線到叢集

redis-cli -c -p 7000

127.0.0.1:7000> set node 7000
-> Redirected to slot [14143] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get node
"7000"
127.0.0.1:7002> set name lyon
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK
127.0.0.1:7001> get name
"lyon"

分配key的時候,它會使用CRC16('name')%16384演算法,來計算,將這個key 放到哪個節點

資料都會在7000-7002 這3個主節點來跳轉儲存

測試叢集中的節點掛掉

redis-cli -c -p 7000

127.0.0.1:7000> set age 18
OK
127.0.0.1:7000> get age
"18"
127.0.0.1:7000> set cpy cpy
-> Redirected to slot [9692] located at 127.0.0.1:7001
OK
127.0.0.1:7001> get cpy
"eichong"

kill掉redis-7000節點

redis-trib.rb check 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: [ERR] Sorry, can't connect to node 127.0.0.1:7000

7003節點接任

redis-trib.rb check 127.0.0.1:7001
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 Cluster Check (using node 127.0.0.1:7001)
M: 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: c3c753177d9a79eb77091c28a71d7ba0418bdaaf 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 403fa0e59f7cb338442cf2f5f6c2d27d231dff81 127.0.0.1:7003
   slots:0-5460 (5461 slots) master
   0 additional replica(s)
S: e9657dd6211f637ea1898d7cfbe57471d3aede3c 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 70b04bac6e56084a1ed5cdc5a98f63154bd409ba
S: 1a6bf68a26d83d97b4c72acb417d8a0c833d8e00 127.0.0.1:7005
   slots: (0 slots) slave
   replicates c3c753177d9a79eb77091c28a71d7ba0418bdaaf
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
[WARNING] Node 127.0.0.1:7002 has slots in importing state (5798,10576).
[WARNING] The following slots are open: 5798,10576
>>> Check slots coverage...
[OK] All 16384 slots covered.

測試是否可以獲取到資料

redis-cli -c -p 7001

127.0.0.1:7001> get age
-> Redirected to slot [741] located at 127.0.0.1:7003
"18"
127.0.0.1:7003> get name
-> Redirected to slot [5798] located at 127.0.0.1:7001
"www"
127.0.0.1:7001> get cpy
"cpy"

重啟7000節點

[[email protected] ~ ]$ redis-trib.rb check 127.0.0.1:7000
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7005: OK
Connecting to node 127.0.0.1:7002: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7003: OK
>>> Performing Cluster Check (using node 127.0.0.1:7000)
S: 1f2a4d15b18a3215f1061ea3c710a086baa328d1 127.0.0.1:7000
   slots: (0 slots) slave
   replicates 403fa0e59f7cb338442cf2f5f6c2d27d231dff81
S: e9657dd6211f637ea1898d7cfbe57471d3aede3c 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 70b04bac6e56084a1ed5cdc5a98f63154bd409ba
S: 1a6bf68a26d83d97b4c72acb417d8a0c833d8e00 127.0.0.1:7005
   slots: (0 slots) slave
   replicates c3c753177d9a79eb77091c28a71d7ba0418bdaaf
M: c3c753177d9a79eb77091c28a71d7ba0418bdaaf 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 403fa0e59f7cb338442cf2f5f6c2d27d231dff81 127.0.0.1:7003
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
[WARNING] Node 127.0.0.1:7002 has slots in importing state (5798,10576).
[WARNING] The following slots are open: 5798,10576
>>> Check slots coverage...
[OK] All 16384 slots covered

可以看到節點狀態為啟用態,但是作為7003的從節點

叢集中新加入節點

分2種情況,1是作為主節點,2是作為一個節點的從節點

1 新建一個 7006 節點 作為一個新的主節點加入
cd /usr/local/cluster-test/
mkdir 7006
cp 7005/6379.conf 7006/6379.conf
sed -i "s/7005/7006/g" /usr/local/cluster-test/7006/6379.conf

重啟叢集節點
for k in {7000..7006};do redis-server ./$k/6379.conf;done
新增7006節點
redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000
檢查7006節點
redis-trib.rb check 127.0.0.1:7006
Connecting to node 127.0.0.1:7006: OK
Connecting to node 127.0.0.1:7004: OK
Connecting to node 127.0.0.1:7002: OK
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:7005: OK
Connecting to node 127.0.0.1:7003: OK
>>> Performing Cluster Check (using node 127.0.0.1:7006)
S: 173f43306bcfac4319a9556e2d49f246880cbbf4 127.0.0.1:7006
   slots: (0 slots) slave
   replicates 70b04bac6e56084a1ed5cdc5a98f63154bd409ba
S: e9657dd6211f637ea1898d7cfbe57471d3aede3c 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 70b04bac6e56084a1ed5cdc5a98f63154bd409ba
M: c3c753177d9a79eb77091c28a71d7ba0418bdaaf 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 1f2a4d15b18a3215f1061ea3c710a086baa328d1 127.0.0.1:7000
   slots: (0 slots) slave
   replicates 403fa0e59f7cb338442cf2f5f6c2d27d231dff81
M: 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   2 additional replica(s)
S: 1a6bf68a26d83d97b4c72acb417d8a0c833d8e00 127.0.0.1:7005
   slots: (0 slots) slave
   replicates c3c753177d9a79eb77091c28a71d7ba0418bdaaf
M: 403fa0e59f7cb338442cf2f5f6c2d27d231dff81 127.0.0.1:7003
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
[WARNING] Node 127.0.0.1:7002 has slots in importing state (5798,10576).
[WARNING] The following slots are open: 5798,10576
>>> Check slots coverage...
[OK] All 16384 slots covered.

redis-cli -c -p 7006
127.0.0.1:7006> cluster nodes
e9657dd6211f637ea1898d7cfbe57471d3aede3c 127.0.0.1:7004 slave 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 0 1537170999876 2 connected
c3c753177d9a79eb77091c28a71d7ba0418bdaaf 127.0.0.1:7002 master - 0 1537170999366 3 connected 10923-16383
1f2a4d15b18a3215f1061ea3c710a086baa328d1 127.0.0.1:7000 slave 403fa0e59f7cb338442cf2f5f6c2d27d231dff81 0 1537170998346 7 connected
70b04bac6e56084a1ed5cdc5a98f63154bd409ba 127.0.0.1:7001 master - 0 1537170997836 2 connected 5461-10922
1a6bf68a26d83d97b4c72acb417d8a0c833d8e00 127.0.0.1:7005 slave c3c753177d9a79eb77091c28a71d7ba0418bdaaf 0 1537170998855 3 connected
173f43306bcfac4319a9556e2d49f246880cbbf4 127.0.0.1:7006 myself,slave 70b04bac6e56084a1ed5cdc5a98f63154bd409ba 0 0 0 connected
403fa0e59f7cb338442cf2f5f6c2d27d231dff81 127.0.0.1:7003 master - 0 1537170999884 7 connected 0-5460

可以看到7006成為了從節點

參考文件