之前搭建了Redis的集群环境,3主3从共六个节点(三主三从redis集群搭建:https://blog.csdn.net/craftsman2020/article/details/128175886)
现在想要水平扩展、删除1主1从。
./redis-server ../redis-cluster/8001/redis-8001.conf
./redis-server ../redis-cluster/8002/redis-8002.conf
./redis-server ../redis-cluster/8003/redis-8003.conf
./redis-server ../redis-cluster/8004/redis-8004.conf
./redis-server ../redis-cluster/8005/redis-8005.conf
./redis-server ../redis-cluster/8006/redis-8006.conf
redis-cli -p 8003 -h 192.168.1.51 -c -a root
# 连接成功后,查看集群节点信息
192.168.1.51:8003> CLUSTER NODES
信息如下:
192.168.1.51:8003> cluster nodes
af4c1669c8cf1de35836ebeeec294227ab567120 192.168.1.52:8005@18005 master - 0 1685081492000 5 connected 10923-16383
96c7ce1ea3a57ad49a75e857c676aee8f2fb6011 192.168.1.50:8001@18001 slave 06683de7cf7c415b907913e5d4aece5a8091e9ed 0 1685081493000 9 connected
06683de7cf7c415b907913e5d4aece5a8091e9ed 192.168.1.51:8004@18004 master - 0 1685081493000 9 connected 0-5460
ed604208cf43e7b2490ff2640ae17e0ff590ef5e 192.168.1.51:8003@18003 myself,slave bd311076d5e70d5b8e4cfef073c6298a736c7880 0 1685081492000 3 connected
bd311076d5e70d5b8e4cfef073c6298a736c7880 192.168.1.52:8006@18006 master - 0 1685081493812 8 connected 5461-10922
72999f45b138940d65692e8f9c9f52d75df64fff 192.168.1.50:8002@18002 slave af4c1669c8cf1de35836ebeeec294227ab567120 0 1685081491806 5 connected
从上面信息可以看出,整个集群运行正常,三个master节点和三个slave节点,8004端口的实例节点存储0-5460这些hash槽,8006端口的实 例节点存储5461-10922这些hash槽,8005端口的实例节点存储10923-16383这些hash槽,这三个master节点存储的所有hash槽组成 redis集群的存储槽位,slave点是每个主节点的备份从节点,不显示存储槽位。
或者通过以下命令查看集群状态:
../src/redis-cli --cluster info 192.168.1.50:8001
显示:
[root@192 cluster]# ../src/redis-cli --cluster info 192.168.1.50:8001
192.168.1.52:8006 (a9e25213...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.51:8004 (579d2e92...) -> 0 keys | 5462 slots | 1 slaves.
192.168.1.52:8005 (78b51db2...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
我这里在192.168.1.53服务上集群配置文件同路径下创建了8007和8008文件夹,并将8001的配置文件复制到8007/redis-8007.conf和8008/redis-8008.conf,并对800x相关配置进行修改。
这里配置文件修改同之前的集群配置文件,将集群配置文件800x相关配置修改即可
./redis-server ../redis-cluster/8007/redis-8007.conf
./redis-server ../redis-cluster/8008/redis-8008.conf
# 查看redis进程
ps -ef|grep redis
8007、8008所在服务器redis进程
[[email protected] ~]$ ps -ef|grep redis
root 3166220 1 0 May25 ? 00:01:46 redis-server *:8008 [cluster]
root 3234303 1 0 May25 ? 00:02:42 redis-server *:8007 [cluster]
使用以下命令添加主节点。
redis-cli --cluster add-node new_host:new_port existing_host:existing_port --cluster-master-id node_id
例子
/usr/local/redis/bin/redis-cli -a 123456 --cluster add-node 192.168.1.53:8007 192.168.1.52:8005 --cluster-master-id 4568a2560a688898a5d2337bce3a288f12355ae8
redis集群的命令帮助参考 【4.redis集群命令帮助】
# 将8007加入集群,并成为主节点
./redis-cli --cluster add-node 192.168.1.53:8007 192.168.1.53:8001 -a root
加入成功!
由于本人增加节点的时候,没有逐个截图保存,在网上找了类似的结果如下(引用自网络):
从上面的信息可以看出:哈希槽分别为:0-5460、5464-10922、10923-16380(2的14次方)。
查看集群状态:
./redis-cli -h 192.168.1.50 -p 8001 -a root -c
# 查看集群状态
cluster nodes
由于本人增加节点的时候,没有逐个截图保存,在网上找了类似的结果如下(引用自网络):
注意: 当添加节点成功以后,新增的节点不会有任何数据,因为它还没有分配任何的slot(hash槽),我们需要为新节点手工分配hash槽。新增的节点也没有子节点。我们可以先给它分配hash槽,或者先给它添加子节点也可以。
命令
redis-cli --cluster add-node new_host:new_port existing_host:existing_port --cluster-slave --cluster-master-id node_id
例子
/usr/local/redis/bin/redis-cli -a 123456 --cluster add-node 192.168.1.53:8007 192.168.1.53:8008 --cluster-slave --cluster-master-id 66f3cbc063a84ff3c209fa2db7104e4666d82e9c
–cluster-master-id 后面显示的66f3cbc063a84ff3c209fa2db7104e4666d82e9c 为 8007 的标识,可从上一步信息中获取。
增加子节点之后,再次检查redis集群
../src/redis-cli --cluster check 192.168.1.50:8001
或者通过以下命令查看
# 连接8007主节点
./redis-cli -h 192.168.1.53 -p 8007 -a root -c
# 查看集群信息
cluster nodes
由于本人增加节点的时候,没有逐个截图保存,在网上找了类似的结果如下(引用自网络)
我们接下来就需要为8007分配hash槽
../redis-cli -a root --cluster reshard 192.169.1.53:8007
[root@192 cluster]# ../src/redis-cli --cluster reshard 192.168.1.53:8007
>>> Performing Cluster Check (using node 192.168.1.53:8007)
M: 3067f715eaa5c88c260fb2e7f21edc06112208d3 192.168.1.53:8007
slots: (0 slots) master
1 additional replica(s)
M: 579d2e9233faa12d6e13dfba8fe37f7e1c9983f8 192.168.1.50:8002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 9e266605685ab453eccdb6042327a7e187cee7c3 192.168.1.51:8004
slots: (0 slots) slave
replicates 579d2e9233faa12d6e13dfba8fe37f7e1c9983f8
S: 1780235a26ce1ed943fcdff24c509ebb27c2d03e 192.168.1.52:8005
slots: (0 slots) slave
replicates 78b51db2871be7fa265db054dd79cded9e97c4ff
S: b6f7f1df8c16039c764d4c259182515139263e84 192.168.1.52:8006
slots: (0 slots) slave
replicates a9e25213d46b7d19bc46931b0e2490a93c8237ac
S: 5ef427a8a8c752301826e3f88e3a7282ff75a537 192.168.1.53:8008
slots: (0 slots) slave
replicates 3067f715eaa5c88c260fb2e7f21edc06112208d3
M: 78b51db2871be7fa265db054dd79cded9e97c4ff 192.168.1.51:8003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: a9e25213d46b7d19bc46931b0e2490a93c8237ac 192.168.1.50:8001
slots:[0-5460] (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.
How many slots do you want to move (from 1 to 16384)?
输入(4个主节点,共16384个哈希槽,平均每个主节点为:4096):4096
显示:
[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)? 4096
What is the receiving node ID?
输入(6007的标识):3067f715eaa5c88c260fb2e7f21edc06112208d3
显示:
[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)? 4096
What is the receiving node ID? 3067f715eaa5c88c260fb2e7f21edc06112208d3
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:
输入(将从哪些节点分配至8007):all
显示:
***
Moving slot 1359 from a9e25213d46b7d19bc46931b0e2490a93c8237ac
Moving slot 1360 from a9e25213d46b7d19bc46931b0e2490a93c8237ac
Moving slot 1361 from a9e25213d46b7d19bc46931b0e2490a93c8237ac
Moving slot 1362 from a9e25213d46b7d19bc46931b0e2490a93c8237ac
Moving slot 1363 from a9e25213d46b7d19bc46931b0e2490a93c8237ac
Moving slot 1364 from a9e25213d46b7d19bc46931b0e2490a93c8237ac
Do you want to proceed with the proposed reshard plan (yes/no)?
输入:yes
至此已完成 哈希槽 的分配。
redis-cli --cluster check 192.168.1.53:8007
显示:
8007 有4096个哈希槽,有一个子节点。
(base)[root@192-cluster]$redis-cli -a 123456 --cluster check 10.12.1.53:8007
10.12.1.53:8007 (84d4bc3e...) -> 127 keys | 4096 slots | 1 slaves.
10.12.1.51:8004 (06683de7...) -> 128 keys | 4096 slots | 1 slaves.
192.168.1.52:8005 (af4c1669...) -> 151 keys | 4096 slots | 1 slaves.
192.168.1.52:8006 (bd311076...) -> 135 keys | 4096 slots | 1 slaves.
[OK] 541 keys in 4 masters.
0.03 keys per slot on average.
>>> Performing Cluster Check (using node 10.12.1.53:8007)
M: 84d4bc3e46a829df205f645867468f5ec44682e9 10.12.1.53:8007
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
1 additional replica(s)
M: 06683de7cf7c415b907913e5d4aece5a8091e9ed 10.12.1.51:8004
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: af4c1669c8cf1de35836ebeeec294227ab567120 192.168.1.52:8005
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: 72999f45b138940d65692e8f9c9f52d75df64fff 10.12.1.50:8002
slots: (0 slots) slave
replicates af4c1669c8cf1de35836ebeeec294227ab567120
S: ed604208cf43e7b2490ff2640ae17e0ff590ef5e 10.12.1.51:8003
slots: (0 slots) slave
replicates bd311076d5e70d5b8e4cfef073c6298a736c7880
S: bf352d47f8c4559b3bf54aecc0f2f75687eac4e2 10.12.1.53:8008
slots: (0 slots) slave
replicates 84d4bc3e46a829df205f645867468f5ec44682e9
M: bd311076d5e70d5b8e4cfef073c6298a736c7880 192.168.1.52:8006
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: 96c7ce1ea3a57ad49a75e857c676aee8f2fb6011 10.12.1.50:8001
slots: (0 slots) slave
replicates 06683de7cf7c415b907913e5d4aece5a8091e9ed
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
这里主要介绍主节点的删除。删除主节点稍微麻烦一点,因为主节点分配了 slots 槽,所以必须先把 slots 槽放到其他可用节点中去, 然后再进行移除节点操作才行,不然会出现数据丢失问题。从节点的删除很简单,在删除主节点的过程中,将hash槽转移到其他主节点之后,最后再删除没有hash槽的主节点。而删除从节点的操作就和删除没有hash槽的主节点一样。
把数据移动到其它主节点中去,执行重新分片命令。
redis-cli -a 111111--cluster reshard xx.xx.xx.xx:8005
xx.xx.xx.xx:8005分配了 4096个槽,这里输入 4096即可。
回车以后,出现 what is the receiving node ID? 意思是你想移动到那个节点上。
我想移动到 8003的节点上,那么此处输入 8003节点的 ID。
回车以后,还可以继续选择其他源节点,但是我这里只想把 8005节点分到8003就行,此处输入 done 即可,否则输入其它节点的 ID,最后输入 done。
注意:其他源节点必须是主节点。
# 再次查看集群信息
redis-cli -c -a 111111-h xx.xx.xx.xx -p 8001cluster nodes
# 发现 8005已经没有 slots 槽分配了
图中之所以是fail,是因为我已经kill掉了这个节点的redis进程。不用kill也没有关系。
接下来,调用删除从节点的方式,删除主节点。
redis-cli -a 111111 --cluster del-node xx.xx.xx.xx:8005 af4c1669c8cf1de35836ebeeec294227ab567120
# 再次查看集群信息
redis-cli -c -a 111111 -h xx.xx.xx.xx -p 8001 cluster nodes
# 环境中 8005主节点已被移除
# 查看redis集群的命令帮助
./redis‐cli ‐‐cluster help
[root@192]$ redis-cli --cluster help
Cluster Manager Commands:
create host1:port1 ... hostN:portN
--cluster-replicas <arg>
check host:port
--cluster-search-multiple-owners
info host:port
fix host:port
--cluster-search-multiple-owners
reshard host:port
--cluster-from <arg>
--cluster-to <arg>
--cluster-slots <arg>
--cluster-yes
--cluster-timeout <arg>
--cluster-pipeline <arg>
--cluster-replace
rebalance host:port
--cluster-weight <node1=w1...nodeN=wN>
--cluster-use-empty-masters
--cluster-timeout <arg>
--cluster-simulate
--cluster-pipeline <arg>
--cluster-threshold <arg>
--cluster-replace
add-node new_host:new_port existing_host:existing_port
--cluster-slave
--cluster-master-id <arg>
del-node host:port node_id
call host:port command arg arg .. arg
set-timeout host:port milliseconds
import host:port
--cluster-from <arg>
--cluster-copy
--cluster-replace
help
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.