redis自带集群搭建

1.redis自带集群主要通过分配槽位来实现
2.到redis安装目录

//进入到utils目录下
[root@localhost redis-5.0.10]# cd utils
//在进入到create-cluster目录下
[root@localhost utils]# cd create-cluster/
//跑官方样例,执行脚本create-cluster,会根据脚本里的NODES=6
REPLICAS=1,这两句话,启动三主三从redis实例。
//下面是启动命令
[root@localhost create-cluster]# ./create-cluster  start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
//分赃
[root@localhost 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.
//接下来就可以连接客户端
[root@localhost 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> 
[root@localhost 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来连接,让它自动从定向到不同实例中去。
//上面是通过脚本自动搭建,下面开始自己写脚本搭建:
先关闭和清除上面的:
[root@localhost create-cluster]# ./create-cluster stop
Stopping 30001
Stopping 30002
Stopping 30003
Stopping 30004
Stopping 30005
Stopping 30006
[root@localhost 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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值