• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    Redis集群扩容的实现示例

    发布者: 涵韵3588 | 发布时间: 2025-6-19 12:43| 查看数: 74| 评论数: 0|帖子模式

    在虚拟机上新建两个6387,6388服务配置文件,新建之后启动
    1. [root@redis-cluster1 cluster]# cat redisCluster6387.conf
    2. bind 0.0.0.0
    3. daemonize yes
    4. protected-mode no
    5. logfile "/myredis/cluster/cluster6387.log"
    6. pidfile /myredis/cluster6387.pid
    7. dir /myredis/cluster
    8. dbfilename dump6387.rdb
    9. appendonly yes
    10. appendfilename "appendonly6387.aof"
    11. requirepass 111111
    12. masterauth 111111
    13. port 6387
    14. cluster-enabled yes
    15. cluster-config-file nodes-6387.conf
    16. cluster-node-timeout 5000

    17. [root@redis-cluster1 cluster]# cat redisCluster6388.conf
    18. bind 0.0.0.0
    19. daemonize yes
    20. protected-mode no
    21. logfile "/myredis/cluster/cluster6388.log"
    22. pidfile /myredis/cluster6388.pid
    23. dir /myredis/cluster
    24. dbfilename dump6388.rdb
    25. appendonly yes
    26. appendfilename "appendonly6388.aof"
    27. requirepass 111111
    28. masterauth 111111
    29. port 6388
    30. cluster-enabled yes
    31. cluster-config-file nodes-6388.conf
    32. cluster-node-timeout 5000
    复制代码
    启动87,88两个服务,此时他们都是master,没有加入集群
    1. [root@redis-cluster1 cluster]# redis-server redisCluster6387.conf
    2. [root@redis-cluster1 cluster]# redis-server redisCluster6388.conf
    3. [root@redis-cluster1 cluster]# ps -ef | grep redis
    4. root        2038       1  0 15:31 ?        00:00:31 redis-server 0.0.0.0:6386 [cluster]
    5. root        2221       1  0 15:47 ?        00:00:24 redis-server 0.0.0.0:6385 [cluster]
    6. root        2327    1753  0 16:01 pts/0    00:00:00 redis-cli -a 111111 -p 6385
    7. root        2472       1  0 16:44 ?        00:00:00 redis-server 0.0.0.0:6387 [cluster]
    8. root        2479       1  0 16:44 ?        00:00:00 redis-server 0.0.0.0:6388 [cluster]
    9. root        2486    1954  0 16:44 pts/1    00:00:00 grep --color=auto redis
    10. [root@redis-cluster1 cluster]# redis-cli -a 111111 -p 6387
    11. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    12. 127.0.0.1:6387>
    13. 127.0.0.1:6387>
    14. 127.0.0.1:6387> cluster nodes
    15. 494e60980fac86000312e544f71c0e835bc1445b :6387@16387 myself,master - 0 0 0 connected
    16. 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
    1. [root@redis-cluster1 cluster]# redis-cli -a 111111  --cluster add-node 192.168.58.212:6387 192.168.58.129:6381
    2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    3. >>> Adding node 192.168.58.212:6387 to cluster 192.168.58.129:6381
    4. >>> Performing Cluster Check (using node 192.168.58.129:6381)
    5. M: ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381
    6.    slots:[0-5460] (5461 slots) master
    7.    1 additional replica(s)
    8. S: fedf4b13be2122f44dde749a08c4b943582b3566 192.168.58.129:6382
    9.    slots: (0 slots) slave
    10.    replicates be191621d4aa881d864a113c385d58b751753392
    11. S: 91cf395e8f04a5df1aeec4602dd38b3e4a708d55 192.168.58.130:6384
    12.    slots: (0 slots) slave
    13.    replicates ba40235fdad71fb36c3bf165ea51055825d453c2
    14. M: 0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383
    15.    slots:[5461-10922] (5462 slots) master
    16.    1 additional replica(s)
    17. M: be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385
    18.    slots:[10923-16383] (5461 slots) master
    19.    1 additional replica(s)
    20. S: 30e5bdec200a671e4de666551d4489f0035235ba 192.168.58.212:6386
    21.    slots: (0 slots) slave
    22.    replicates 0a820807a70885c1d87d141824d3869dcf2418ee
    23. [OK] All nodes agree about slots configuration.
    24. >>> Check for open slots...
    25. >>> Check slots coverage...
    26. [OK] All 16384 slots covered.
    27. >>> Getting functions from cluster
    28. >>> Send FUNCTION LIST to 192.168.58.212:6387 to verify there is no functions in it
    29. >>> Send FUNCTION RESTORE to 192.168.58.212:6387
    30. >>> Send CLUSTER MEET to node 192.168.58.212:6387 to make it join the cluster.
    31. [OK] New node added correctly.
    复制代码
    第一次检查集群状态:

    6387还没有被分配槽位
    1. [root@redis-cluster1 cluster]# redis-cli -a 111111 --cluster check 192.168.58.129:6381
    2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    3. 192.168.58.129:6381 (ba40235f...) -> 0 keys | 5461 slots | 1 slaves.
    4. 192.168.58.130:6383 (0a820807...) -> 0 keys | 5462 slots | 1 slaves.
    5. 192.168.58.212:6385 (be191621...) -> 0 keys | 5461 slots | 1 slaves.
    6. 192.168.58.212:6387 (494e6098...) -> 0 keys | 0 slots | 0 slaves.
    7. [OK] 0 keys in 4 masters.
    8. 0.00 keys per slot on average.
    9. >>> Performing Cluster Check (using node 192.168.58.129:6381)
    10. M: ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381
    11.    slots:[0-5460] (5461 slots) master
    12.    1 additional replica(s)
    13. S: fedf4b13be2122f44dde749a08c4b943582b3566 192.168.58.129:6382
    14.    slots: (0 slots) slave
    15.    replicates be191621d4aa881d864a113c385d58b751753392
    16. S: 91cf395e8f04a5df1aeec4602dd38b3e4a708d55 192.168.58.130:6384
    17.    slots: (0 slots) slave
    18.    replicates ba40235fdad71fb36c3bf165ea51055825d453c2
    19. M: 0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383
    20.    slots:[5461-10922] (5462 slots) master
    21.    1 additional replica(s)
    22. M: be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385
    23.    slots:[10923-16383] (5461 slots) master
    24.    1 additional replica(s)
    25. S: 30e5bdec200a671e4de666551d4489f0035235ba 192.168.58.212:6386
    26.    slots: (0 slots) slave
    27.    replicates 0a820807a70885c1d87d141824d3869dcf2418ee
    28. M: 494e60980fac86000312e544f71c0e835bc1445b 192.168.58.212:6387
    29.    slots: (0 slots) master
    30. [OK] All nodes agree about slots configuration.
    31. >>> Check for open slots...
    32. >>> Check slots coverage...
    33. [OK] All 16384 slots covered.
    复制代码
    重新分配槽号

    16384处于4,因为现在有四个主机;
    填写6387的id号;
    1. [root@redis-cluster1 cluster]# redis-cli -a 111111 --cluster reshard 192.168.58.129:6381
    2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    3. >>> Performing Cluster Check (using node 192.168.58.129:6381)
    4. M: ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381
    5.    slots:[0-5460] (5461 slots) master
    6.    1 additional replica(s)
    7. S: fedf4b13be2122f44dde749a08c4b943582b3566 192.168.58.129:6382
    8.    slots: (0 slots) slave
    9.    replicates be191621d4aa881d864a113c385d58b751753392
    10. S: 91cf395e8f04a5df1aeec4602dd38b3e4a708d55 192.168.58.130:6384
    11.    slots: (0 slots) slave
    12.    replicates ba40235fdad71fb36c3bf165ea51055825d453c2
    13. M: 0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383
    14.    slots:[5461-10922] (5462 slots) master
    15.    1 additional replica(s)
    16. M: be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385
    17.    slots:[10923-16383] (5461 slots) master
    18.    1 additional replica(s)
    19. S: 30e5bdec200a671e4de666551d4489f0035235ba 192.168.58.212:6386
    20.    slots: (0 slots) slave
    21.    replicates 0a820807a70885c1d87d141824d3869dcf2418ee
    22. M: 494e60980fac86000312e544f71c0e835bc1445b 192.168.58.212:6387
    23.    slots: (0 slots) master
    24. [OK] All nodes agree about slots configuration.
    25. >>> Check for open slots...
    26. >>> Check slots coverage...
    27. [OK] All 16384 slots covered.
    28. How many slots do you want to move (from 1 to 16384)? 4096
    29. What is the receiving node ID? 494e60980fac86000312e544f71c0e835bc1445b
    30. Please enter all the source node IDs.
    31.   Type 'all' to use all the nodes as source nodes for the hash slots.
    32.   Type 'done' once you entered all the source nodes IDs.
    33. Source node #1: all

    34. Ready to move 4096 slots.
    35.   Source nodes:
    36.     M: ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381
    37.        slots:[0-5460] (5461 slots) master
    38.        1 additional replica(s)
    39.     M: 0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383
    40.        slots:[5461-10922] (5462 slots) master
    41.        1 additional replica(s)
    42.     M: be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385
    43.        slots:[10923-16383] (5461 slots) master
    44.        1 additional replica(s)
    45.   Destination node:
    46.     M: 494e60980fac86000312e544f71c0e835bc1445b 192.168.58.212:6387
    47.        slots: (0 slots) master
    48.   Resharding plan:
    复制代码
    再次查看集群状态

    四个主节点均已被分配槽位;
    当前状态为三主三从,一主零从
    1. [root@redis-cluster1 cluster]# redis-cli -a 111111 --cluster check 192.168.58.129:6381
    2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    3. 192.168.58.129:6381 (ba40235f...) -> 0 keys | 4096 slots | 1 slaves.
    4. 192.168.58.130:6383 (0a820807...) -> 0 keys | 4096 slots | 1 slaves.
    5. 192.168.58.212:6385 (be191621...) -> 0 keys | 4096 slots | 1 slaves.
    6. 192.168.58.212:6387 (494e6098...) -> 0 keys | 4096 slots | 0 slaves.
    7. [OK] 0 keys in 4 masters.
    8. 0.00 keys per slot on average.
    9. >>> Performing Cluster Check (using node 192.168.58.129:6381)
    10. M: ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381
    11.    slots:[1365-5460] (4096 slots) master
    12.    1 additional replica(s)
    13. S: fedf4b13be2122f44dde749a08c4b943582b3566 192.168.58.129:6382
    14.    slots: (0 slots) slave
    15.    replicates be191621d4aa881d864a113c385d58b751753392
    16. S: 91cf395e8f04a5df1aeec4602dd38b3e4a708d55 192.168.58.130:6384
    17.    slots: (0 slots) slave
    18.    replicates ba40235fdad71fb36c3bf165ea51055825d453c2
    19. M: 0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383
    20.    slots:[6827-10922] (4096 slots) master
    21.    1 additional replica(s)
    22. M: be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385
    23.    slots:[12288-16383] (4096 slots) master
    24.    1 additional replica(s)
    25. S: 30e5bdec200a671e4de666551d4489f0035235ba 192.168.58.212:6386
    26.    slots: (0 slots) slave
    27.    replicates 0a820807a70885c1d87d141824d3869dcf2418ee
    28. M: 494e60980fac86000312e544f71c0e835bc1445b 192.168.58.212:6387
    29.    slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
    30. [OK] All nodes agree about slots configuration.
    31. >>> Check for open slots...
    32. >>> Check slots coverage...
    33. [OK] All 16384 slots covered.
    复制代码
    为主节点87分配从节点88
    1. [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号
    再次查看当前的集群状态:
    1. 127.0.0.1:6385> cluster nodes
    2. 0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383@16383 master - 0 1733303488000 3 connected 6827-10922
    3. 91cf395e8f04a5df1aeec4602dd38b3e4a708d55 192.168.58.130:6384@16384 slave ba40235fdad71fb36c3bf165ea51055825d453c2 0 1733303487073 1 connected
    4. be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385@16385 myself,master - 0 0 8 connected 12288-16383
    5. 30e5bdec200a671e4de666551d4489f0035235ba 192.168.58.212:6386@16386 slave 0a820807a70885c1d87d141824d3869dcf2418ee 0 1733303488559 3 connected
    6. ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381@16381 master - 0 1733303488000 1 connected 1365-5460
    7. c26c4f5ec9882083544326df9249fa48e35c6a2a 192.168.58.212:6388@16388 slave 494e60980fac86000312e544f71c0e835bc1445b 0 1733303488345 9 connected
    8. 494e60980fac86000312e544f71c0e835bc1445b 192.168.58.212:6387@16387 master - 0 1733303488675 9 connected 0-1364 5461-6826 10923-12287
    9. fedf4b13be2122f44dde749a08c4b943582b3566 192.168.58.129:6382@16382 slave be191621d4aa881d864a113c385d58b751753392 0 1733303488106 8 connected
    10. 127.0.0.1:6385>
    复制代码
    四主四从,集群扩容完成!
    1. [root@redis-cluster1 cluster]# redis-cli -a 111111 --cluster check 192.168.58.129:6381
    2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    3. 192.168.58.129:6381 (ba40235f...) -> 0 keys | 4096 slots | 1 slaves.
    4. 192.168.58.212:6387 (494e6098...) -> 0 keys | 4096 slots | 1 slaves.
    5. 192.168.58.130:6383 (0a820807...) -> 0 keys | 4096 slots | 1 slaves.
    6. 192.168.58.212:6385 (be191621...) -> 0 keys | 4096 slots | 1 slaves.
    7. [OK] 0 keys in 4 masters.
    8. 0.00 keys per slot on average.
    9. >>> Performing Cluster Check (using node 192.168.58.129:6381)
    10. M: ba40235fdad71fb36c3bf165ea51055825d453c2 192.168.58.129:6381
    11.    slots:[1365-5460] (4096 slots) master
    12.    1 additional replica(s)
    13. S: 30e5bdec200a671e4de666551d4489f0035235ba 192.168.58.212:6386
    14.    slots: (0 slots) slave
    15.    replicates 0a820807a70885c1d87d141824d3869dcf2418ee
    16. M: 494e60980fac86000312e544f71c0e835bc1445b 192.168.58.212:6387
    17.    slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
    18.    1 additional replica(s)
    19. S: fedf4b13be2122f44dde749a08c4b943582b3566 192.168.58.129:6382
    20.    slots: (0 slots) slave
    21.    replicates be191621d4aa881d864a113c385d58b751753392
    22. M: 0a820807a70885c1d87d141824d3869dcf2418ee 192.168.58.130:6383
    23.    slots:[6827-10922] (4096 slots) master
    24.    1 additional replica(s)
    25. S: 91cf395e8f04a5df1aeec4602dd38b3e4a708d55 192.168.58.130:6384
    26.    slots: (0 slots) slave
    27.    replicates ba40235fdad71fb36c3bf165ea51055825d453c2
    28. M: be191621d4aa881d864a113c385d58b751753392 192.168.58.212:6385
    29.    slots:[12288-16383] (4096 slots) master
    30.    1 additional replica(s)
    31. S: c26c4f5ec9882083544326df9249fa48e35c6a2a 192.168.58.212:6388
    32.    slots: (0 slots) slave
    33.    replicates 494e60980fac86000312e544f71c0e835bc1445b
    34. [OK] All nodes agree about slots configuration.
    35. >>> Check for open slots...
    36. >>> Check slots coverage...
    37. [OK] All 16384 slots covered.
    复制代码
    到此这篇关于Redis集群扩容的实现示例的文章就介绍到这了,更多相关Redis集群扩容内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/database/3366640sb.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    浏览过的版块

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表