【百万字详解Redis】集群部署

avatar
作者
筋斗云
阅读量:2

在这里插入图片描述

文章目录

🌈你好呀!我是 山顶风景独好
🎈欢迎踏入我的博客世界,能与您在此邂逅,真是缘分使然!😊
🌸愿您在此停留的每一刻,都沐浴在轻松愉悦的氛围中。
📖这里不仅有丰富的知识和趣味横生的内容等您来探索,更是一个自由交流的平台,期待您留下独特的思考与见解。🌟
🎓 惊喜福利!完成三连并加v,即可免费领取课程设计和毕业设计的源码!🎁这是对您支持的特别回馈,赶快行动吧!
📍文章末尾附有领取指南,别忘了查看哦!🎉
🚀让我们一起踏上这段探索与成长的旅程,携手挖掘更多可能,共同进步!💪✨

Redis集群部署

Redis5版本前(redis3.x及redis4.x)cluster部署较麻烦,因部署过程中涉及的依赖软件版本要求较苛刻,因此记录一下redis4.0.14版本redis cluster的部署过程。

1 Redis各节点部署

使用源码安装各节点,不过与非cluster方式不同的是,配置文件中需启动cluster相关的配置。编译过程可参考分分钟搞定Redis编译安装

因本次为伪分布式部署,生产环境部署时建议至少3台机器部署(其中每台机器1主1从)



ipport
192.168.56.1017000
192.168.56.1017001
192.168.56.1017002
192.168.56.1017003
192.168.56.1017004
192.168.56.1017005

1.1 启动cluster各节点

创建数据目录

mkdir -p  /data/redis/cluster/{7000,7001,7002,7003,7004,7005} 

配置文件中主要修改如下内容,其他的可按需调整,也可保持默认值,各节点中注意修改对应的端口号

 bind 192.168.56.101 port 7000 daemonize yes pidfile /data/redis/cluster/7000/redis_7000.pid logfile "/data/redis/cluster/7000/redis_7000.log" appendonly yes appendfilename "appendonly.aof" appendfsync everysec cluster-enabled yes cluster-config-file nodes-7000.conf     #注意此文件自动生成,且初始化时不要有和此重名的文件 cluster-node-timeout 5000 cluster-slave-validity-factor 10 cluster-migration-barrier 1 cluster-require-full-coverage yes cluster-slave-no-failover no 

启动各节点,建议用redis用户启动

 useradd redis chown -R  redis:redis  /data/redis/ su - redis cd /data/redis/cluster/7001 cp /data/redis/cluster/7000/redis.conf .  sed -i "s#7000#7001#g" redis.conf 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/

tar -zxvf  ruby-2.7.5.tar.gz cd ruby-2.7.5  ./configure make   make install 

安装完毕后,检查ruby以及gem版本

2.2 安装openssl-devel及zlib-devel

安装完ruby后,使用gem安装redis包,此时如果没有安装openssl 则回报如下错误

gem install  redis ERROR:  Loading command: install (LoadError)     cannot load such file -- openssl ERROR:  While executing gem ... (NoMethodError)     undefined method `invoke_with_build_args' for nil:NilClass 

按照过程如下:

yum方式先安装openssl

 yum install openssl-devel -y 

再进入ruby源码目录中的ext目录下,找到openssl目录,进入后进行安装

 cd ruby-2.7.5/ext/openssl ruby extconf.rb  make make install 

zlib-devel包如报错,也可同上方式处理。

在执行make,若出现如下报错:

 make: *** 没有规则可以创建“ossl_asn1.o”需要的目标“/include/ruby.h” 停止。 

可以在Makefile顶部中的增加 top_srcdir = …/…

再次执行 make && make install

2.3 gem安装redis

 gem install redis 

3. 初始化redis集群

相关依赖安装完成后,即可初始化redis集群,命令及过程如下:

[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 >>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.56.101:7000 192.168.56.101:7001 192.168.56.101:7002 Adding replica 192.168.56.101:7004 to 192.168.56.101:7000 Adding replica 192.168.56.101:7005 to 192.168.56.101:7001 Adding replica 192.168.56.101:7003 to 192.168.56.101:7002 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: cd6663924f0c31e6b60b815dca9cb7ea863f5573 192.168.56.101:7000    slots:0-5460 (5461 slots) master M: abc1f43c9a4e8813e9da15433ac66cd185dc39fe 192.168.56.101:7001    slots:5461-10922 (5462 slots) master M: 43fa53cec1ae164f784e5d439aaf80ee2f7e35af 192.168.56.101:7002    slots:10923-16383 (5461 slots) master S: 6ffcd16f1d445b0091c6239bc0988fb8a77fac96 192.168.56.101:7003    replicates cd6663924f0c31e6b60b815dca9cb7ea863f5573 S: c523846d491f8df0bc97033b025b0d24375a13f8 192.168.56.101:7004    replicates abc1f43c9a4e8813e9da15433ac66cd185dc39fe S: 905dc9de7e074c282aab44b4ed5680a2020bcf4c 192.168.56.101:7005    replicates 43fa53cec1ae164f784e5d439aaf80ee2f7e35af Can I set the above configuration? (type 'yes' to accept): 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.56.101:7000) M: cd6663924f0c31e6b60b815dca9cb7ea863f5573 192.168.56.101:7000    slots:0-5460 (5461 slots) master    1 additional replica(s) M: 43fa53cec1ae164f784e5d439aaf80ee2f7e35af 192.168.56.101:7002    slots:10923-16383 (5461 slots) master    1 additional replica(s) S: c523846d491f8df0bc97033b025b0d24375a13f8 192.168.56.101:7004    slots: (0 slots) slave    replicates abc1f43c9a4e8813e9da15433ac66cd185dc39fe S: 6ffcd16f1d445b0091c6239bc0988fb8a77fac96 192.168.56.101:7003    slots: (0 slots) slave    replicates cd6663924f0c31e6b60b815dca9cb7ea863f5573 M: abc1f43c9a4e8813e9da15433ac66cd185dc39fe 192.168.56.101:7001    slots:5461-10922 (5462 slots) master    1 additional replica(s) S: 905dc9de7e074c282aab44b4ed5680a2020bcf4c 192.168.56.101:7005    slots: (0 slots) slave    replicates 43fa53cec1ae164f784e5d439aaf80ee2f7e35af [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. 

至此,redis集群初始化完毕,各节点slot范围及角色也打印出来了

经历了redis5.x之前(redis3.x或redis4.x)集群部署过程中ruby版本及各种依赖包蹂躏后,再感受一下redis5.x之后集群部署的便捷。

Redis4 Cluster部署

1、安装redis集群节点

因本次为伪分布式部署,生产环境部署时建议至少3台机器部署(其中每台机器1主1从),依旧和redis4.0.14的方式一样部署



ipport
192.168.56.1017000
192.168.56.1017001
192.168.56.1017002
192.168.56.1017003
192.168.56.1017004
192.168.56.1017005

1.1 启动cluster各节点

创建数据目录

mkdir -p  /data/redis/cluster/{7000,7001,7002,7003,7004,7005} 

配置文件中主要修改如下内容,其他的可按需调整,也可保持默认值,各节点中注意修改对应的端口号

 bind 192.168.56.103 port 7000 daemonize yes pidfile /data/redis/cluster/7000/redis_7000.pid logfile "/data/redis/cluster/7000/redis_7000.log" appendonly yes appendfilename "appendonly.aof" appendfsync everysec cluster-enabled yes cluster-config-file nodes-7000.conf     #注意此文件自动生成,且初始化时不要有和此重名的文件 cluster-node-timeout 5000 cluster-slave-validity-factor 10 cluster-migration-barrier 1 cluster-require-full-coverage yes cluster-slave-no-failover no 

启动各节点,建议用redis用户启动

useradd redis chown -R  redis:redis  /data/redis/ su - redis cd /data/redis/cluster/7001 cp /data/redis/cluster/7000/redis.conf .  sed -i "s#7000#7001#g" redis.conf redis-server redis.conf 

其他节点和7001类似启动,启动后进程中会标记redis节点以cluster模式启动

在这里插入图片描述

2. 初始化集群

redis5.x之后的版本初始化集群相当便捷,命令及过程如下

 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 >>> 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.56.103:7004 to 192.168.56.103:7000 Adding replica 192.168.56.103:7005 to 192.168.56.103:7001 Adding replica 192.168.56.103:7003 to 192.168.56.103:7002 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 84ea774c08450db01bf5a518e3b9e55fd26d4d34 192.168.56.103:7000    slots:[0-5460] (5461 slots) master M: eb98e53273fd348deb5eabcc6bfffc20484749b1 192.168.56.103:7001    slots:[5461-10922] (5462 slots) master M: e059d418c11401189558d0f33bd5658297c10939 192.168.56.103:7002    slots:[10923-16383] (5461 slots) master S: 23eed1c27ad11c685e5a226e2f82d5c0b6384c79 192.168.56.103:7003    replicates e059d418c11401189558d0f33bd5658297c10939 S: cfa2549ea7782bb4f0ed6d45e9e3704a7d38d540 192.168.56.103:7004    replicates 84ea774c08450db01bf5a518e3b9e55fd26d4d34 S: c3e14c4139bfa88af03ea97679715f051a122806 192.168.56.103:7005    replicates eb98e53273fd348deb5eabcc6bfffc20484749b1 Can I set the above configuration? (type 'yes' to accept): 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.56.103:7000) M: 84ea774c08450db01bf5a518e3b9e55fd26d4d34 192.168.56.103:7000    slots:[0-5460] (5461 slots) master    1 additional replica(s) S: 23eed1c27ad11c685e5a226e2f82d5c0b6384c79 192.168.56.103:7003    slots: (0 slots) slave    replicates e059d418c11401189558d0f33bd5658297c10939 M: eb98e53273fd348deb5eabcc6bfffc20484749b1 192.168.56.103:7001    slots:[5461-10922] (5462 slots) master    1 additional replica(s) S: c3e14c4139bfa88af03ea97679715f051a122806 192.168.56.103:7005    slots: (0 slots) slave    replicates eb98e53273fd348deb5eabcc6bfffc20484749b1 S: cfa2549ea7782bb4f0ed6d45e9e3704a7d38d540 192.168.56.103:7004    slots: (0 slots) slave    replicates 84ea774c08450db01bf5a518e3b9e55fd26d4d34 M: e059d418c11401189558d0f33bd5658297c10939 192.168.56.103:7002    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.  

看到如下结果,代表成功配置并分配slot完成

在这里插入图片描述

查看各节点信息也可以用如下命令

 [redis@localhost 7005]$ redis-cli -h 192.168.56.103 -p 7000 cluster nodes 23eed1c27ad11c685e5a226e2f82d5c0b6384c79 192.168.56.103:7003@17003 slave e059d418c11401189558d0f33bd5658297c10939 0 1646118171000 4 connected eb98e53273fd348deb5eabcc6bfffc20484749b1 192.168.56.103:7001@17001 master - 0 1646118171604 2 connected 5461-10922 c3e14c4139bfa88af03ea97679715f051a122806 192.168.56.103:7005@17005 slave eb98e53273fd348deb5eabcc6bfffc20484749b1 0 1646118171000 6 connected cfa2549ea7782bb4f0ed6d45e9e3704a7d38d540 192.168.56.103:7004@17004 slave 84ea774c08450db01bf5a518e3b9e55fd26d4d34 0 1646118170000 5 connected e059d418c11401189558d0f33bd5658297c10939 192.168.56.103:7002@17002 master - 0 1646118169590 3 connected 10923-16383 84ea774c08450db01bf5a518e3b9e55fd26d4d34 192.168.56.103:7000@17000 myself,master - 0 1646118171000 1 connected 0-5460 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!