redis搭建集群 - 增加节点

集群扩容- 增加节点

新增redis服务:(:1,$s/6381/8382/g  替换)
    192.168.2.111:6381
    192.168.2.111:6382
    增加节点必须是一对,一个主节点一个从节点(手动分配6382是6381的从服务),注意对外开放对应的端口和监控端口(10000+端口)

一:主节点

1:把节点添加到集群中
[root@192 redis6382]$ redis-cli --cluster add-node 192.168.2.111:6381 192.168.2.111:6379
>>> Adding node 192.168.2.111:6381 to cluster 192.168.2.111:6379
>>> Performing Cluster Check (using node 192.168.2.111:6379)
M: 63cfbaf1e7d7d9fce095d0f485776ba15404bf8a 192.168.2.111:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 75b477ef4967c1bf50502cb834df3fd5660cfa22 192.168.2.112:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: ebc80d8dcabe30645b96fdfe08de84f52a42cbff 192.168.2.113:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: e6d09dcaa252336190e4e8747a60d067572d5156 192.168.2.112:6380
   slots: (0 slots) slave
   replicates ebc80d8dcabe30645b96fdfe08de84f52a42cbff
S: f3dbdddc6c2dc23e1dde2e6e6662c6c5ebf167e9 192.168.2.111:6380
   slots: (0 slots) slave
   replicates 75b477ef4967c1bf50502cb834df3fd5660cfa22
S: 5da5d0d7f7036c971926a821b33764fb2c78ea50 192.168.2.113:6380
   slots: (0 slots) slave
   replicates 63cfbaf1e7d7d9fce095d0f485776ba15404bf8a
[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.2.111:6381 to make it join the cluster.
[OK] New node added correctly.

命令解释:
redis-cli --cluster add-node new_host:new_port existing_host:existing_port
    -> new_host:new_port: 新的ip: 新添加的端口号
    -> existing_host:existing_port: 集群中任意ip和对应端口号
    

2:查看添加的节点在集群中的状态
[root@192 redis]$ redis-cli -h 192.168.2.111 -p 6381 -c
192.168.2.111:6381> cluster nodes
    4ca16d842ea07379be11371aa2381529a9b5d7bc 192.168.2.111:6381@16381 myself,master - 0 1555496064000 0 connected
结果解释:6381节点是一个master节点, connected为空说明没有分配哈希槽,不能存储数据

3:为新增的master节点分配哈希槽 -  分片操作
redis-cli --cluster reshard 需要增加哈希槽的ip加端口

[root@192 redis6382]# redis-cli --cluster reshard 192.168.2.111:6381
>>> Performing Cluster Check (using node 192.168.2.111:6381)
M: 4ca16d842ea07379be11371aa2381529a9b5d7bc 192.168.2.111:6381
   slots: (0 slots) master
S: f3dbdddc6c2dc23e1dde2e6e6662c6c5ebf167e9 192.168.2.111:6380
   slots: (0 slots) slave
   replicates 75b477ef4967c1bf50502cb834df3fd5660cfa22
M: ebc80d8dcabe30645b96fdfe08de84f52a42cbff 192.168.2.113:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 75b477ef4967c1bf50502cb834df3fd5660cfa22 192.168.2.112:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 63cfbaf1e7d7d9fce095d0f485776ba15404bf8a 192.168.2.111:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: e6d09dcaa252336190e4e8747a60d067572d5156 192.168.2.112:6380
   slots: (0 slots) slave
   replicates ebc80d8dcabe30645b96fdfe08de84f52a42cbff
S: 5da5d0d7f7036c971926a821b33764fb2c78ea50 192.168.2.113:6380
   slots: (0 slots) slave
   replicates 63cfbaf1e7d7d9fce095d0f485776ba15404bf8a
[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)? 500
    -> 解释:希望分配多少个哈希槽,这里分配了500个哈希槽
What is the receiving node ID? 4ca16d842ea07379be11371aa2381529a9b5d7bc
    -> 解释:希望哪个节点接收这500个哈希槽(感觉废话),id从上面取(4ca16d842ea07379be11371aa2381529a9b5d7bc)
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.
    -> 解释:清楚如all或者done, all:从16384个哈希槽中随机分配500个槽,done:指定节点id从中分配500个
Source node #1: all
-> 解释:列出500个哈希槽,分别从哪些节点分配的
Ready to move 500 slots.
Source nodes:
M: ebc80d8dcabe30645b96fdfe08de84f52a42cbff 192.168.2.113:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 75b477ef4967c1bf50502cb834df3fd5660cfa22 192.168.2.112:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 63cfbaf1e7d7d9fce095d0f485776ba15404bf8a 192.168.2.111:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
Destination node:
M: 4ca16d842ea07379be11371aa2381529a9b5d7bc 192.168.2.111:6381
   slots: (0 slots) master
Resharding plan:
    -> 展示具体分配的是哪些哈希槽
    Moving slot 5461 from 75b477ef4967c1bf50502cb834df3fd5660cfa22
    .....
Do you want to proceed with the proposed reshard plan (yes/no)? yes
    ->解释:是否同意分配的哈希槽
    
     -> 展示具体分配的是哪些哈希槽
    Moving slot 5461 from 192.168.2.112:6379 to 192.168.2.111:6381: 
    .....

4: 查看分配结果
192.168.2.111:6381> cluster nodes
    4ca16d842ea07379be11371aa2381529a9b5d7bc 192.168.2.111:6381@16381 myself,master - 0 1555557769000 7 connected 0-165 5461-5627 10923-11088


二:从节点
[root@192 ~]# redis-cli --cluster add-node 192.168.2.111:6382 192.168.2.111:6379  --cluster-slave --cluster-master-id  4ca16d842ea07379be11371aa2381529a9b5d7bc
    结果:
    >>> Adding node 192.168.2.111:6382 to cluster 192.168.2.111:6379
    >>> Performing Cluster Check (using node 192.168.2.111:6379)
    M: 63cfbaf1e7d7d9fce095d0f485776ba15404bf8a 192.168.2.111:6379
       slots:[166-5460] (5295 slots) master
       1 additional replica(s)
    M: 75b477ef4967c1bf50502cb834df3fd5660cfa22 192.168.2.112:6379
       slots:[5628-10922] (5295 slots) master
       1 additional replica(s)
    M: 4ca16d842ea07379be11371aa2381529a9b5d7bc 192.168.2.111:6381
       slots:[0-165],[5461-5627],[10923-11088] (499 slots) master
    M: ebc80d8dcabe30645b96fdfe08de84f52a42cbff 192.168.2.113:6379
       slots:[11089-16383] (5295 slots) master
       1 additional replica(s)
    S: e6d09dcaa252336190e4e8747a60d067572d5156 192.168.2.112:6380
       slots: (0 slots) slave
       replicates ebc80d8dcabe30645b96fdfe08de84f52a42cbff
    S: f3dbdddc6c2dc23e1dde2e6e6662c6c5ebf167e9 192.168.2.111:6380
       slots: (0 slots) slave
       replicates 75b477ef4967c1bf50502cb834df3fd5660cfa22
    S: 5da5d0d7f7036c971926a821b33764fb2c78ea50 192.168.2.113:6380
       slots: (0 slots) slave
       replicates 63cfbaf1e7d7d9fce095d0f485776ba15404bf8a
    [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.2.111:6382 to make it join the cluster.
    Waiting for the cluster to join
    ..
    >>> Configure node as replica of 192.168.2.111:6381.
    [OK] New node added correctly.

命令解释:
redis-cli --cluster add-node new_host:new_port existing_host:existing_port
--cluster-slave --cluster-master-id  <arg>

2:查看节点信息
[root@192 ~]# redis-cli --cluster check 192.168.2.111:6382
    结果:
    192.168.2.111:6379 (63cfbaf1...) -> 1 keys | 5295 slots | 1 slaves.
    192.168.2.112:6379 (75b477ef...) -> 1 keys | 5295 slots | 1 slaves.
    192.168.2.113:6379 (ebc80d8d...) -> 0 keys | 5295 slots | 1 slaves.
    192.168.2.111:6381 (4ca16d84...) -> 0 keys | 499 slots | 1 slaves.
    [OK] 2 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.2.111:6382)
    S: cc3cfe29129ff7a5bcb2c5f803e8f4614b291373 192.168.2.111:6382
       slots: (0 slots) slave
       replicates 4ca16d842ea07379be11371aa2381529a9b5d7bc
       
    M: 4ca16d842ea07379be11371aa2381529a9b5d7bc 192.168.2.111:6381
       slots:[0-165],[5461-5627],[10923-11088] (499 slots) master
       1 additional replica(s)
    显示已经变成192.168.2.111:6381 的slave
发布了20 篇原创文章 · 获赞 8 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/web_snail/article/details/89384454
今日推荐