首页 > 代码库 > centos下搭建redis集群

centos下搭建redis集群

必备的工具:
redis-3.0.0.tar
redis-3.0.0.gem(ruby和redis接口)
分析:
首先,集群数需要基数,这里搭建一个简单的redis集群(6个redis实例进行集群)。
在一台服务器上操作,因此仅需要6个不同的端口号即可。分别是:7001、7002、7003、7004、7005、7006。
步骤:
1、上传redis-3.0.0.tar到服务器(自己指定自己的软件目录),解压redis-3.0.0.tar。
2、安装c语言环境(安装centos之后,自带c语言环境)
yuminstallgcc-c++
3、进入到redis-3.0.0目录
make
4、安装redis到/usr/local/redis目录
makeinstallPREFIX=/usr/local/redis
5、查看是否安装成功(出现bin目录即可)
6、前端启动redis(进入上图的bin目录)
./redis-server(开启)
./redis-clishutdown
技术分享
7、后端启动
需要将redis解压之后的源码包中(redis-3.0.0目录下)的redis.conf文件拷贝到bin目录下
修改redis.conf文件,将daemonize改为yes先要使用vimredis.conf
技术分享
使用命令后端启动redis
bin目录下执行./redis-serverredis.conf
查看是否启动成功
关闭后端启动的方式:
以上是单独的redis安装,接下来搭建集群!
安装ruby
yuminstallruby
yuminstallrubygems
将以下文件上传到linux系统
进入该目录执行:geminstallredis-3.0.0.gem
将redis-3.0.0包下src目录中的以下文件拷贝到/usr/local/redis/redis-cluster/
前提:先新建一个redis-cluster目录
搭建集群最少也得需要3台主机,如果每台主机再配置一台从机的话,则最少需要6台机器。
端口设计如下:7001-7006
第一步:复制出一个7001机器
进入/usr/local/redis/目录执行cpbin./redis-cluster/7001–r
第二步:如果存在持久化文件,则删除
rm-rfappendonly.aofdump.rdb
第三步:设置集群参数
第四步:修改端口
第五步:复制出7002-7006机器
给出我桌面系统下的目录结构:
技术分享
接下来设置一键启动全部redis或者关闭redis群:
首先,新建一个文件,可以用vim打开一个不存在的文件,然后保存就存在了。(上图的start-all.sh和shutdow-all.sh就是我新建的)
start-all.sh:cd7001./redis-serverredis.confcd..cd7002./redis-serverredis.confcd..cd7003./redis-serverredis.confcd..cd7004./redis-serverredis.confcd..cd7005./redis-serverredis.confcd..cd7006./redis-serverredis.confcd..
shutdow-all.sh:cd7001./redis-cli-p7001shutdowncd..cd7002./redis-cli-p7002shutdowncd..cd7003./redis-cli-p7003shutdowncd..cd7004./redis-cli-p7004shutdowncd..cd7005./redis-cli-p7005shutdowncd..cd7006./redis-cli-p7006shutdowncd..
接下来修改那两个文件权限,设置其为可启动的脚本文件
chmodu+xstart-all.sh
chmodu+xshutdown-all.sh
接下来用ruby连接这几个集群,进行管理
/redis-trib.rbcreate--replicas1192.168.78.133:7001192.168.78.133:7002192.168.78.133:7003192.168.78.133:7004192.168.78.133:7005192.168.78.133:7006
出现:Connectingtonode192.168.242.137:7001:OKConnectingtonode192.168.242.137:7002:OKConnectingtonode192.168.242.137:7003:OKConnectingtonode192.168.242.137:7004:OKConnectingtonode192.168.242.137:7005:OKConnectingtonode192.168.242.137:7006:OK>>>Performinghashslotsallocationon6nodes...Using3masters:192.168.242.137:7001192.168.242.137:7002192.168.242.137:7003Addingreplica192.168.242.137:7004to192.168.242.137:7001Addingreplica192.168.242.137:7005to192.168.242.137:7002Addingreplica192.168.242.137:7006to192.168.242.137:7003M:8240cd0fe6d6f842faa42b0174fe7c5ddcf7ae24192.168.242.137:7001slots:0-5460(5461slots)masterM:4f52a974f64343fd9f1ee0388490b3c0647a4db7192.168.242.137:7002slots:5461-10922(5462slots)masterM:cb7c5def8f61df2016b38972396a8d1f349208c2192.168.242.137:7003slots:10923-16383(5461slots)masterS:66adf006fed43b3b5e499ce2ff1949a756504a16192.168.242.137:7004replicates8240cd0fe6d6f842faa42b0174fe7c5ddcf7ae24S:cbb0c9bc4b27dd85511a7ef2d01bec90e692793b192.168.242.137:7005replicates4f52a974f64343fd9f1ee0388490b3c0647a4db7S:a908736eadd1cd06e86fdff8b2749a6f46b38c00192.168.242.137:7006replicatescb7c5def8f61df2016b38972396a8d1f349208c2CanIsettheaboveconfiguration?(type‘yes‘toaccept):yes>>>Nodesconfigurationupdated>>>Assignadifferentconfigepochtoeachnode>>>SendingCLUSTERMEETmessagestojointheclusterWaitingfortheclustertojoin..>>>PerformingClusterCheck(usingnode192.168.242.137:7001)M:8240cd0fe6d6f842faa42b0174fe7c5ddcf7ae24192.168.242.137:7001slots:0-5460(5461slots)masterM:4f52a974f64343fd9f1ee0388490b3c0647a4db7192.168.242.137:7002slots:5461-10922(5462slots)masterM:cb7c5def8f61df2016b38972396a8d1f349208c2192.168.242.137:7003slots:10923-16383(5461slots)masterM:66adf006fed43b3b5e499ce2ff1949a756504a16192.168.242.137:7004slots:(0slots)masterreplicates8240cd0fe6d6f842faa42b0174fe7c5ddcf7ae24M:cbb0c9bc4b27dd85511a7ef2d01bec90e692793b192.168.242.137:7005slots:(0slots)masterreplicates4f52a974f64343fd9f1ee0388490b3c0647a4db7M:a908736eadd1cd06e86fdff8b2749a6f46b38c00192.168.242.137:7006slots:(0slots)masterreplicatescb7c5def8f61df2016b38972396a8d1f349208c2[OK]Allnodesagreeaboutslotsconfiguration.>>>Checkforopenslots...>>>Checkslotscoverage...[OK]All16384slotscovered.
则代表成功了!
测试一下,进入7001目录执行:./redis-cli-h192.168.242.137-p7001–c
技术分享
 
 
转自:https://baijiahao.baidu.com/po/feed/share?wfr=spider&for=pc&context=%7B%22sourceFrom%22%3A%22bjh%22%2C%22nid%22%3A%22news_3430456511238301812%22%7D

centos下搭建redis集群