详解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语言环境)
yum install gcc-c++
3、进入到redis-3.0.0目录
make
4、安装redis到/usr/local/redis目录
make install PREFIX=/usr/local/redis
5、查看是否安装成功(出现bin目录即可)
6、前端启动redis(进入上图的bin目录)
./redis-server(开启)
./redis-cli shutdown(关闭)
7、后端启动
需要将redis解压之后的源码包中(redis-3.0.0目录下)的redis.conf文件拷贝到bin目录下
修改redis.conf文件,将daemonize改为yes先要使用vim redis.conf
使用命令后端启动redis
bin目录下执行 ./redis-server redis.conf
查看是否启动成功
关闭后端启动的方式:
./redis-cli shutdown
以上是单独的redis安装,接下来搭建集群!
安装ruby
yum install ruby yum install rubygems
将以下文件上传到linux系统
redis-3.0.0.gem (ruby和redis接口)
进入该目录执行:gem install redis-3.0.0.gem
将redis-3.0.0包下src目录中的以下文件拷贝到/usr/local/redis/redis-cluster/
前提:先新建一个redis-cluster目录
搭建集群最少也得需要3台主机,如果每台主机再配置一台从机的话,则最少需要6台机器。
端口设计如下:7001-7006
第一步:复制出一个7001机器
进入/usr/local/redis/目录执行 cp bin ./redis-cluster/7001 –r
第二步:如果存在持久化文件,则删除
rm -rf appendonly.aof dump.rdb
第三步:设置集群参数
第四步:修改端口
第五步:复制出7002-7006机器
给出我桌面系统下的目录结构:
接下来设置一键启动全部redis或者关闭redis群:
首先,新建一个文件,可以用vim打开一个不存在的文件,然后保存就存在了。(上图的start-all.sh和shutdow-all.sh就是我新建的)
start-all.sh:
cd 7001 ./redis-server redis.conf cd .. cd 7002 ./redis-server redis.conf cd .. cd 7003 ./redis-server redis.conf cd .. cd 7004 ./redis-server redis.conf cd .. cd 7005 ./redis-server redis.conf cd .. cd 7006 ./redis-server redis.conf cd ..
shutdow-all.sh:
cd 7001 ./redis-cli -p 7001 shutdown cd .. cd 7002 ./redis-cli -p 7002 shutdown cd .. cd 7003 ./redis-cli -p 7003 shutdown cd .. cd 7004 ./redis-cli -p 7004 shutdown cd .. cd 7005 ./redis-cli -p 7005 shutdown cd .. cd 7006 ./redis-cli -p 7006 shutdown cd ..
接下来修改那两个文件权限,设置其为可启动的脚本文件
chmod u+x start-all.sh chmod u+x shutdown-all.sh
接下来用ruby连接这几个集群,进行管理
/redis-trib.rb create --replicas 1 192.168.78.133:7001 192.168.78.133:7002 192.168.78.133:7003 192.168.78.133:7004 192.168.78.133:7005 192.168.78.133:7006
出现:
Connecting to node 192.168.242.137:7001: OK Connecting to node 192.168.242.137:7002: OK Connecting to node 192.168.242.137:7003: OK Connecting to node 192.168.242.137:7004: OK Connecting to node 192.168.242.137:7005: OK Connecting to node 192.168.242.137:7006: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.242.137:7001 192.168.242.137:7002 192.168.242.137:7003 Adding replica 192.168.242.137:7004 to 192.168.242.137:7001 Adding replica 192.168.242.137:7005 to 192.168.242.137:7002 Adding replica 192.168.242.137:7006 to 192.168.242.137:7003 M: 8240cd0fe6d6f842faa42b0174fe7c5ddcf7ae24 192.168.242.137:7001 slots:0-5460 (5461 slots) master M: 4f52a974f64343fd9f1ee0388490b3c0647a4db7 192.168.242.137:7002 slots:5461-10922 (5462 slots) master M: cb7c5def8f61df2016b38972396a8d1f349208c2 192.168.242.137:7003 slots:10923-16383 (5461 slots) master S: 66adf006fed43b3b5e499ce2ff1949a756504a16 192.168.242.137:7004 replicates 8240cd0fe6d6f842faa42b0174fe7c5ddcf7ae24 S: cbb0c9bc4b27dd85511a7ef2d01bec90e692793b 192.168.242.137:7005 replicates 4f52a974f64343fd9f1ee0388490b3c0647a4db7 S: a908736eadd1cd06e86fdff8b2749a6f46b38c00 192.168.242.137:7006 replicates cb7c5def8f61df2016b38972396a8d1f349208c2 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.242.137:7001) M: 8240cd0fe6d6f842faa42b0174fe7c5ddcf7ae24 192.168.242.137:7001 slots:0-5460 (5461 slots) master M: 4f52a974f64343fd9f1ee0388490b3c0647a4db7 192.168.242.137:7002 slots:5461-10922 (5462 slots) master M: cb7c5def8f61df2016b38972396a8d1f349208c2 192.168.242.137:7003 slots:10923-16383 (5461 slots) master M: 66adf006fed43b3b5e499ce2ff1949a756504a16 192.168.242.137:7004 slots: (0 slots) master replicates 8240cd0fe6d6f842faa42b0174fe7c5ddcf7ae24 M: cbb0c9bc4b27dd85511a7ef2d01bec90e692793b 192.168.242.137:7005 slots: (0 slots) master replicates 4f52a974f64343fd9f1ee0388490b3c0647a4db7 M: a908736eadd1cd06e86fdff8b2749a6f46b38c00 192.168.242.137:7006 slots: (0 slots) master replicates cb7c5def8f61df2016b38972396a8d1f349208c2 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
则代表成功了!
测试一下,进入7001目录执行:./redis-cli -h 192.168.242.137 -p 7001 –c
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。