文章目录
- 1. 背景
- 2. Redis集群水平扩展
- 2.1 启动集群
- 2.2 连接其中一个节点
- 2.3 配置新节点
- 2.4 启动新节点
- 2.5 配置8007节点加入集群,成为主节点
- 2.6 增加子节点
- 2.7 分配hash槽
- 2.8 检查 Redis 集群
- 3. 节点删除
- 4. redis集群命令帮助
- 5. 参考文献
1. 背景
之前搭建了Redis的集群环境,3主3从共六个节点(三主三从redis集群搭建:https://blog.csdn.net/craftsman2020/article/details/128175886)
现在想要水平扩展、删除1主1从。
2. Redis集群水平扩展
2.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
2.2 连接其中一个节点
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.
2.3 配置新节点
我这里在192.168.1.53服务上集群配置文件同路径下创建了8007和8008文件夹,并将8001的配置文件复制到8007/redis-8007.conf和8008/redis-8008.conf,并对800x相关配置进行修改。
这里配置文件修改同之前的集群配置文件,将集群配置文件800x相关配置修改即可
2.4 启动新节点
./redis-server ../redis-cluster/8007/redis-8007.conf
./redis-server ../redis-cluster/8008/redis-8008.conf
# 查看redis进程
ps -ef|grep redis
8007、8008所在服务器redis进程
[root@192.168.1.53 ~]$ 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]
2.5 配置8007节点加入集群,成为主节点
使用以下命令添加主节点。
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
- new_host:new_port:为要新添加的主节点 IP 和端口
- existing_host:existing_port:表示的是环境中已存在的最后一个主节点的 IP 和端口,这个可以通过 查看节点信息得知,根据 slots 槽数,192.168.1.52:8005 对应的节点槽数是 10923-16383, 16383 表示的是最后的槽数
- –cluster-master-id:表示的是最后一个主节点的节点 ID,表示的是新添加的主节点要在这个节点 后面
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槽,或者先给它添加子节点也可以。
2.6 增加子节点
命令
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
- new_host:new_port:表示的是要添加的那个从节点的 IP 和端口
- existing_host:existing_port:表示的是要给哪个主节点添加从节点
- –cluster-slave:表示的是要添加从节点
- –cluster-master-id node_id:表示要给哪个主节点添加从节点,该主节点节点 ID
–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
由于本人增加节点的时候,没有逐个截图保存,在网上找了类似的结果如下(引用自网络)
2.7 分配hash槽
我们接下来就需要为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
至此已完成 哈希槽 的分配。
2.8 检查 Redis 集群
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.
3. 节点删除
4. redis集群命令帮助
# 查看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.
- create:创建一个集群环境host1:port1 … hostN:portN
- call:可以执行redis命令
- add-node:将一个节点添加到集群里,第一个参数为新节点的ip:port,第二个参数为集群中任意一个已经存在的节点的ip:port
- del-node:移除一个节点
- reshard:重新分片
- check:检查集群状态
5. 参考文献
- CentOS 1908 配置 Redis 5.0.7 五(集群新增 主从节点)https://blog.csdn.net/qq_32596527/article/details/104402130
- Redis集群水平扩容与节点删除 https://blog.csdn.net/weixin_40955398/article/details/122666132
- Redis5.0+——集群搭建,水平扩容 https://blog.csdn.net/G_change_/article/details/128448636
- Redis集群节点管理(cluster)https://blog.csdn.net/weixin_41753567/article/details/124110749
- 三台机器搭建redis集群过程及问题记录
- windows环境下redis安装、配置、使用、卸载
- Linux安装部署Redis
- redis5.0集群搭建(两台服务器)