redis自帶叢集搭建
阿新 • • 發佈:2021-01-17
技術標籤:redis
1.redis自帶叢集主要通過分配槽位來實現
2.到redis安裝目錄
//進入到utils目錄下
[[email protected] redis-5.0.10]# cd utils
//在進入到create-cluster目錄下
[[email protected] utils]# cd create-cluster/
//跑官方樣例,執行指令碼create-cluster,會根據腳本里的NODES=6
REPLICAS=1,這兩句話,啟動三主三從redis例項。
//下面是啟動命令
[[email protected] create-cluster]# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
//分贓
[[email protected] create-cluster]# ./create-cluster create
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
>> > Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: cef1a277fb22f360038cee6a36d76df8d260807f 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
M: dcf8d50c0b6678d5960a3b1c4cad8f649fb34309 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
M: 360a7a729d8125507ee896a3b97694d7eeed872a 127.0.0.1:30003
slots:[ 10923-16383] (5461 slots) master
S: 8eb3050ff31cff6484dddc060c4277fcd7c8ce83 127.0.0.1:30004
replicates dcf8d50c0b6678d5960a3b1c4cad8f649fb34309
S: 24d632a933146e2dcda367258da55518ab65a43b 127.0.0.1:30005
replicates 360a7a729d8125507ee896a3b97694d7eeed872a
S: 84b8313d6d6c503001e480facb0b874018f6b28c 127.0.0.1:30006
replicates cef1a277fb22f360038cee6a36d76df8d260807f
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:30001)
M: cef1a277fb22f360038cee6a36d76df8d260807f 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 360a7a729d8125507ee896a3b97694d7eeed872a 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 8eb3050ff31cff6484dddc060c4277fcd7c8ce83 127.0.0.1:30004
slots: (0 slots) slave
replicates dcf8d50c0b6678d5960a3b1c4cad8f649fb34309
S: 84b8313d6d6c503001e480facb0b874018f6b28c 127.0.0.1:30006
slots: (0 slots) slave
replicates cef1a277fb22f360038cee6a36d76df8d260807f
M: dcf8d50c0b6678d5960a3b1c4cad8f649fb34309 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 24d632a933146e2dcda367258da55518ab65a43b 127.0.0.1:30005
slots: (0 slots) slave
replicates 360a7a729d8125507ee896a3b97694d7eeed872a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
//接下來就可以連線客戶端
[[email protected] create-cluster]# redis-cli -p 30001
127.0.0.1:30001> set k2 1
OK
127.0.0.1:30001> set 4 5
(error) MOVED 14039 127.0.0.1:30003
127.0.0.1:30001> set k1 55
(error) MOVED 12706 127.0.0.1:30003
127.0.0.1:30001> set k2 4
OK
127.0.0.1:30001>
[[email protected] create-cluster]# redis-cli -c -p 30001
127.0.0.1:30001> set 3 5
OK
127.0.0.1:30001> set k2 4
OK
127.0.0.1:30001> set k4 66
-> Redirected to slot [8455] located at 127.0.0.1:30002
OK
127.0.0.1:30002>
//注意因為我們要連線的是叢集,所以我們要使用-c來連線,讓它自動從定向到不同例項中去。
//上面是通過指令碼自動搭建,下面開始自己寫指令碼搭建:
先關閉和清除上面的:
[[email protected] create-cluster]# ./create-cluster stop
Stopping 30001
Stopping 30002
Stopping 30003
Stopping 30004
Stopping 30005
Stopping 30006
[[email protected] create-cluster]# ./create-cluster clean
3.自己寫指令碼啟動
[root@localhost create-cluster]# ./create-cluster start
[root@localhost create-cluster]# redis-cli --cluster create 127.0.0.1:30001 127.0.0.1:30002 127.0.0.1:30003 127.0.0.1:30004 127.0.0.1:30005 127.0.0.1:30006 --cluster-replicas 1
[root@localhost create-cluster]# redis-cli -c -p 30001
//下面就是redis遷移資料
[root@localhost create-cluster]# redis-cli --cluster reshard 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 612033cd82930567ac143ce0e03fa3af11258432 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 209a1c0f8da644e992d19963cd85d872d30aecb2 127.0.0.1:30006
slots: (0 slots) slave
replicates 612033cd82930567ac143ce0e03fa3af11258432
S: eb416e21547cad9a4a749c2623b6857d4d1cdc13 127.0.0.1:30004
slots: (0 slots) slave
replicates e4ebe4f25152a865306b568c93724c87f57ae92f
M: 4a322e48e3a6a44e48de4755c6a318713c486b9e 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: e4ebe4f25152a865306b568c93724c87f57ae92f 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 0706ee34fe1e6bd678b16927f1974d1ed16d3c39 127.0.0.1:30005
slots: (0 slots) slave
replicates 4a322e48e3a6a44e48de4755c6a318713c486b9e
[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)? ^[[4~^[[2~^[[2~^Hstty erase ^H
How many slots do you want to move (from 1 to 16384)? 200
What is the receiving node ID? 209a1c0f8da644e992d19963cd85d872d30aecb2
*** The specified node (209a1c0f8da644e992d19963cd85d872d30aecb2) is not known or not a master, please retry.
[root@localhost create-cluster]# 612033cd82930567ac143ce0e03fa3af11258432
bash: 612033cd82930567ac143ce0e03fa3af11258432: 未找到命令...
[root@localhost create-cluster]# redis-cli --cluster reshard 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 612033cd82930567ac143ce0e03fa3af11258432 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 209a1c0f8da644e992d19963cd85d872d30aecb2 127.0.0.1:30006
slots: (0 slots) slave
replicates 612033cd82930567ac143ce0e03fa3af11258432
S: eb416e21547cad9a4a749c2623b6857d4d1cdc13 127.0.0.1:30004
slots: (0 slots) slave
replicates e4ebe4f25152a865306b568c93724c87f57ae92f
M: 4a322e48e3a6a44e48de4755c6a318713c486b9e 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: e4ebe4f25152a865306b568c93724c87f57ae92f 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 0706ee34fe1e6bd678b16927f1974d1ed16d3c39 127.0.0.1:30005
slots: (0 slots) slave
replicates 4a322e48e3a6a44e48de4755c6a318713c486b9e
[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)? 200
What is the receiving node ID? 612033cd82930567ac143ce0e03fa3af11258432
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: 4a322e48e3a6a44e48de4755c6a318713c486b9e
Source node #2: done