redis集群节点新增、删除、重新分配slot实战

1、搭建好 Redis 集群
参考我的博文 http://blog.csdn .NET /nuli888/article/details/52134117
下面操作都在同台机子server1 192.168.1.198上操作

2、查看现有节点
[root@localhost ~]# cd /usr/local/redis-3.0.6/src/
[root@localhost src]# redis-cli -c -p 7000 cluster nodes
6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004 slave 26101db06b5c2d4431ca8308cf43d51f6939b4fc 0 1470480300217 7 connected
ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005 slave 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 0 1470480302228 9 connected
e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001 master - 0 1470480302226 8 connected 5000-5460
02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003 slave e60f49920cf8620927b200b0001892d08067d065 0 1470480301220 8 connected
2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000 myself,master - 0 0 9 connected 0-4999 5461-15922
26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002 master - 0 1470480300217 7 connected 15923-16383

3、增加主节点192.168.1.198:7006
[root@localhost cluster]# cd /usr/local/cluster/
[root@localhost cluster]# mkdir 7006
#复制redis配置文件并把端口改成mkdir 7006
[root@localhost cluster]# cp redis.conf  7006/redis.conf && sed -i "s/7000/7006/g" 7006/redis.conf
[root@localhost cluster]# cd 7006
#启动redis
[root@localhost 7006]# redis-server redis.conf
[root@localhost 7006]# ps -ef | grep redis
root      2741     1  0 09:39 ?        00:03:09 redis-server *:7000 [cluster]
root      2747     1  0 09:40 ?        00:03:08 redis-server *:7001 [cluster]
root      2751     1  0 09:40 ?        00:03:07 redis-server *:7002 [cluster]
root      3482     1  0 18:11 ?        00:00:00 redis-server *:7006 [cluster]
root      3486  2687  0 18:11 pts/0    00:00:00 grep redis
[root@localhost 7006]# cd /usr/local/redis-3.0.6/src/
#增加主节点操作192.168.1.198:7000是任意的旧节点
[root@localhost src]# ./redis-trib.rb add-node 192.168.1.198:7006 192.168.1.198:7000
>>> Adding node 192.168.1.198:7006 to cluster 192.168.1.198:7000
>>> Performing Cluster Check (using node 192.168.1.198:7000)
M: 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
   slots: (0 slots) slave
   replicates 26101db06b5c2d4431ca8308cf43d51f6939b4fc
S: ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
   slots: (0 slots) slave
   replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
M: e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
   slots: (0 slots) slave
   replicates e60f49920cf8620927b200b0001892d08067d065
M: 26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198: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.
>>> Send CLUSTER MEET to node 192.168.1.198:7006 to make it join the cluster.
[OK] New node added correctly.
#查看集群节点情况
[root@localhost src]# redis-cli -c -p 7000 cluster nodes
6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004 slave 26101db06b5c2d4431ca8308cf43d51f6939b4fc 0 1470478484964 7 connected
ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005 slave 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 0 1470478486472 6 connected
e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001 master - 0 1470478484961 8 connected 0-5460
02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003 slave e60f49920cf8620927b200b0001892d08067d065 0 1470478485466 8 connected
4458ef594b310e61bf5f249e8efd707d0da880b0 192.168.1.198:7006 master - 0 1470478485968 0 connected
2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000 myself,master - 0 0 1 connected 5461-10922
26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002 master - 0 1470478485968 7 connected 10923-16383

4、增加从节点192.168.1.198:7007
[root@localhost cluster]# cd /usr/local/cluster/
[root@localhost cluster]# mkdir 7007
[root@localhost cluster]# cp redis.conf  7007/redis.conf && sed -i "s/7000/7007/g" 7007/redis.conf
[root@localhost cluster]# cd 7007
[root@localhost 7007]# redis-server redis.conf
[root@localhost 7007]# ps -ef | grep redis
root      2741     1  0 09:39 ?        00:03:11 redis-server *:7000 [cluster]
root      2747     1  0 09:40 ?        00:03:10 redis-server *:7001 [cluster]
root      2751     1  0 09:40 ?        00:03:09 redis-server *:7002 [cluster]
root      3482     1  0 18:11 ?        00:00:01 redis-server *:7006 [cluster]
root      3495     1  0 18:17 ?        00:00:00 redis-server *:7007 [cluster]
root      3499  2687  0 18:17 pts/0    00:00:00 grep redis
[root@localhost 7007]# cd /usr/local/redis-3.0.6/src/
#--slave,表示添加的是从节点
#--master-id 2f70e9f2b4a06a846e46d7034a54e0fe6971beea,主节点的node id,在这里是7000的node id
#192.168.1.198:7007,新节点
#192.168.1.198:7000集群任一个旧节点
[root@localhost src]# ./redis-trib.rb add-node --slave --master-id 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7007 192.168.1.198:7000
>>> Adding node 192.168.1.198:7007 to cluster 192.168.1.198:7000
>>> Performing Cluster Check (using node 192.168.1.198:7000)
M: 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
   slots:0-4999,5461-15922 (15462 slots) master
   1 additional replica(s)
S: 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
   slots: (0 slots) slave
   replicates 26101db06b5c2d4431ca8308cf43d51f6939b4fc
S: ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
   slots: (0 slots) slave
   replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
M: e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
   slots:5000-5460 (461 slots) master
   1 additional replica(s)
S: 02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
   slots: (0 slots) slave
   replicates e60f49920cf8620927b200b0001892d08067d065
M: 4458ef594b310e61bf5f249e8efd707d0da880b0 192.168.1.198:7006
   slots: (0 slots) master
   0 additional replica(s)
M: 26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002
   slots:15923-16383 (461 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.
>>> Send CLUSTER MEET to node 192.168.1.198:7007 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.1.198:7000.
[OK] New node added correctly.
#查看集群节点
[root@localhost src]# redis-cli -c -p 7000 cluster nodes
6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004 slave 26101db06b5c2d4431ca8308cf43d51f6939b4fc 0 1470483081269 7 connected
ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005 slave 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 0 1470483079858 9 connected
e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001 master - 0 1470483080765 8 connected 5000-5460
9e5422dac0c715ef6d81bf82994fe909f97ccbff 192.168.1.198:7007 slave 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 0 1470483081771 9 connected
02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003 slave e60f49920cf8620927b200b0001892d08067d065 0 1470483080261 8 connected
2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000 myself,master - 0 0 9 connected 0-4999 5461-15922
4458ef594b310e61bf5f249e8efd707d0da880b0 192.168.1.198:7006 master - 0 1470483080765 0 connected
26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002 master - 0 1470483080260 7 connected 15923-16383

5、对主节点重新分配slot
新增加的主节点,是没有slots的,主节点如果没有slots的话,存取数据就都不会被选中
可以把分配的过程理解成打扑克牌,all表示大家重新洗牌;输入某个主节点的node id,然后在输入done的话,就好比从某个节点,抽牌
[root@localhost src]# ./redis-trib.rb reshard 192.168.1.198:7006
>>> Performing Cluster Check (using node 192.168.1.198:7006)
M: 4458ef594b310e61bf5f249e8efd707d0da880b0 192.168.1.198:7006
   slots:0-943,5000-5027,15923-15950 (1000 slots) master
   0 additional replica(s)
S: 9e5422dac0c715ef6d81bf82994fe909f97ccbff 192.168.1.198:7007
   slots: (0 slots) slave
   replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
S: ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
   slots: (0 slots) slave
   replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
S: 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
   slots: (0 slots) slave
   replicates 26101db06b5c2d4431ca8308cf43d51f6939b4fc
M: 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
   slots:944-4999,5461-15922 (14518 slots) master
   2 additional replica(s)
M: 26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002
   slots:15951-16383 (433 slots) master
   1 additional replica(s)
S: 02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
   slots: (0 slots) slave
   replicates e60f49920cf8620927b200b0001892d08067d065
M: e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
   slots:5028-5460 (433 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.
How many slots do you want to move (from 1 to 16384)? 1000
What is the receiving node ID? 4458ef594b310e61bf5f249e8efd707d0da880b0
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:all
#输入all 表示从所有的主节点中随机转移,凑够1000个哈希槽
......
Do you want to proceed with the proposed reshard plan (yes/no)? yes 
#输入yes,redis集群就开始分配哈希槽了
......

6、删除节点
1)192.168.1.198:7007 节点ip和端口
2)9e5422dac0c715ef6d81bf82994fe909f97ccbff 节点的id值
[root@localhost src]# ./redis-trib.rb del-node 192.168.1.198:7007 '9e5422dac0c715ef6d81bf82994fe909f97ccbff'
>>> Removing node 9e5422dac0c715ef6d81bf82994fe909f97ccbff from cluster 192.168.1.198:7007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Redis Cluster 是 Redis 的分布式解决方案,可以将数据分散存储在不同的节点上,以提高性能和可扩展性。在 Redis Cluster 中,每个节点负责一部分槽位(slot)的存储和处理。 要新增一个节点,并进行槽位的分配,我们需要执行以下步骤: 1. 启动一个新的 Redis 节点,配置文件中要指定集群模式,并确保其他节点也都开启了集群模式。 2. 在一个已经运行的节点上执行 CLUSTER MEET 命令,将新节点加入到集群中。命令格式为 CLUSTER MEET <ip> <port>,其中 <ip> 和 <port> 分别是新节点的 IP 地址和端口号。 3. 新节点加入集群后,其他节点会自动感知到这个变化,它们会进行重新的槽位分配。每个节点会负责同时管理一些旧槽位和新槽位。 4. 为了保持集群的平衡,可以使用 CLUSTER REBALANCE 命令来手动执行槽位的重新分配。这个命令会将不均匀分布的槽位重新分配节点,使得集群中每个节点管理的槽位数尽量接近。 要删除一个节点,并重新分配其槽位,我们需要执行以下步骤: 1. 首先,将要删除节点设置为故障状态。可以使用 CLUSTER FORGET 命令从其他节点中移除它。命令格式为 CLUSTER FORGET <node_id>,其中 <node_id> 是要删除节点的 ID。 2. 接下来,进行槽位的重新分配Redis Cluster 会自动进行槽位的再平衡。每个存活的节点都会参与槽位的重新分配,以保持集群的平衡。 需要注意的是,新增删除节点时,Redis Cluster 会自动进行槽位的重新分配,不需要手动操作。在节点加入或离开集群后,其他存活节点会按照一定的规则重新分配槽位,以确保数据的平衡和高可用性。如果发生数据迁移,Redis Cluster 会自动处理数据的同步和恢复。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值