Redis集群提供了灵活的节点扩容和收缩方案。在不影响集群对外服务的情况下,可以为集群添加节点进行扩容也可以下线部分节点进行缩容。
Redis集群可以实现对节点的灵活上下线控制。其中原理可抽象为槽和对应数据在不同节点之间灵活移动。首先来看我们之前搭建的集群槽和数据与节点的对应关系。
**三个主节点分别维护自己负责的槽和对应的数据**,如果希望加入1个节点实现集群扩容时,需要通过相关命令把一部分槽和数据迁移给新节点。集群扩容 节点配置和启动节点
我们加入两个节点,主节点的端口为6903,从节点的端口为6933。配置与前面的6900类似,不再赘述。
启动这两个节点。
使用之前编写的脚本cp配置
./cp-redis-config.sh 6900 6903 m ./cp-redis-config.sh 6900 6933 s
启动新节点
./redis-server ../conf/cluster_m_6903.conf ./redis-server ../conf/cluster_s_6933.conf加入集群
执行命令
./redis-cli --cluster info localhost:6900 [root@localhost src]# ./redis-cli --cluster info localhost:6900 -a cc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. localhost:6900 (224c0426...) -> 0 keys | 5461 slots | 1 slaves. 127.0.0.1:6901 (fa2199a4...) -> 0 keys | 5462 slots | 1 slaves. 127.0.0.1:6902 (6497602a...) -> 0 keys | 5461 slots | 1 slaves.
执行命令
##-a cc 认证必须在cluster nodes前面 [root@localhost src]# ./redis-cli -h localhost -p 6900 -a cc cluster nodes ./redis-cli -h localWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 4ded6ea1b37e88f2a9d18f28417191ead33435b4 127.0.0.1:6930@16930 slave 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d 0 1641190590800 1 connected fa2199a4049ef681a0f4c683bd13631dbbd52551 127.0.0.1:6901@16901 master - 0 1641190591000 2 connected 5461-10922 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d 127.0.0.1:6900@16900 myself,master - 0 1641190590000 1 connected 0-5460 6497602a2780465ecc48e7b7744ad8e560702a76 127.0.0.1:6902@16902 master - 0 1641190592816 3 connected 10923-16383 9f306d850e9c708c109748c84a04f2d8b905a5fd 127.0.0.1:6931@16931 slave fa2199a4049ef681a0f4c683bd13631dbbd52551 0 1641190591000 2 connected c0a78a8c1f9d0554bea95148b1b9fab3590b38ea 127.0.0.1:6932@16932 slave 6497602a2780465ecc48e7b7744ad8e560702a76 0 1641190591808 3 connected
可以看到,6903和6933还属于孤立节点,需要将这两个实例节点加入到集群中。
将主节点6903加入集群执行命令
./redis-cli --cluster add-node localhost:6903 127.0.0.1:6900 -a cc
./redis-cli --cluster add-node localhost:6903 localhost:6900 >>> Send CLUSTER MEET to node localhost:6903 to make it join the cluster. Node localhost:6903 replied with error: ERR Invalid node address specified: localhost:6900 改成 127.0.0.1 [root@localhost src]# ./redis-cli --cluster add-node localhost:6903 127.0.0.1:6900 -a cc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Adding node localhost:6903 to cluster 127.0.0.1:6900 >>> Performing Cluster Check (using node 127.0.0.1:6900) M: 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d 127.0.0.1:6900 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 4ded6ea1b37e88f2a9d18f28417191ead33435b4 127.0.0.1:6930 slots: (0 slots) slave replicates 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d M: fa2199a4049ef681a0f4c683bd13631dbbd52551 127.0.0.1:6901 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: 6497602a2780465ecc48e7b7744ad8e560702a76 127.0.0.1:6902 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 9f306d850e9c708c109748c84a04f2d8b905a5fd 127.0.0.1:6931 slots: (0 slots) slave replicates fa2199a4049ef681a0f4c683bd13631dbbd52551 S: c0a78a8c1f9d0554bea95148b1b9fab3590b38ea 127.0.0.1:6932 slots: (0 slots) slave replicates 6497602a2780465ecc48e7b7744ad8e560702a76 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node localhost:6903 to make it join the cluster. [OK] New node added correctly.
执行命令
./redis-cli --cluster info localhost:6900 [root@localhost src]# ./redis-cli --cluster info localhost:6900 -a cc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. localhost:6900 (224c0426...) -> 0 keys | 5461 slots | 1 slaves. 127.0.0.1:6901 (fa2199a4...) -> 0 keys | 5462 slots | 1 slaves. 127.0.0.1:6903 (706395d2...) -> 0 keys | 0 slots | 0 slaves. ##没有数据槽, 没有从节点 127.0.0.1:6902 (6497602a...) -> 0 keys | 5461 slots | 1 slaves. [OK] 0 keys in 4 masters. 0.00 keys per slot on average.
执行命令
./redis-cli -p 6900 cluster nodes
[root@localhost src]# ./redis-cli -p 6900 -a cc cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 4ded6ea1b37e88f2a9d18f28417191ead33435b4 127.0.0.1:6930@16930 slave 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d 0 1641191241325 1 connected fa2199a4049ef681a0f4c683bd13631dbbd52551 127.0.0.1:6901@16901 master - 0 1641191245357 2 connected 5461-10922 706395d234c72167b2f1285f88825b947fee147f 127.0.0.1:6903@16903 master - 0 1641191242000 0 connected 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d 127.0.0.1:6900@16900 myself,master - 0 1641191243000 1 connected 0-5460 6497602a2780465ecc48e7b7744ad8e560702a76 127.0.0.1:6902@16902 master - 0 1641191244350 3 connected 10923-16383 9f306d850e9c708c109748c84a04f2d8b905a5fd 127.0.0.1:6931@16931 slave fa2199a4049ef681a0f4c683bd13631dbbd52551 0 1641191244000 2 connected c0a78a8c1f9d0554bea95148b1b9fab3590b38ea 127.0.0.1:6932@16932 slave 6497602a2780465ecc48e7b7744ad8e560702a76 0 1641191243340 3 connected将从节点6933加入集群
执行命令, 同时将刚刚加入的节点6903作为从节点6933的主节点
./redis-cli --cluster add-node localhost:6933 127.0.0.1:6900 --cluster-slave --cluster-master-id 706395d234c72167b2f1285f88825b947fee147f -a cc [root@localhost src]# ./redis-cli --cluster add-node localhost:6933 127.0.0.1:6900 --cluster-slave --cluster-master-id 706395d234c72167b2f1285f88825b947fee147f -a cc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Adding node localhost:6933 to cluster 127.0.0.1:6900 >>> Performing Cluster Check (using node 127.0.0.1:6900) M: 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d 127.0.0.1:6900 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 4ded6ea1b37e88f2a9d18f28417191ead33435b4 127.0.0.1:6930 slots: (0 slots) slave replicates 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d M: fa2199a4049ef681a0f4c683bd13631dbbd52551 127.0.0.1:6901 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: 706395d234c72167b2f1285f88825b947fee147f 127.0.0.1:6903 slots: (0 slots) master M: 6497602a2780465ecc48e7b7744ad8e560702a76 127.0.0.1:6902 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 9f306d850e9c708c109748c84a04f2d8b905a5fd 127.0.0.1:6931 slots: (0 slots) slave replicates fa2199a4049ef681a0f4c683bd13631dbbd52551 S: c0a78a8c1f9d0554bea95148b1b9fab3590b38ea 127.0.0.1:6932 slots: (0 slots) slave replicates 6497602a2780465ecc48e7b7744ad8e560702a76 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node localhost:6933 to make it join the cluster. Waiting for the cluster to join >>> Configure node as replica of 127.0.0.1:6903. [OK] New node added correctly.
报错:
Node localhost:6933 replied with error:
ERR Invalid node address specified: localhost:6900
localhost替换成127.0.0.1
查看节点信息
[root@localhost src]# ./redis-cli -p 6900 -a cc cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 4ded6ea1b37e88f2a9d18f28417191ead33435b4 127.0.0.1:6930@16930 slave 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d 0 1641191655000 1 connected fa2199a4049ef681a0f4c683bd13631dbbd52551 127.0.0.1:6901@16901 master - 0 1641191656053 2 connected 5461-10922 706395d234c72167b2f1285f88825b947fee147f 127.0.0.1:6903@16903 master - 0 1641191656000 0 connected ea31b19744a25c35f3cbfd7359378f196ecbb449 127.0.0.1:6933@16933 slave 706395d234c72167b2f1285f88825b947fee147f 0 1641191656961 0 connected 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d 127.0.0.1:6900@16900 myself,master - 0 1641191653000 1 connected 0-5460 6497602a2780465ecc48e7b7744ad8e560702a76 127.0.0.1:6902@16902 master - 0 1641191657970 3 connected 10923-16383 9f306d850e9c708c109748c84a04f2d8b905a5fd 127.0.0.1:6931@16931 slave fa2199a4049ef681a0f4c683bd13631dbbd52551 0 1641191657000 2 connected c0a78a8c1f9d0554bea95148b1b9fab3590b38ea 127.0.0.1:6932@16932 slave 6497602a2780465ecc48e7b7744ad8e560702a76 0 1641191658979 3 connected
查看集群信息
[root@localhost src]# ./redis-cli --cluster info localhost:6900 -a cc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. localhost:6900 (224c0426...) -> 0 keys | 5461 slots | 1 slaves. 127.0.0.1:6901 (fa2199a4...) -> 0 keys | 5462 slots | 1 slaves. 127.0.0.1:6903 (706395d2...) -> 0 keys | 0 slots | 1 slaves. ##没有数据槽, 有从节点 127.0.0.1:6902 (6497602a...) -> 0 keys | 5461 slots | 1 slaves. [OK] 0 keys in 4 masters. 0.00 keys per slot on average.迁移槽和数据
上面的图中可以看到,6903和6933已正确添加到集群中,接下来就开始分配槽位。我们将6900、6901、6902三个节点中的槽位分别迁出一些槽位给6903,假设分配后的每个节点槽位平均,那么应该分出(16384/4)=4096个槽位。
执行命令
./redis-cli --cluster reshard localhost:6900 [root@localhost src]# ./redis-cli --cluster reshard localhost:6900 -a cc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing Cluster Check (using node localhost:6900) M: 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d localhost:6900 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 4ded6ea1b37e88f2a9d18f28417191ead33435b4 127.0.0.1:6930 slots: (0 slots) slave replicates 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d M: fa2199a4049ef681a0f4c683bd13631dbbd52551 127.0.0.1:6901 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: 706395d234c72167b2f1285f88825b947fee147f 127.0.0.1:6903 slots: (0 slots) master 1 additional replica(s) S: ea31b19744a25c35f3cbfd7359378f196ecbb449 127.0.0.1:6933 slots: (0 slots) slave replicates 706395d234c72167b2f1285f88825b947fee147f M: 6497602a2780465ecc48e7b7744ad8e560702a76 127.0.0.1:6902 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 9f306d850e9c708c109748c84a04f2d8b905a5fd 127.0.0.1:6931 slots: (0 slots) slave replicates fa2199a4049ef681a0f4c683bd13631dbbd52551 S: c0a78a8c1f9d0554bea95148b1b9fab3590b38ea 127.0.0.1:6932 slots: (0 slots) slave replicates 6497602a2780465ecc48e7b7744ad8e560702a76 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
Redis会提问要迁移的槽位数和接受槽位的节点id,我们这里输入4096和706395d234c72167b2f1285f88825b947fee147f。
How many slots do you want to move (from 1 to 16384)? 4096 What is the receiving node ID? 706395d234c72167b2f1285f88825b947fee147f
接下来,Redis会提问从哪些源节点进行迁移,我们输入“all”
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:
Redis会显示一个分配计划:
... Moving slot 12286 from 6497602a2780465ecc48e7b7744ad8e560702a76 Moving slot 12287 from 6497602a2780465ecc48e7b7744ad8e560702a76 Do you want to proceed with the proposed reshard plan (yes/no)? yes
填入“yes”。
Redis会开始进行迁移, 稍等一会,等待Redis迁移完成。
迁移完成后,执行命令
./redis-cli -p 6900 cluster nodes ## 注意认证信息, 在端口之后
节点信息
[root@localhost src]# ./redis-cli -p 6900 -a cc cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 4ded6ea1b37e88f2a9d18f28417191ead33435b4 127.0.0.1:6930@16930 slave 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d 0 1641192161407 1 connected fa2199a4049ef681a0f4c683bd13631dbbd52551 127.0.0.1:6901@16901 master - 0 1641192161407 2 connected 6827-10922 706395d234c72167b2f1285f88825b947fee147f 127.0.0.1:6903@16903 master - 0 1641192160399 7 connected 0-1364 5461-6826 10923-12287 ## 三个区间的槽位 ea31b19744a25c35f3cbfd7359378f196ecbb449 127.0.0.1:6933@16933 slave 706395d234c72167b2f1285f88825b947fee147f 0 1641192161003 7 connected 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d 127.0.0.1:6900@16900 myself,master - 0 1641192160000 1 connected 1365-5460 6497602a2780465ecc48e7b7744ad8e560702a76 127.0.0.1:6902@16902 master - 0 1641192158000 3 connected 12288-16383 9f306d850e9c708c109748c84a04f2d8b905a5fd 127.0.0.1:6931@16931 slave fa2199a4049ef681a0f4c683bd13631dbbd52551 0 1641192160000 2 connected c0a78a8c1f9d0554bea95148b1b9fab3590b38ea 127.0.0.1:6932@16932 slave 6497602a2780465ecc48e7b7744ad8e560702a76 0 1641192159390 3 connected
集群信息
[root@localhost src]# ./redis-cli --cluster info 127.0.0.1:6900 -a cc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6900 (224c0426...) -> 0 keys | 4096 slots | 1 slaves. 127.0.0.1:6901 (fa2199a4...) -> 0 keys | 4096 slots | 1 slaves. 127.0.0.1:6903 (706395d2...) -> 0 keys | 4096 slots | 1 slaves. 127.0.0.1:6902 (6497602a...) -> 0 keys | 4096 slots | 1 slaves. [OK] 0 keys in 4 masters. 0.00 keys per slot on average.
可以看到槽位确实被迁移到了节点6903之上。这样就实现了集群的扩容。
集群缩容 迁移槽和数据 命令语法:redis-cli --cluster reshard --cluster-from 要迁出节点ID --cluster-to 接收槽节点ID --cluster-slots 迁出槽数量 已存在节点ip 端口
例如:
迁出1365个槽位到6900节点
./redis-cli --cluster reshard --cluster-from 706395d234c72167b2f1285f88825b947fee147f --cluster-to 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d --cluster-slots 1365 localhost:6900 -a cc
迁出1365个槽位到6901节点
./redis-cli --cluster reshard --cluster-from 706395d234c72167b2f1285f88825b947fee147f --cluster-to fa2199a4049ef681a0f4c683bd13631dbbd52551 --cluster-slots 1365 localhost:6900 -a cc
迁出1366个槽位到6902节点
./redis-cli --cluster reshard --cluster-from 706395d234c72167b2f1285f88825b947fee147f --cluster-to 6497602a2780465ecc48e7b7744ad8e560702a76 --cluster-slots 1366 localhost:6900 -a cc
稍等片刻,等全部槽迁移完成后,执行命令
./redis-cli -p 6900 -a cc cluster nodes
[root@localhost src]# ./redis-cli -p 6900 -a cc cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 4ded6ea1b37e88f2a9d18f28417191ead33435b4 127.0.0.1:6930@16930 slave 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d 0 1641193578799 8 connected fa2199a4049ef681a0f4c683bd13631dbbd52551 127.0.0.1:6901@16901 master - 0 1641193579807 9 connected 5461-6825 6827-10922 706395d234c72167b2f1285f88825b947fee147f 127.0.0.1:6903@16903 master - 0 1641193580009 7 connected ea31b19744a25c35f3cbfd7359378f196ecbb449 127.0.0.1:6933@16933 slave 6497602a2780465ecc48e7b7744ad8e560702a76 0 1641193579000 10 connected 224c042651ff55e9bbf67c8ff9ce6edc4cdae63d 127.0.0.1:6900@16900 myself,master - 0 1641193577000 8 connected 0-5460 6497602a2780465ecc48e7b7744ad8e560702a76 127.0.0.1:6902@16902 master - 0 1641193579000 10 connected 6826 10923-16383 9f306d850e9c708c109748c84a04f2d8b905a5fd 127.0.0.1:6931@16931 slave fa2199a4049ef681a0f4c683bd13631dbbd52551 0 1641193576000 9 connected c0a78a8c1f9d0554bea95148b1b9fab3590b38ea 127.0.0.1:6932@16932 slave 6497602a2780465ecc48e7b7744ad8e560702a76 0 1641193579000 10 connected
./redis-cli --cluster info localhost:6900 -a cc
[root@localhost src]# ./redis-cli --cluster info localhost:6900 -a cc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. localhost:6900 (224c0426...) -> 0 keys | 5461 slots | 1 slaves. 127.0.0.1:6901 (fa2199a4...) -> 0 keys | 5461 slots | 1 slaves. 127.0.0.1:6903 (706395d2...) -> 0 keys | 0 slots | 0 slaves. 127.0.0.1:6902 (6497602a...) -> 0 keys | 5462 slots | 2 slaves. [OK] 0 keys in 4 masters. 0.00 keys per slot on average.
可以看到6903上不再存在着槽了。
下线节点执行命令格式redis-cli --cluster del-node 已存在节点:端口 要删除的节点ID
例如:
./redis-cli --cluster del-node localhost:6900 706395d234c72167b2f1285f88825b947fee147f
[root@localhost src]# ./redis-cli --cluster del-node localhost:6900 706395d234c72167b2f1285f88825b947fee147f -a cc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Removing node 706395d234c72167b2f1285f88825b947fee147f from cluster localhost:6900 >>> Sending CLUSTER FORGET messages to the cluster... >>> Sending CLUSTER RESET SOFT to the deleted node.
./redis-cli --cluster del-node localhost:6900 ea31b19744a25c35f3cbfd7359378f196ecbb449
[root@localhost src]# ./redis-cli --cluster del-node localhost:6900 ea31b19744a25c35f3cbfd7359378f196ecbb449 - a cc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Removing node ea31b19744a25c35f3cbfd7359378f196ecbb449 from cluster localhost:6900 >>> Sending CLUSTER FORGET messages to the cluster... >>> Sending CLUSTER RESET SOFT to the deleted node.
可以看到这两个节点确实脱离集群了,这样就完成了集群的缩容
[root@localhost src]# ./redis-cli --cluster info localhost:6900 -a cc Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. localhost:6900 (224c0426...) -> 0 keys | 5461 slots | 1 slaves. 127.0.0.1:6901 (fa2199a4...) -> 0 keys | 5461 slots | 1 slaves. 127.0.0.1:6902 (6497602a...) -> 0 keys | 5462 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average.
再关闭节点即可。
./redis-cli -p 6903 -a cc shutdown ./redis-cli -p 6933 -a cc shutdown
查看进程
[root@localhost src]# ps -ef | grep 69 root 169 2 0 05:38 ? 00:00:00 [irq/36-pciehp] root 869 2 0 05:38 ? 00:00:00 [rpciod] root 2469 2247 0 05:43 tty2 00:00:02 /usr/libexec/gsd-smartcard root 17482 1 0 20:24 ? 00:00:09 ./redis-server *:6900 [cluster] root 17484 1 0 20:24 ? 00:00:09 ./redis-server *:6901 [cluster] root 17494 1 0 20:24 ? 00:00:09 ./redis-server *:6902 [cluster] root 17500 1 0 20:24 ? 00:00:07 ./redis-server *:6930 [cluster] root 17506 1 0 20:24 ? 00:00:07 ./redis-server *:6931 [cluster] root 17512 1 0 20:24 ? 00:00:07 ./redis-server *:6932 [cluster] root 20281 2970 0 22:54 pts/1 00:00:00 ./redis-cli --cluster info localhsot 6900 -a cc root 20637 2970 0 23:13 pts/1 00:00:00 grep --color=auto 69
已关闭
迁移相关 在线迁移slot在线把集群的一些slot从集群原来slot节点迁移到新的节点。其实在前面扩容集群的时候我们已经看到了相关的用法
直接连接到集群的任意一节点
redis-cli --cluster reshard XXXXXXXXXXX:XXXX
按提示 *** 作即可。
平衡(rebalance)slot1)平衡集群中各个节点的slot数量
redis-cli --cluster rebalance XXXXXXXXXXX:XXXX
2)还可以根据集群中各个节点设置的权重来平衡slot数量
redis-cli --cluster rebalance --cluster-weight 117457eab5071954faab5e81c3170600d5192270=5 815da8448f5d5a304df0353ca10d8f9b77016b28=4 56005b9413cbf225783906307a2631109e753f8f=3 --cluster-simulate 192.168.163.132:6379
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)