it编程 > 数据库 > Redis

Redis集群扩容的实现示例

15人参与 2025-02-27 Redis

在虚拟机上新建两个6387,6388服务配置文件,新建之后启动

[root@redis-cluster1 cluster]# cat rediscluster6387.conf 
bind 0.0.0.0
daemonize yes
protected-mode no
logfile "/myredis/cluster/cluster6387.log"
pidfile /myredis/cluster6387.pid
dir /myredis/cluster
dbfilename dump6387.rdb
appendonly yes
appendfilename "appendonly6387.aof"
requirepass 111111
masterauth 111111
port 6387
cluster-enabled yes
cluster-config-file nodes-6387.conf
cluster-node-timeout 5000

[root@redis-cluster1 cluster]# cat rediscluster6388.conf 
bind 0.0.0.0
daemonize yes
protected-mode no
logfile "/myredis/cluster/cluster6388.log"
pidfile /myredis/cluster6388.pid
dir /myredis/cluster
dbfilename dump6388.rdb
appendonly yes
appendfilename "appendonly6388.aof"
requirepass 111111
masterauth 111111
port 6388
cluster-enabled yes
cluster-config-file nodes-6388.conf
cluster-node-timeout 5000

启动87,88两个服务,此时他们都是master,没有加入集群

[root@redis-cluster1 cluster]# redis-server rediscluster6387.conf 
[root@redis-cluster1 cluster]# redis-server rediscluster6388.conf 
[root@redis-cluster1 cluster]# ps -ef | grep redis
root        2038       1  0 15:31 ?        00:00:31 redis-server 0.0.0.0:6386 [cluster]
root        2221       1  0 15:47 ?        00:00:24 redis-server 0.0.0.0:6385 [cluster]
root        2327    1753  0 16:01 pts/0    00:00:00 redis-cli -a 111111 -p 6385
root        2472       1  0 16:44 ?        00:00:00 redis-server 0.0.0.0:6387 [cluster]
root        2479       1  0 16:44 ?        00:00:00 redis-server 0.0.0.0:6388 [cluster]
root        2486    1954  0 16:44 pts/1    00:00:00 grep --color=auto redis
[root@redis-cluster1 cluster]# redis-cli -a 111111 -p 6387
warning: using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6387> 
127.0.0.1:6387> 
127.0.0.1:6387> cluster nodes
494e60980fac86000312e544f71c0e835bc1445b :6387@16387 myself,master - 0 0 0 connected
127.0.0.1:6387> 

将新增的6387节点作为master加入原集群

将新增的6387作为master节点加入原有集群
redis-cli -a 密码 --cluster add-node,自己实际ip地址:6387 自己实际ip地址:6381
6387 就是将要作为master新增节点
6381 就是原来集群节点里面的领路人,相当于6387拜拜6381的码头从而找到组织加入集群redis-cli -a 111111 --cluster add-node 192.168.111.174:6387 192.168.111.175:6381

[root@redis-cluster1 cluster]# redis-cli -a 111111  --cluster add-node 192.168.58.212:6387 192.168.58.129:6381
warning: using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> adding node 192.168.58.212:6387 to cluster 192.168.58.129:6381
>>> performing cluster check (using node 192.168.58.129:6381)
m: ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
s: fedf4b13be2122f44dde749a08c4b943582b3566 192.168.58.129:6382
   slots: (0 slots) slave
   replicates be191621d4aa881d864a113c385d58b751753392
s: 91cf395e8f04a5df1aeec4602dd38b3e4a708d55 192.168.58.130:6384
   slots: (0 slots) slave
   replicates ba40235fdad71fb36c3bf165ea51055825d453c2
m: 0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
m: be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
s: 30e5bdec200a671e4de666551d4489f0035235ba 192.168.58.212:6386
   slots: (0 slots) slave
   replicates 0a820807a70885c1d87d141824d3869dcf2418ee
[ok] all nodes agree about slots configuration.
>>> check for open slots...
>>> check slots coverage...
[ok] all 16384 slots covered.
>>> getting functions from cluster
>>> send function list to 192.168.58.212:6387 to verify there is no functions in it
>>> send function restore to 192.168.58.212:6387
>>> send cluster meet to node 192.168.58.212:6387 to make it join the cluster.
[ok] new node added correctly.

第一次检查集群状态:

6387还没有被分配槽位

[root@redis-cluster1 cluster]# redis-cli -a 111111 --cluster check 192.168.58.129:6381
warning: using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.58.129:6381 (ba40235f...) -> 0 keys | 5461 slots | 1 slaves.
192.168.58.130:6383 (0a820807...) -> 0 keys | 5462 slots | 1 slaves.
192.168.58.212:6385 (be191621...) -> 0 keys | 5461 slots | 1 slaves.
192.168.58.212:6387 (494e6098...) -> 0 keys | 0 slots | 0 slaves.
[ok] 0 keys in 4 masters.
0.00 keys per slot on average.
>>> performing cluster check (using node 192.168.58.129:6381)
m: ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
s: fedf4b13be2122f44dde749a08c4b943582b3566 192.168.58.129:6382
   slots: (0 slots) slave
   replicates be191621d4aa881d864a113c385d58b751753392
s: 91cf395e8f04a5df1aeec4602dd38b3e4a708d55 192.168.58.130:6384
   slots: (0 slots) slave
   replicates ba40235fdad71fb36c3bf165ea51055825d453c2
m: 0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
m: be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
s: 30e5bdec200a671e4de666551d4489f0035235ba 192.168.58.212:6386
   slots: (0 slots) slave
   replicates 0a820807a70885c1d87d141824d3869dcf2418ee
m: 494e60980fac86000312e544f71c0e835bc1445b 192.168.58.212:6387
   slots: (0 slots) master
[ok] all nodes agree about slots configuration.
>>> check for open slots...
>>> check slots coverage...
[ok] all 16384 slots covered.

重新分配槽号

16384处于4,因为现在有四个主机;

填写6387的id号;

[root@redis-cluster1 cluster]# redis-cli -a 111111 --cluster reshard 192.168.58.129:6381
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.58.129:6381)
m: ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
s: fedf4b13be2122f44dde749a08c4b943582b3566 192.168.58.129:6382
   slots: (0 slots) slave
   replicates be191621d4aa881d864a113c385d58b751753392
s: 91cf395e8f04a5df1aeec4602dd38b3e4a708d55 192.168.58.130:6384
   slots: (0 slots) slave
   replicates ba40235fdad71fb36c3bf165ea51055825d453c2
m: 0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
m: be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
s: 30e5bdec200a671e4de666551d4489f0035235ba 192.168.58.212:6386
   slots: (0 slots) slave
   replicates 0a820807a70885c1d87d141824d3869dcf2418ee
m: 494e60980fac86000312e544f71c0e835bc1445b 192.168.58.212:6387
   slots: (0 slots) master
[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? 494e60980fac86000312e544f71c0e835bc1445b
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: all

ready to move 4096 slots.
  source nodes:
    m: ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    m: 0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    m: be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
  destination node:
    m: 494e60980fac86000312e544f71c0e835bc1445b 192.168.58.212:6387
       slots: (0 slots) master
  resharding plan:

再次查看集群状态

四个主节点均已被分配槽位;

当前状态为三主三从,一主零从

[root@redis-cluster1 cluster]# redis-cli -a 111111 --cluster check 192.168.58.129:6381
warning: using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.58.129:6381 (ba40235f...) -> 0 keys | 4096 slots | 1 slaves.
192.168.58.130:6383 (0a820807...) -> 0 keys | 4096 slots | 1 slaves.
192.168.58.212:6385 (be191621...) -> 0 keys | 4096 slots | 1 slaves.
192.168.58.212:6387 (494e6098...) -> 0 keys | 4096 slots | 0 slaves.
[ok] 0 keys in 4 masters.
0.00 keys per slot on average.
>>> performing cluster check (using node 192.168.58.129:6381)
m: ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
s: fedf4b13be2122f44dde749a08c4b943582b3566 192.168.58.129:6382
   slots: (0 slots) slave
   replicates be191621d4aa881d864a113c385d58b751753392
s: 91cf395e8f04a5df1aeec4602dd38b3e4a708d55 192.168.58.130:6384
   slots: (0 slots) slave
   replicates ba40235fdad71fb36c3bf165ea51055825d453c2
m: 0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
m: be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
s: 30e5bdec200a671e4de666551d4489f0035235ba 192.168.58.212:6386
   slots: (0 slots) slave
   replicates 0a820807a70885c1d87d141824d3869dcf2418ee
m: 494e60980fac86000312e544f71c0e835bc1445b 192.168.58.212:6387
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
[ok] all nodes agree about slots configuration.
>>> check for open slots...
>>> check slots coverage...
[ok] all 16384 slots covered.

 为主节点87分配从节点88

[root@redis-cluster1 cluster]# redis-cli  -a 111111 --cluster add-node 192.168.58.212:6388 192.168.58.212:6387 --cluster-slave --cluster-master-id 494e60980fac86000312e544f71c0e835bc1445b 

--cluster-master-id 494e60980fac86000312e544f71c0e835bc1445b :主节点id号

再次查看当前的集群状态:

127.0.0.1:6385> cluster nodes
0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383@16383 master - 0 1733303488000 3 connected 6827-10922
91cf395e8f04a5df1aeec4602dd38b3e4a708d55 192.168.58.130:6384@16384 slave ba40235fdad71fb36c3bf165ea51055825d453c2 0 1733303487073 1 connected
be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385@16385 myself,master - 0 0 8 connected 12288-16383
30e5bdec200a671e4de666551d4489f0035235ba 192.168.58.212:6386@16386 slave 0a820807a70885c1d87d141824d3869dcf2418ee 0 1733303488559 3 connected
ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381@16381 master - 0 1733303488000 1 connected 1365-5460
c26c4f5ec9882083544326df9249fa48e35c6a2a 192.168.58.212:6388@16388 slave 494e60980fac86000312e544f71c0e835bc1445b 0 1733303488345 9 connected
494e60980fac86000312e544f71c0e835bc1445b 192.168.58.212:6387@16387 master - 0 1733303488675 9 connected 0-1364 5461-6826 10923-12287
fedf4b13be2122f44dde749a08c4b943582b3566 192.168.58.129:6382@16382 slave be191621d4aa881d864a113c385d58b751753392 0 1733303488106 8 connected
127.0.0.1:6385> 

四主四从,集群扩容完成!

[root@redis-cluster1 cluster]# redis-cli -a 111111 --cluster check 192.168.58.129:6381
warning: using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.58.129:6381 (ba40235f...) -> 0 keys | 4096 slots | 1 slaves.
192.168.58.212:6387 (494e6098...) -> 0 keys | 4096 slots | 1 slaves.
192.168.58.130:6383 (0a820807...) -> 0 keys | 4096 slots | 1 slaves.
192.168.58.212:6385 (be191621...) -> 0 keys | 4096 slots | 1 slaves.
[ok] 0 keys in 4 masters.
0.00 keys per slot on average.
>>> performing cluster check (using node 192.168.58.129:6381)
m: ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
s: 30e5bdec200a671e4de666551d4489f0035235ba 192.168.58.212:6386
   slots: (0 slots) slave
   replicates 0a820807a70885c1d87d141824d3869dcf2418ee
m: 494e60980fac86000312e544f71c0e835bc1445b 192.168.58.212:6387
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
s: fedf4b13be2122f44dde749a08c4b943582b3566 192.168.58.129:6382
   slots: (0 slots) slave
   replicates be191621d4aa881d864a113c385d58b751753392
m: 0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
s: 91cf395e8f04a5df1aeec4602dd38b3e4a708d55 192.168.58.130:6384
   slots: (0 slots) slave
   replicates ba40235fdad71fb36c3bf165ea51055825d453c2
m: be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
s: c26c4f5ec9882083544326df9249fa48e35c6a2a 192.168.58.212:6388
   slots: (0 slots) slave
   replicates 494e60980fac86000312e544f71c0e835bc1445b
[ok] all nodes agree about slots configuration.
>>> check for open slots...
>>> check slots coverage...
[ok] all 16384 slots covered.

到此这篇关于redis集群扩容的实现示例的文章就介绍到这了,更多相关redis集群扩容内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

Redis高阶之容错切换的实现

02-27

配置redis.conf远程访问的操作

02-28

Redis 缓存双写一致性的解决方案

02-28

深入理解Redis BigKey

02-28

Redis实现集群搭建+集群读写的示例

02-28

redis restore 命令的用法实例详解

02-28

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论