搭建Redis集群的方法
这篇文章主要讲解了“搭建Redis集群的方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“搭建Redis集群的方法”吧!
Redis集群是一个由多个主从节点群组成的分布式服务集群,它具有复制、高可用和分片特性。Redis集群不需要sentinel哨兵也能完成节点移除和故障转移的功能。需要将每个节点设置成集群模式,这种集群模式没有中心节点,可水平扩展,据官方文档称可以线性扩展到上万个节点(官方推荐不超过1000个节点)。redis集群的性能和高可用性均优于之前版本的哨兵模式,且集群配置非常简单。
搭建环境系统:centos7.4
服务器金山云
安装ruby环境[root@jsy-bj-test00~]#yuminstall-yrubyrubygems复制6份redis服务
[work@jsy-bj-test00~]$cp-rpredisredis1[work@jsy-bj-test00~]$cp-rpredisredis2[work@jsy-bj-test00~]$cp-rpredisredis3redis配置文件修改
#六个节点需做如下更改[work@jsy-bj-test00~]$vimredis1/etc/redis.conf[work@jsy-bj-test00~]$sed-i's/port6379/port6380/g'redis5/etc/redis.conf#修改端口port6380#打开注释,开启集群模式cluster-enabledyes#集群的配置文件cluster-config-filenodes-6380.conf[work@jsy-bj-test00~]$sed-i's/cluster-config-filenodes-6379.conf/cluster-config-filenodes-6380.conf/g'redis5/etc/redis.conf#pidfile文件pidfile/var/run/redis_6380.pid[work@jsy-bj-test00~]$sed-i's/pidfile\/var\/run\/redis_6380.pid/pidfile\/var\/run\/redis_6380.pid/g'redis5/etc/redis.conf#日志文件logfile"/home/work/logs/redis/6380.log"[work@jsy-bj-test00~]$sed-i's/logfile"\/home\/work\/logs\/redis\/6379.log"/logfile"\/home\/work\/logs\/redis\/6380.log"/g'redis5/etc/redis.conf#rdb持久化文件dbfilenamedump6380.rdb[work@jsy-bj-test00~]$sed-i's/dbfilenamedump6379.rdb/dbfilenamedump6380.rdb/g'redis5/etc/redis.conf#请求超时,单位毫秒cluster-node-timeout5000#开启aof持久化方式appendonlyyes#配置持久化文件appendfilename"appendonly6379.aof"[work@jsy-bj-test00~]$sed-i's/appendfilename"appendonly6379.aof"/appendfilename"appendonly6383.aof"/g'redis5/etc/redis.conf编写集群启动脚本和停止脚本
启动脚本start_all.sh
/home/work/redis/bin/redis-server/home/work/redis/etc/redis.conf&/home/work/redis/bin/redis-server/home/work/redis/etc/redis6380.conf&/home/work/redis/bin/redis-server/home/work/redis/etc/redis6381.conf&/home/work/redis/bin/redis-server/home/work/redis/etc/redis6382.conf&/home/work/redis/bin/redis-server/home/work/redis/etc/redis6383.conf&/home/work/redis/bin/redis-server/home/work/redis/etc/redis6384.conf&
关闭脚本stop_all.sh
/home/work/redis/bin/redis-cli-p6379-atestshutdown/home/work/redis/bin/redis-cli-p6380-atestshutdown/home/work/redis/bin/redis-cli-p6381-atestshutdown/home/work/redis/bin/redis-cli-p6382-atestshutdown/home/work/redis/bin/redis-cli-p6383-atestshutdown/home/work/redis/bin/redis-cli-p6384-atestshutdown执行创建集群命令
有如下报错
[work@jsy-bj-test00src]$./redis-trib.rbcreate--replicas1127.0.0.1:6379127.0.0.1:6380127.0.0.1:6381127.0.0.1:6382127.0.0.1:6383127.0.0.1:6384/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in`require':cannotloadsuchfile--redis(LoadError)from/usr/share/rubygems/rubygems/core_ext/kernel_require.rb:55:in`require'from./redis-trib.rb:25:in`
‘
原因:安装redis接口报错ruby版本过低
[root@jsy-bj-test00~]#geminstallredisFetching:redis-4.1.3.gem(100%)ERROR:Errorinstallingredis:redisrequiresRubyversion>=2.3.0.
解决办法到官网下载最新稳定版源代码 http://www.ruby-lang.org/en/downloads/ 进行编译安装
[work@jsy-bj-test00soft]$tarzxvfruby-2.7.0.tar.gz[work@jsy-bj-test00soft]$cdruby-2.7.0[work@jsy-bj-test00ruby-2.7.0]$./configure--prefix=/home/work/ruby&&make&&makeinstall
安装redis接口
[work@jsy-bj-test00bin]$geminstallredis
再次执行创建集群命令
[work@jsy-bj-test00src]$./redis-trib.rbcreate--replicas1127.0.0.1:6379127.0.0.1:6380127.0.0.1:6381127.0.0.1:6382127.0.0.1:6383127.0.0.1:6384>>>Creatingcluster>>>Performinghashslotsallocationon6nodes.........>>>Nodesconfigurationupdated>>>Assignadifferentconfigepochtoeachnode>>>SendingCLUSTERMEETmessagestojointheclusterWaitingfortheclustertojoin...>>>PerformingClusterCheck(usingnode127.0.0.1:6379)......[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[OK]All16384slotscovered.
创建成功,查看集群状态
[work@jsy-bj-test00~]$./redis/bin/redis-cli-h127.0.0.1-p6379-c127.0.0.1:6379>clusterinfocluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:6cluster_my_epoch:1cluster_stats_messages_sent:627cluster_stats_messages_received:627
查看集群节点信息
127.0.0.1:6379>clusternodes565246bf31d8e05e464db7455521b1a9f165a9cd127.0.0.1:6380master-015784477762302connected5461-109221b99b2a1e4b530501476ab48422c75f30423fd19127.0.0.1:6383slave565246bf31d8e05e464db7455521b1a9f165a9cd015784477782335connected735ad5778458059316794b9378d4b81aaff20322127.0.0.1:6379myself,master-001connected0-5460757c2c11ecebfc607aa10a6877e348d0e2da484f127.0.0.1:6381master-015784477777323connected10923-16383f22efc2bcfcd11cee6487ebc9c75de3b59f5e1d0127.0.0.1:6382slave735ad5778458059316794b9378d4b81aaff20322015784477762304connectedb8bba94b9647caa8600363144fd7108082e45f56127.0.0.1:6384slave757c2c11ecebfc607aa10a6877e348d0e2da484f015784477772326connected#这是很重要的命令,我们需要关心的信息有:#第一个参数:节点ID#第二个参数:IP:PORT@TCP这里一个坑,jedis-2.9.0之前的版本解析@出错#第三个参数:标志(Master,Slave,Myself,Fail...)#第四个参数:如果是从机则是主机的节点ID#最后两个参数:连接的状态和槽的位置。集群管理相关命令
#集群增加节点,先复制两个配置文件,并修改配置文件内容[work@jsy-bj-test00etc]$cp-predis.confredis6385.conf[work@jsy-bj-test00etc]$cp-predis.confredis6386.conf[work@jsy-bj-test00etc]$sed-i's/6379/6385/g'redis6385.conf[work@jsy-bj-test00etc]$sed-i's/6379/6386/g'redis6386.conf
启动6385节点
[work@jsy-bj-test00bin]$./redis-server/home/work/redis/etc/redis6385.conf&
将集群管理工具软连到redis的bin下
[work@jsy-bj-test00bin]$ln-s/home/work/soft/redis-3.2.11/src/redis-trib.rb/home/work/redis/bin/redis-trib.rb
将新节点加入集群master
[work@jsy-bj-test00bin]$./redis-trib.rbadd-node127.0.0.1:6385127.0.0.1:6379>>>Addingnode127.0.0.1:6385tocluster127.0.0.1:6379>>>PerformingClusterCheck(usingnode127.0.0.1:6379)......[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[OK]All16384slotscovered.>>>SendCLUSTERMEETtonode127.0.0.1:6385tomakeitjointhecluster.[OK]Newnodeaddedcorrectly.
查看集群状态,未分配槽位
[work@jsy-bj-test00bin]$./redis-cli-h127.0.0.1-p6379-cclusternodesd00d05f601df0b69df0c2cc532b636d2c83347be127.0.0.1:6385master-015784502801910connected
给6385节点分配槽位
[work@jsy-bj-test00bin]$./redis-trib.rbreshard127.0.0.1:6379>>>PerformingClusterCheck(usingnode127.0.0.1:6379)......[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[OK]All16384slotscovered.Howmanyslotsdoyouwanttomove(from1to16384)?500WhatisthereceivingnodeID?d00d05f601df0b69df0c2cc532b636d2c83347bePleaseenterallthesourcenodeIDs.Type'all'touseallthenodesassourcenodesforthehashslots.Type'done'onceyouenteredallthesourcenodesIDs.Sourcenode#1:allDoyouwanttoproceedwiththeproposedreshardplan(yes/no)?yes#第一个参数:需要移动槽的个数,#第二个参数:接受槽的节点ID,#第三个参数:输入"all"表示从所有原节点中获取槽,#第四个参数:输入"yes"开始移动槽到目标结点id#查看6385节点信息,已经分配槽位[work@jsy-bj-test00bin]$./redis-cli-h127.0.0.1-p6379-cclusternodesd00d05f601df0b69df0c2cc532b636d2c83347be127.0.0.1:6385master-015784524221677connected0-1655461-562710923-11088
添加从节点,启动6386节点并加入集群
[work@jsy-bj-test00bin]$./redis-server/home/work/redis/etc/redis6386.conf&[work@jsy-bj-test00bin]$./redis-trib.rbadd-node--slave--master-idd00d05f601df0b69df0c2cc532b636d2c83347be127.0.0.1:6386127.0.0.1:6385>>>Addingnode127.0.0.1:6386tocluster127.0.0.1:6385>>>PerformingClusterCheck(usingnode127.0.0.1:6385)......[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[OK]All16384slotscovered.>>>SendCLUSTERMEETtonode127.0.0.1:6386tomakeitjointhecluster.Waitingfortheclustertojoin.>>>Configurenodeasreplicaof127.0.0.1:6385.[OK]Newnodeaddedcorrectly.
查看6386节点状态
[work@jsy-bj-test00bin]$./redis-cli-h127.0.0.1-p6379-cclusternodes6845878cbef3fe25f19a70a8db3eb29abb1b9ea6127.0.0.1:6386slaved00d05f601df0b69df0c2cc532b636d2c83347be015784528054397connected
删除节点
[work@jsy-bj-test00bin]$./redis-trib.rbdel-node127.0.0.1:63831b99b2a1e4b530501476ab48422c75f30423fd19>>>Removingnode1b99b2a1e4b530501476ab48422c75f30423fd19fromcluster127.0.0.1:6383>>>SendingCLUSTERFORGETmessagestothecluster...>>>SHUTDOWNthenode.
感谢各位的阅读,以上就是“搭建Redis集群的方法”的内容了,经过本文的学习后,相信大家对搭建Redis集群的方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。