【深入学习Redis丨第二篇】Redis集群摆设详解

打印 上一主题 下一主题

主题 654|帖子 654|积分 1962


Redis集群摆设

1 Redis各节点摆设
利用源码安装各节点,不过与非cluster方式差别的是,配置文件中需启动cluster相关的配置。
因本次为伪分布式摆设,生产情况摆设时建议至少3台机器摆设(此中每台机器1主1从)


ipport192.168.56.1017000192.168.56.1017001192.168.56.1017002192.168.56.1017003192.168.56.1017004192.168.56.1017005 1.1 启动cluster各节点
创建数据目次
  1. mkdir -p  /data/redis/cluster/{7000,7001,7002,7003,7004,7005}
复制代码
配置文件中紧张修改如下内容,其他的可按需调整,也可保持默认值,各节点中注意修改对应的端标语
  1. bind 192.168.56.101
  2. port 7000
  3. daemonize yes
  4. pidfile /data/redis/cluster/7000/redis_7000.pid
  5. logfile "/data/redis/cluster/7000/redis_7000.log"
  6. appendonly yes
  7. appendfilename "appendonly.aof"
  8. appendfsync everysec
  9. cluster-enabled yes
  10. cluster-config-file nodes-7000.conf     #注意此文件自动生成,且初始化时不要有和此重名的文件
  11. cluster-node-timeout 5000
  12. cluster-slave-validity-factor 10
  13. cluster-migration-barrier 1
  14. cluster-require-full-coverage yes
  15. cluster-slave-no-failover no
复制代码
启动各节点,建议用redis用户启动
  1. useradd redis
  2. chown -R  redis:redis  /data/redis/
  3. su - redis
  4. cd /data/redis/cluster/7001
  5. cp /data/redis/cluster/7000/redis.conf .
  6. sed -i "s#7000#7001#g" redis.conf
  7. redis-server redis.conf
复制代码
其他节点和7001雷同启动,启动后进程中会标志redis节点以cluster模式启动

2. 按照依赖
因redis5之前版本前cluster安装依赖ruby,且版本要求比较苛刻,本次安装的版本redis4.0.14,依赖的ruby版本为>=ruby2.4,因此各人安装时可以安装高版本的ruby,本次利用的是ruby2.7.5版本
2.1 编译安装ruby
下载ruby,建议从官网下载源码进行编译安装
https://www.ruby-lang.org/en/downloads/
  1. tar -zxvf  ruby-2.7.5.tar.gz
  2. cd ruby-2.7.5
  3. ./configure
  4. make  
  5. make install
复制代码
安装完毕后,检查ruby以及gem版本
2.2 安装openssl-devel及zlib-devel
安装完ruby后,利用gem安装redis包,此时如果没有安装openssl 则回报如下错误
  1. gem install  redis
  2. ERROR:  Loading command: install (LoadError)
  3.     cannot load such file -- openssl
  4. ERROR:  While executing gem ... (NoMethodError)
  5.     undefined method `invoke_with_build_args' for nil:NilClass
复制代码
按照过程如下:
yum方式先安装openssl
  1. yum install openssl-devel -y
复制代码
再进入ruby源码目次中的ext目次下,找到openssl目次,进入后进行安装
  1. cd ruby-2.7.5/ext/openssl
  2. ruby extconf.rb
  3. make
  4. make install
复制代码
zlib-devel包如报错,也可同上方式处理。
在实行make,若出现如下报错:
  1. make: *** 没有规则可以创建“ossl_asn1.o”需要的目标“/include/ruby.h” 停止。
复制代码
可以在Makefile顶部中的增长 top_srcdir = …/…
再次实行 make && make install
2.3 gem安装redis
  1. gem install redis
复制代码
3. 初始化redis集群
相关依赖安装完成后,即可初始化redis集群,下令及过程如下:
  1. [redis@localhost redis-4.0.14]$ src/redis-trib.rb create --replicas 1 192.168.56.101:7000 192.168.56.101:7001 192.168.56.101:7002 192.168.56.101:7003 192.168.56.101:7004 192.168.56.101:7005
  2. >>> Creating cluster
  3. >>> Performing hash slots allocation on 6 nodes...
  4. Using 3 masters:
  5. 192.168.56.101:7000
  6. 192.168.56.101:7001
  7. 192.168.56.101:7002
  8. Adding replica 192.168.56.101:7004 to 192.168.56.101:7000
  9. Adding replica 192.168.56.101:7005 to 192.168.56.101:7001
  10. Adding replica 192.168.56.101:7003 to 192.168.56.101:7002
  11. >>> Trying to optimize slaves allocation for anti-affinity
  12. [WARNING] Some slaves are in the same host as their master
  13. M: cd6663924f0c31e6b60b815dca9cb7ea863f5573 192.168.56.101:7000
  14.    slots:0-5460 (5461 slots) master
  15. M: abc1f43c9a4e8813e9da15433ac66cd185dc39fe 192.168.56.101:7001
  16.    slots:5461-10922 (5462 slots) master
  17. M: 43fa53cec1ae164f784e5d439aaf80ee2f7e35af 192.168.56.101:7002
  18.    slots:10923-16383 (5461 slots) master
  19. S: 6ffcd16f1d445b0091c6239bc0988fb8a77fac96 192.168.56.101:7003
  20.    replicates cd6663924f0c31e6b60b815dca9cb7ea863f5573
  21. S: c523846d491f8df0bc97033b025b0d24375a13f8 192.168.56.101:7004
  22.    replicates abc1f43c9a4e8813e9da15433ac66cd185dc39fe
  23. S: 905dc9de7e074c282aab44b4ed5680a2020bcf4c 192.168.56.101:7005
  24.    replicates 43fa53cec1ae164f784e5d439aaf80ee2f7e35af
  25. Can I set the above configuration? (type 'yes' to accept): yes
  26. >>> Nodes configuration updated
  27. >>> Assign a different config epoch to each node
  28. >>> Sending CLUSTER MEET messages to join the cluster
  29. Waiting for the cluster to join....
  30. >>> Performing Cluster Check (using node 192.168.56.101:7000)
  31. M: cd6663924f0c31e6b60b815dca9cb7ea863f5573 192.168.56.101:7000
  32.    slots:0-5460 (5461 slots) master
  33.    1 additional replica(s)
  34. M: 43fa53cec1ae164f784e5d439aaf80ee2f7e35af 192.168.56.101:7002
  35.    slots:10923-16383 (5461 slots) master
  36.    1 additional replica(s)
  37. S: c523846d491f8df0bc97033b025b0d24375a13f8 192.168.56.101:7004
  38.    slots: (0 slots) slave
  39.    replicates abc1f43c9a4e8813e9da15433ac66cd185dc39fe
  40. S: 6ffcd16f1d445b0091c6239bc0988fb8a77fac96 192.168.56.101:7003
  41.    slots: (0 slots) slave
  42.    replicates cd6663924f0c31e6b60b815dca9cb7ea863f5573
  43. M: abc1f43c9a4e8813e9da15433ac66cd185dc39fe 192.168.56.101:7001
  44.    slots:5461-10922 (5462 slots) master
  45.    1 additional replica(s)
  46. S: 905dc9de7e074c282aab44b4ed5680a2020bcf4c 192.168.56.101:7005
  47.    slots: (0 slots) slave
  48.    replicates 43fa53cec1ae164f784e5d439aaf80ee2f7e35af
  49. [OK] All nodes agree about slots configuration.
  50. >>> Check for open slots...
  51. >>> Check slots coverage...
  52. [OK] All 16384 slots covered.
复制代码
至此,redis集群初始化完毕,各节点slot范围及脚色也打印出来了
Redis4 Cluster摆设

1、安装redis集群节点
因本次为伪分布式摆设,生产情况摆设时建议至少3台机器摆设(此中每台机器1主1从),依旧和redis4.0.14的方式一样摆设


**ip ****port **192.168.56.1017000192.168.56.1017001192.168.56.1017002192.168.56.1017003192.168.56.1017004192.168.56.1017005 1.1 启动cluster各节点
创建数据目次
  1. mkdir -p  /data/redis/cluster/{7000,7001,7002,7003,7004,7005}
复制代码
配置文件中紧张修改如下内容,其他的可按需调整,也可保持默认值,各节点中注意修改对应的端标语
  1. bind 192.168.56.103
  2. port 7000
  3. daemonize yes
  4. pidfile /data/redis/cluster/7000/redis_7000.pid
  5. logfile "/data/redis/cluster/7000/redis_7000.log"
  6. appendonly yes
  7. appendfilename "appendonly.aof"
  8. appendfsync everysec
  9. cluster-enabled yes
  10. cluster-config-file nodes-7000.conf     #注意此文件自动生成,且初始化时不要有和此重名的文件
  11. cluster-node-timeout 5000
  12. cluster-slave-validity-factor 10
  13. cluster-migration-barrier 1
  14. cluster-require-full-coverage yes
  15. cluster-slave-no-failover no
复制代码
启动各节点,建议用redis用户启动
  1. useradd redis
  2. chown -R  redis:redis  /data/redis/
  3. su - redis
  4. cd /data/redis/cluster/7001
  5. cp /data/redis/cluster/7000/redis.conf .
  6. sed -i "s#7000#7001#g" redis.conf
  7. redis-server redis.conf
复制代码
其他节点和7001雷同启动,启动后进程中会标志redis节点以cluster模式启动

2. 初始化集群
redis5.x之后的版本初始化集群相当便捷,下令及过程如下
  1. redis-cli --cluster create --cluster-replicas 1 192.168.56.103:7000 192.168.56.103:7001 192.168.56.103:7002 192.168.56.103:7003 192.168.56.103:7004 192.168.56.103:7005
  2. >>> Performing hash slots allocation on 6 nodes...
  3. Master[0] -> Slots 0 - 5460
  4. Master[1] -> Slots 5461 - 10922
  5. Master[2] -> Slots 10923 - 16383
  6. Adding replica 192.168.56.103:7004 to 192.168.56.103:7000
  7. Adding replica 192.168.56.103:7005 to 192.168.56.103:7001
  8. Adding replica 192.168.56.103:7003 to 192.168.56.103:7002
  9. >>> Trying to optimize slaves allocation for anti-affinity
  10. [WARNING] Some slaves are in the same host as their master
  11. M: 84ea774c08450db01bf5a518e3b9e55fd26d4d34 192.168.56.103:7000
  12.    slots:[0-5460] (5461 slots) master
  13. M: eb98e53273fd348deb5eabcc6bfffc20484749b1 192.168.56.103:7001
  14.    slots:[5461-10922] (5462 slots) master
  15. M: e059d418c11401189558d0f33bd5658297c10939 192.168.56.103:7002
  16.    slots:[10923-16383] (5461 slots) master
  17. S: 23eed1c27ad11c685e5a226e2f82d5c0b6384c79 192.168.56.103:7003
  18.    replicates e059d418c11401189558d0f33bd5658297c10939
  19. S: cfa2549ea7782bb4f0ed6d45e9e3704a7d38d540 192.168.56.103:7004
  20.    replicates 84ea774c08450db01bf5a518e3b9e55fd26d4d34
  21. S: c3e14c4139bfa88af03ea97679715f051a122806 192.168.56.103:7005
  22.    replicates eb98e53273fd348deb5eabcc6bfffc20484749b1
  23. Can I set the above configuration? (type 'yes' to accept): yes
  24. >>> Nodes configuration updated
  25. >>> Assign a different config epoch to each node
  26. >>> Sending CLUSTER MEET messages to join the cluster
  27. Waiting for the cluster to join
  28. .
  29. >>> Performing Cluster Check (using node 192.168.56.103:7000)
  30. M: 84ea774c08450db01bf5a518e3b9e55fd26d4d34 192.168.56.103:7000
  31.    slots:[0-5460] (5461 slots) master
  32.    1 additional replica(s)
  33. S: 23eed1c27ad11c685e5a226e2f82d5c0b6384c79 192.168.56.103:7003
  34.    slots: (0 slots) slave
  35.    replicates e059d418c11401189558d0f33bd5658297c10939
  36. M: eb98e53273fd348deb5eabcc6bfffc20484749b1 192.168.56.103:7001
  37.    slots:[5461-10922] (5462 slots) master
  38.    1 additional replica(s)
  39. S: c3e14c4139bfa88af03ea97679715f051a122806 192.168.56.103:7005
  40.    slots: (0 slots) slave
  41.    replicates eb98e53273fd348deb5eabcc6bfffc20484749b1
  42. S: cfa2549ea7782bb4f0ed6d45e9e3704a7d38d540 192.168.56.103:7004
  43.    slots: (0 slots) slave
  44.    replicates 84ea774c08450db01bf5a518e3b9e55fd26d4d34
  45. M: e059d418c11401189558d0f33bd5658297c10939 192.168.56.103:7002
  46.    slots:[10923-16383] (5461 slots) master
  47.    1 additional replica(s)
  48. [OK] All nodes agree about slots configuration.
  49. >>> Check for open slots...
  50. >>> Check slots coverage...
  51. [OK] All 16384 slots covered.
复制代码
看到如下效果,代表成功配置并分配slot完成

检察各节点信息也可以用如下下令
  1. [redis@localhost 7005]$ redis-cli -h 192.168.56.103 -p 7000 cluster nodes
  2. 23eed1c27ad11c685e5a226e2f82d5c0b6384c79 192.168.56.103:7003@17003 slave e059d418c11401189558d0f33bd5658297c10939 0 1646118171000 4 connected
  3. eb98e53273fd348deb5eabcc6bfffc20484749b1 192.168.56.103:7001@17001 master - 0 1646118171604 2 connected 5461-10922
  4. c3e14c4139bfa88af03ea97679715f051a122806 192.168.56.103:7005@17005 slave eb98e53273fd348deb5eabcc6bfffc20484749b1 0 1646118171000 6 connected
  5. cfa2549ea7782bb4f0ed6d45e9e3704a7d38d540 192.168.56.103:7004@17004 slave 84ea774c08450db01bf5a518e3b9e55fd26d4d34 0 1646118170000 5 connected
  6. e059d418c11401189558d0f33bd5658297c10939 192.168.56.103:7002@17002 master - 0 1646118169590 3 connected 10923-16383
  7. 84ea774c08450db01bf5a518e3b9e55fd26d4d34 192.168.56.103:7000@17000 myself,master - 0 1646118171000 1 connected 0-5460
复制代码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

河曲智叟

金牌会员
这个人很懒什么都没写!

标签云

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