Redis集群实现动态扩缩容
2021/8/7 2:05:54
本文主要是介绍Redis集群实现动态扩缩容,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
创建 redis cluster集群的环境准备
系统 | 内存 | redis版本 | 主机名 | 主机IP |
CentOS8 | 2G | 5.0.3 | Master1 | 192.168.18.80 |
CentOS8 | 2G | 5.0.3 | Master2 | 192.168.18.81 |
CentOS8 | 2G | 5.0.3 | Master3 | 192.168.18.82 |
CentOS8 | 2G | 5.0.3 | Slave1 | 192.168.18.83 |
CentOS8 | 2G | 5.0.3 | Slave2 | 192.168.18.84 |
CentOS8 | 2G | 5.0.3 | Slave3 | 192.168.18.85 |
所有主机安装redis
[root@Master1 ~]#dnf -y install redis [root@Master2 ~]#dnf -y install redis [root@Master3 ~]#dnf -y install redis [root@Slave1 ~]#dnf -y install redis [root@Slave2 ~]#dnf -y install redis [root@Slave3 ~]#dnf -y install redis
所有主机修改redis配置文件
[root@Master1 ~]# vim /etc/redis.conf bind 0.0.0.0 daemonize yes #以守护进程方式运行 masterauth 123456 #slave连接master密码,master可省略 requirepass 123456 #设置本机redis连接密码 cluster-enabled yes #取消此行注释,开启redis cluster集群功能 cluster-config-file nodes-6379.conf #取消此行注释,此为集群状态文件,记录主从关系及slot范围信息,由redis cluster 集群自动创建和维护 cluster-require-full-coverage no #集群请求槽位全部覆盖,默认值为yes,设置为no则槽位不全也能对外提供服务 #启动redis并设置开机自启 [root@Master1 ~]#systemctl enable --now redis
创建redis集群
# redis-cli --cluster-replicas 1 表示每个master对应一个slave节点 [root@Master1 ~]# redis-cli -a 123456 --cluster create 192.168.18.80:6379 192.168.18.81:6379 192.168.18.82:6379 192.168.18.83:6379 192.168.18.84:6379 192.168.18.85:6379 --cluster-replicas 1 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 192.168.18.83:6379 to 192.168.18.80:6379 Adding replica 192.168.18.84:6379 to 192.168.18.81:6379 Adding replica 192.168.18.85:6379 to 192.168.18.82:6379 M: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379 slots:[0-5460] (5461 slots) master M: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379 slots:[5461-10922] (5462 slots) master M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379 slots:[10923-16383] (5461 slots) master S: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379 replicates 07f6fea79e94447ae2116a212c575e87f726c0b2 S: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379 replicates f361eb219afe76e1d316ea75628d3f8f16e2b8cc S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379 replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1 Can I set the above configuration? (type 'yes' to accept): yes #输入yes自动创建集群 >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join ..... >>> Performing Cluster Check (using node 192.168.18.80:6379) M: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379 slots: (0 slots) slave replicates 07f6fea79e94447ae2116a212c575e87f726c0b2 S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379 slots: (0 slots) slave replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1 S: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379 slots: (0 slots) slave replicates f361eb219afe76e1d316ea75628d3f8f16e2b8cc M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379 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.
验证集群状态
[root@Master1 ~]# redis-cli -a 123456 cluster info Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:453 cluster_stats_messages_pong_sent:454 cluster_stats_messages_sent:907 cluster_stats_messages_ping_received:449 cluster_stats_messages_pong_received:453 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:907
查看集群node对应关系
[root@Master1 ~]# redis-cli -a 123456 cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379@16379 master - 0 1628090052349 2 connected 5461-10922 a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379@16379 slave 07f6fea79e94447ae2116a212c575e87f726c0b2 0 1628090053355 4 connected 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379@16379 slave 9ff40f467859b3daf016b9270c9f657eef11d3e1 0 1628090053000 6 connected 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379@16379 slave f361eb219afe76e1d316ea75628d3f8f16e2b8cc 0 1628090054363 5 connected 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379@16379 master - 0 1628090053000 3 connected 10923-16383 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379@16379 myself,master - 0 1628090051000 1 connected 0-5460
Python脚本实现Redis Cluster 集群批量写入数据
[root@Master1 ~]# which pip3 /usr/bin/pip3 #安装redis集群模块 [root@Master1 ~]# pip3 install redis-py-cluster #编写python脚本 [root@Master1 ~]# cat redis_cluster_test.py #!/usr/bin/env python3 from rediscluster import RedisCluster startup_nodes = [ {"host":"192.168.18.80", "port":6379}, {"host":"192.168.18.81", "port":6379}, {"host":"192.168.18.82", "port":6379}, {"host":"192.168.18.83", "port":6379}, {"host":"192.168.18.84", "port":6379}, {"host":"192.168.18.85", "port":6379} ] redis_conn= RedisCluster(startup_nodes=startup_nodes,password='123456', decode_responses=True) for i in range(0, 10000): redis_conn.set('key'+str(i),'value'+str(i)) print('key'+str(i)+':',redis_conn.get('key'+str(i))) #执行python脚本 [root@Master1 ~]# python3 redis_cluster_test.py #验证数据写入结果 [root@Master1 ~]# redis-cli -a 123456 -h 192.168.18.80 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.18.80:6379> dbsize (integer) 3331 192.168.18.80:6379> [root@Master1 ~]# redis-cli -a 123456 -h 192.168.18.81 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.18.81:6379> dbsize (integer) 3340 [root@Master1 ~]# redis-cli -a 123456 -h 192.168.18.82 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.18.82:6379> dbsize (integer) 3329
Redis集群故障转移测试
[root@Master2 ~]# redis-cli -a 123456 cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379@16379 slave 9ff40f467859b3daf016b9270c9f657eef11d3e1 0 1628261594357 6 connected 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379@16379 master - 0 1628261596372 3 connected 10923-16383 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379@16379 slave f361eb219afe76e1d316ea75628d3f8f16e2b8cc 0 1628261595365 5 connected a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379@16379 master - 0 1628261594000 7 connected 0-5460 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379@16379 slave a2488deac6e75c4dee7e7546e55d81394a92de34 0 1628261592342 7 connected f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379@16379 myself,master - 0 1628261593000 2 connected 5461-10922 #手动停止Master2的redis来模拟故障 [root@Master2 ~]# redis-cli -a 123456 shutdown Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. [root@Master2 ~]# ss -ntlp |grep redis [root@Master2 ~]# redis-cli -a 123456 --cluster info 192.168.18.80:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Could not connect to Redis at 192.168.18.81:6379: Connection refused 192.168.18.83:6379 (a2488dea...) -> 3331 keys | 5461 slots | 1 slaves. 192.168.18.84:6379 (767f9c45...) -> 3340 keys | 5462 slots | 0 slaves. #192.168.18.84:6379 已成为新的master 192.168.18.82:6379 (9ff40f46...) -> 3329 keys | 5461 slots | 1 slaves. [OK] 10000 keys in 3 masters. 0.61 keys per slot on average. #检查集群 [root@Master2 ~]# redis-cli -a 123456 --cluster check 192.168.18.80:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Could not connect to Redis at 192.168.18.81:6379: Connection refused 192.168.18.83:6379 (a2488dea...) -> 3331 keys | 5461 slots | 1 slaves. 192.168.18.84:6379 (767f9c45...) -> 3340 keys | 5462 slots | 0 slaves. 192.168.18.82:6379 (9ff40f46...) -> 3329 keys | 5461 slots | 1 slaves. [OK] 10000 keys in 3 masters. 0.61 keys per slot on average. >>> Performing Cluster Check (using node 192.168.18.80:6379) S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379 slots: (0 slots) slave replicates a2488deac6e75c4dee7e7546e55d81394a92de34 M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379 slots: (0 slots) slave replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1 M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379 slots:[5461-10922] (5462 slots) master M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379 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. #启用Master2节点的redis [root@Master2 ~]# systemctl start redis [root@Master2 ~]# ss -ntlp |grep redis LISTEN 0 128 0.0.0.0:6379 0.0.0.0:* users:(("redis-server",pid=7301,fd=6)) LISTEN 0 128 0.0.0.0:16379 0.0.0.0:* users:(("redis-server",pid=7301,fd=8)) [root@Master2 ~]# redis-cli -a 123456 cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379@16379 master - 0 1628263118397 8 connected 5461-10922 f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379@16379 myself,slave 767f9c453c7e5ba27d51a65487428f65ae33d49c 0 1628263115000 2 connected #master2已自动成为slave节点 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379@16379 slave a2488deac6e75c4dee7e7546e55d81394a92de34 0 1628263117389 7 connected 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379@16379 slave 9ff40f467859b3daf016b9270c9f657eef11d3e1 0 1628263116000 6 connected 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379@16379 master - 0 1628263117000 3 connected 10923-16383 a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379@16379 master - 0 1628263117000 7 connected 0-5460
实现redis集群动态扩容
随着公司业务量并发的增长,往往需要对redis集群实现动态扩容。
由于本人物理机内存限制,这里将使用多实例的方式来实现redis cluster的动态扩容。
注意: 生产环境一般建议master节点为奇数个,比如:3,5,7,以防止脑裂现象
#创建redis多实例 [root@Slave3 ~]# mkdir -p /data/redis/638{0,1} [root@Slave3 ~]# cp /etc/redis.conf /data/redis/6380/ [root@Slave3 ~]# cp /etc/redis.conf /data/redis/6381/ #分别修改各实例配置文件 [root@Slave3 ~]# sed -i -e 's#^port 6379#port 6380#' -e 's#^pidfile.*#pidfile /data/redis/6380/redis_6380.pid#' -e 's#^dir.*#dir /data/redis/6380#' -e 's#^cluster-config-file.*#cluster-config-file nodes-6380.conf#' -e 's#^logfile.*#logfile /data/redis/6380/redis-6380.log#' -e 's#^dbfilename.*#dbfilename dump-6380.rdb#' /data/redis/6380/redis.conf [root@Slave3 ~]# sed -i -e 's#^port 6379#port 6381#' -e 's#^pidfile.*#pidfile /data/redis/6381/redis_6381.pid#' -e 's#^dir.*#dir /data/redis/6381#' -e 's#^cluster-config-file.*#cluster-config-file nodes-6381.conf#' -e 's#^logfile.*#logfile /data/redis/6381/redis-6381.log#' -e 's#^dbfilename.*#dbfilename dump-6381.rdb#' /data/redis/6381/redis.conf #启动redis多实例 root@Slave3 ~]# redis-server /data/redis/6380/redis.conf [root@Slave3 ~]# redis-server /data/redis/6381/redis.conf [root@Slave3 ~]# ss -ntlp |grep redis LISTEN 0 128 0.0.0.0:16379 0.0.0.0:* users:(("redis-server",pid=4682,fd=8)) LISTEN 0 128 0.0.0.0:16380 0.0.0.0:* users:(("redis-server",pid=5493,fd=8)) LISTEN 0 128 0.0.0.0:16381 0.0.0.0:* users:(("redis-server",pid=5498,fd=8)) LISTEN 0 128 0.0.0.0:6379 0.0.0.0:* users:(("redis-server",pid=4682,fd=6)) LISTEN 0 128 0.0.0.0:6380 0.0.0.0:* users:(("redis-server",pid=5493,fd=6)) LISTEN 0 128 0.0.0.0:6381 0.0.0.0:* users:(("redis-server",pid=5498,fd=6)) #把redis:6380实例添加到集群中去 [root@Slave3 ~]# redis-cli -a 123456 --cluster add-node 192.168.18.85:6380 192.168.18.80:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Adding node 192.168.18.85:6380 to cluster 192.168.18.80:6379 >>> Performing Cluster Check (using node 192.168.18.80:6379) S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379 slots: (0 slots) slave replicates a2488deac6e75c4dee7e7546e55d81394a92de34 S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379 slots: (0 slots) slave replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379 slots: (0 slots) slave replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1 M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379 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.18.85:6380 to make it join the cluster. [OK] New node added correctly. #查看集群信息 [root@Slave3 ~]# redis-cli -a 123456 --cluster info 192.168.18.85:6380 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.18.85:6380 (fe8397f0...) -> 0 keys | 0 slots | 0 slaves. #可以看到新实例为master节点,但是没有分配slot槽位,所以现在是无法写入数据的 192.168.18.84:6379 (767f9c45...) -> 3340 keys | 5462 slots | 1 slaves. 192.168.18.82:6379 (9ff40f46...) -> 3329 keys | 5461 slots | 1 slaves. 192.168.18.83:6379 (a2488dea...) -> 3331 keys | 5461 slots | 1 slaves. [OK] 10000 keys in 4 masters. 0.61 keys per slot on average. #在新的master上重新分配槽位 新的node节点加到集群之后,默认是master节点,但是没有slots,需要重新分配 添加主机之后需要对添加至集群种的新主机重新分片,否则其没有分片也就无法写入数据。 [root@Slave3 ~]# redis-cli -a 123456 --cluster reshard 192.168.18.85:6380 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing Cluster Check (using node 192.168.18.85:6380) M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380 slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379 slots: (0 slots) slave replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1 S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379 slots: (0 slots) slave replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379 slots:[6827-10922] (4096 slots) master 1 additional replica(s) S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379 slots: (0 slots) slave replicates a2488deac6e75c4dee7e7546e55d81394a92de34 M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379 slots:[1365-5460] (4096 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)? 4096 #输入需要新分配多少个槽位 16384/4 =4096 What is the receiving node ID? fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a #输入新的master的ID Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. #从所有节点中划分出4096个哈希槽分配给新节点 Type 'done' once you entered all the source nodes IDs. #从redis cluster删除某个主机可以使用此方式将指定主机上的槽位全部移动到其他节点 Source node #1: all #输入all即可 Do you want to proceed with the proposed reshard plan (yes/no)? yes #确认分配 ... Moving slot 1359 from 192.168.18.83:6379 to 192.168.18.85:6380: . Moving slot 1360 from 192.168.18.83:6379 to 192.168.18.85:6380: . Moving slot 1361 from 192.168.18.83:6379 to 192.168.18.85:6380: Moving slot 1362 from 192.168.18.83:6379 to 192.168.18.85:6380: Moving slot 1363 from 192.168.18.83:6379 to 192.168.18.85:6380: Moving slot 1364 from 192.168.18.83:6379 to 192.168.18.85:6380: [root@Slave3 ~]# #确定slot槽位分配成功 [root@Slave3 ~]# redis-cli -a 123456 --cluster check 192.168.18.85:6380 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.18.85:6380 (fe8397f0...) -> 2474 keys | 4096 slots | 0 slaves. 192.168.18.84:6379 (767f9c45...) -> 2515 keys | 4096 slots | 1 slaves. 192.168.18.82:6379 (9ff40f46...) -> 2500 keys | 4096 slots | 1 slaves. 192.168.18.83:6379 (a2488dea...) -> 2511 keys | 4096 slots | 1 slaves. [OK] 10000 keys in 4 masters. 0.61 keys per slot on average. >>> Performing Cluster Check (using node 192.168.18.85:6380) M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380 slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379 slots: (0 slots) slave replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1 S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379 slots: (0 slots) slave replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379 slots:[6827-10922] (4096 slots) master 1 additional replica(s) S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379 slots: (0 slots) slave replicates a2488deac6e75c4dee7e7546e55d81394a92de34 M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379 slots:[1365-5460] (4096 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. #确认新master是否有数据 [root@Slave3 ~]# redis-cli -a 123456 -p 6380 dbsize Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. (integer) 2474 #为新的master添加新的slave节点 [root@Slave3 ~]# redis-cli -a 123456 --cluster add-node 192.168.18.85:6381 192.168.18.85:6380 --cluster-slave --cluster-master-id fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Adding node 192.168.18.85:6381 to cluster 192.168.18.85:6380 >>> Performing Cluster Check (using node 192.168.18.85:6380) M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380 slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379 slots: (0 slots) slave replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1 S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379 slots: (0 slots) slave replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379 slots:[6827-10922] (4096 slots) master 1 additional replica(s) S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379 slots: (0 slots) slave replicates a2488deac6e75c4dee7e7546e55d81394a92de34 M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379 slots:[1365-5460] (4096 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.18.85:6381 to make it join the cluster. Waiting for the cluster to join >>> Configure node as replica of 192.168.18.85:6380. [OK] New node added correctly. #查看集群状态 [root@Slave3 ~]# redis-cli -a 123456 cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. f3cf6c95950dbec83ab3a59bf7fd74739c89b774 192.168.18.85:6381@16381 slave fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 0 1628265662757 9 connected 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379@16379 master - 0 1628265663762 3 connected 12288-16383 f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379@16379 slave 767f9c453c7e5ba27d51a65487428f65ae33d49c 0 1628265664770 8 connected fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380@16380 master - 0 1628265662000 9 connected 0-1364 5461-6826 10923-12287 a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379@16379 master - 0 1628265661000 7 connected 1365-5460 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379@16379 myself,slave 9ff40f467859b3daf016b9270c9f657eef11d3e1 0 1628265660000 6 connected 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379@16379 master - 0 1628265662000 8 connected 6827-10922 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379@16379 slave a2488deac6e75c4dee7e7546e55d81394a92de34 0 1628265661750 7 connected
实现redis集群动态缩容
删除节点过程:
添加节点的时候是先添加node节点到集群,然后分配槽位,删除节点的操作与添加节点的操作正好相
反,是先将被删除的Redis node上的槽位迁移到集群中的其他Redis node节点上,然后再将其删除,
如果一个Redis node节点上的槽位没有被完全迁移,删除该node的时候会提示有数据且无法删除。
#将192.168.18.85:6380节点的4096个槽位分别挪到其他三个主节点上 [root@Slave3 ~]# redis-cli -a 123456 --cluster reshard 192.168.18.81:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing Cluster Check (using node 192.168.18.81:6379) S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379 slots: (0 slots) slave replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380 slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master 1 additional replica(s) S: f3cf6c95950dbec83ab3a59bf7fd74739c89b774 192.168.18.85:6381 slots: (0 slots) slave replicates fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379 slots:[6827-10922] (4096 slots) master 1 additional replica(s) S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379 slots: (0 slots) slave replicates a2488deac6e75c4dee7e7546e55d81394a92de34 S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379 slots: (0 slots) slave replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1 M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379 slots:[1365-5460] (4096 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)? 1356 #将4096个槽位分配给其它三个master节点 What is the receiving node ID? 767f9c453c7e5ba27d51a65487428f65ae33d49c #分配给第一个主节点 192.168.18.84:6379 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: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a #输入要删除192.168.18.85:6380节点ID Source node #2: done #输入源节点后键入done ... Moving slot 1350 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 1351 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 1352 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 1353 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 1354 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 1355 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Do you want to proceed with the proposed reshard plan (yes/no)? yes #确认分配 ... Moving slot 1350 from 192.168.18.85:6380 to 192.168.18.84:6379: . Moving slot 1351 from 192.168.18.85:6380 to 192.168.18.84:6379: Moving slot 1352 from 192.168.18.85:6380 to 192.168.18.84:6379: Moving slot 1353 from 192.168.18.85:6380 to 192.168.18.84:6379: Moving slot 1354 from 192.168.18.85:6380 to 192.168.18.84:6379: . Moving slot 1355 from 192.168.18.85:6380 to 192.168.18.84:6379: . [root@Slave3 ~]# redis-cli -a 123456 --cluster reshard 192.168.18.81:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing Cluster Check (using node 192.168.18.81:6379) S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379 slots: (0 slots) slave replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380 slots:[1356-1364],[5461-6826],[10923-12287] (2740 slots) master 1 additional replica(s) S: f3cf6c95950dbec83ab3a59bf7fd74739c89b774 192.168.18.85:6381 slots: (0 slots) slave replicates fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379 slots:[0-1355],[6827-10922] (5452 slots) master 1 additional replica(s) S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379 slots: (0 slots) slave replicates a2488deac6e75c4dee7e7546e55d81394a92de34 S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379 slots: (0 slots) slave replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1 M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379 slots:[12288-16383] (4096 slots) master 1 additional replica(s) M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379 slots:[1365-5460] (4096 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)? 1356 What is the receiving node ID? 9ff40f467859b3daf016b9270c9f657eef11d3e1 #将1356个槽位分配给第二个主节点 192.168.18.82:6379 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: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Source node #2: done ... Moving slot 6801 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 6802 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 6803 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 6804 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 6805 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 6806 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 6807 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Do you want to proceed with the proposed reshard plan (yes/no)? yes ... Moving slot 6802 from 192.168.18.85:6380 to 192.168.18.82:6379: Moving slot 6803 from 192.168.18.85:6380 to 192.168.18.82:6379: .. Moving slot 6804 from 192.168.18.85:6380 to 192.168.18.82:6379: Moving slot 6805 from 192.168.18.85:6380 to 192.168.18.82:6379: Moving slot 6806 from 192.168.18.85:6380 to 192.168.18.82:6379: Moving slot 6807 from 192.168.18.85:6380 to 192.168.18.82:6379: [root@Slave3 ~]# redis-cli -a 123456 --cluster reshard 192.168.18.81:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing Cluster Check (using node 192.168.18.81:6379) S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379 slots: (0 slots) slave replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380 slots:[6808-6826],[10923-12287] (1384 slots) master 1 additional replica(s) S: f3cf6c95950dbec83ab3a59bf7fd74739c89b774 192.168.18.85:6381 slots: (0 slots) slave replicates fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379 slots:[0-1355],[6827-10922] (5452 slots) master 1 additional replica(s) S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379 slots: (0 slots) slave replicates a2488deac6e75c4dee7e7546e55d81394a92de34 S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379 slots: (0 slots) slave replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1 M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379 slots:[1356-1364],[5461-6807],[12288-16383] (5452 slots) master 1 additional replica(s) M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379 slots:[1365-5460] (4096 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)? 1384 What is the receiving node ID? a2488deac6e75c4dee7e7546e55d81394a92de34 #将剩下的1384个槽位分配给第三个主节点 192.168.18.83:6379 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: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Source node #2: done ... Moving slot 12254 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 12255 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 12256 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 12257 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 12258 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Moving slot 12259 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Do you want to proceed with the proposed reshard plan (yes/no)? yes ... Moving slot 12255 from 192.168.18.85:6380 to 192.168.18.83:6379: Moving slot 12256 from 192.168.18.85:6380 to 192.168.18.83:6379: . Moving slot 12257 from 192.168.18.85:6380 to 192.168.18.83:6379: Moving slot 12258 from 192.168.18.85:6380 to 192.168.18.83:6379: Moving slot 12259 from 192.168.18.85:6380 to 192.168.18.83:6379: [root@Slave2 ~]# redis-cli -a 123456 --cluster check 192.168.18.81:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.18.85:6380 (fe8397f0...) -> 0 keys | 0 slots | 0 slaves. 192.168.18.84:6379 (767f9c45...) -> 3331 keys | 5452 slots | 1 slaves. 192.168.18.82:6379 (9ff40f46...) -> 3316 keys | 5452 slots | 1 slaves. 192.168.18.83:6379 (a2488dea...) -> 3353 keys | 5480 slots | 2 slaves. [OK] 10000 keys in 4 masters. 0.61 keys per slot on average. >>> Performing Cluster Check (using node 192.168.18.81:6379) S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379 slots: (0 slots) slave replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380 slots: (0 slots) master S: f3cf6c95950dbec83ab3a59bf7fd74739c89b774 192.168.18.85:6381 slots: (0 slots) slave replicates a2488deac6e75c4dee7e7546e55d81394a92de34 M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379 slots:[0-1355],[6827-10922] (5452 slots) master 1 additional replica(s) S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379 slots: (0 slots) slave replicates a2488deac6e75c4dee7e7546e55d81394a92de34 S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379 slots: (0 slots) slave replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1 M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379 slots:[1356-1364],[5461-6807],[12288-16383] (5452 slots) master 1 additional replica(s) M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379 slots:[1365-5460],[6808-6826],[10923-12287] (5480 slots) master 2 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. #192.168.18.85:6381已自动成为192.168.18.83:6379的slave [root@Slave2 ~]# redis-cli -a 123456 -h 192.168.18.83 info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=192.168.18.80,port=6379,state=online,offset=253994,lag=0 slave1:ip=192.168.18.85,port=6381,state=online,offset=253994,lag=1 master_replid:42a58a2dda54c0b602f3022fb4015defce8f5182 master_replid2:f2f7b980ff32bde88c17cf822092a3868d04e5b6 master_repl_offset:253994 second_repl_offset:4453 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:253994 #将192.168.18.85:6380从集群中移除 [root@Slave3 ~]# redis-cli -a 123456 --cluster del-node 192.168.18.85:6380 fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Removing node fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a from cluster 192.168.18.85:6380 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node. #删除节点后,redis进程自动关闭 #删除多余的slave从节点 [root@Slave3 ~]# redis-cli -a 123456 --cluster del-node 192.168.18.85:6381 f3cf6c95950dbec83ab3a59bf7fd74739c89b774 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Removing node f3cf6c95950dbec83ab3a59bf7fd74739c89b774 from cluster 192.168.18.85:6381 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node. #节点删除成功 [root@Slave2 ~]# redis-cli -a 123456 --cluster check 192.168.18.81:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.18.84:6379 (767f9c45...) -> 3331 keys | 5452 slots | 1 slaves. 192.168.18.82:6379 (9ff40f46...) -> 3316 keys | 5452 slots | 1 slaves. 192.168.18.83:6379 (a2488dea...) -> 3353 keys | 5480 slots | 1 slaves. [OK] 10000 keys in 3 masters. 0.61 keys per slot on average. >>> Performing Cluster Check (using node 192.168.18.81:6379) S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379 slots: (0 slots) slave replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379 slots:[0-1355],[6827-10922] (5452 slots) master 1 additional replica(s) S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379 slots: (0 slots) slave replicates a2488deac6e75c4dee7e7546e55d81394a92de34 S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379 slots: (0 slots) slave replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1 M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379 slots:[1356-1364],[5461-6807],[12288-16383] (5452 slots) master 1 additional replica(s) M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379 slots:[1365-5460],[6808-6826],[10923-12287] (5480 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. [root@Slave2 ~]# redis-cli -a 123456 --cluster info 192.168.18.81:6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.18.84:6379 (767f9c45...) -> 3331 keys | 5452 slots | 1 slaves. 192.168.18.82:6379 (9ff40f46...) -> 3316 keys | 5452 slots | 1 slaves. 192.168.18.83:6379 (a2488dea...) -> 3353 keys | 5480 slots | 1 slaves. [OK] 10000 keys in 3 masters. 0.61 keys per slot on average.
这篇关于Redis集群实现动态扩缩容的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-07Redis高并发入门详解
- 2024-12-07Redis缓存入门:新手必读指南
- 2024-12-07Redis缓存入门:新手必读教程
- 2024-12-07Redis入门:新手必备的简单教程
- 2024-12-07Redis入门:新手必读的简单教程
- 2024-12-06Redis入门教程:从安装到基本操作
- 2024-12-06Redis缓存入门教程:轻松掌握缓存技巧
- 2024-12-04Redis入门:简单教程详解
- 2024-11-29Redis开发入门教程:从零开始学习Redis
- 2024-11-27Redis入门指南:快速掌握Redis基础操作