最近研究Redis-cluster,正好在Centos6上搭建了一个redis集群。遇到了一些问题,结合自己的亲身操作,系统的总结下:
Redis集群部署文档(centos6系统)
(要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下)
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
127.0.0.1:7004
127.0.0.1:7005
1:下载redis。官网下载3.0.0版本,之前2.几的版本不支持集群模式
下载地址:https://github.com/antirez/redis/archive/3.0.0-rc2.tar.gz
2:安装gcc、c++环境,否则make出错:yum -y install gcc automake autoconf libtool make gcc-c++
3:上传服务器,解压,编译
tar -zxvf redis-3.0.0-rc2.tar.gz mv redis-3.0.0-rc2.tar.gz redis3.0 cd /usr/local/redis3.0 make 注意:处理特殊情况,当编译出现如下错误时:error: jemalloc/jemalloc.h: No such file or directory执行命令:make MALLOC=libc make install |
4:创建集群需要的目录
mkdir -p /usr/local/cluster cd /usr/local/cluster mkdir 7000 mkdir 7001 mkdir 7002 mkdir 7003 mkdir 7004 mkdir 7005
|
5:拷贝bin目录到7000/7001/7002/7003/7004/7005目录下
cp -r /usr/local/bin /usr/local/cluster/7000 cp -r /usr/local/bin /usr/local/cluster/7001 cp -r /usr/local/bin /usr/local/cluster/7002 cp -r /usr/local/bin /usr/local/cluster/7003 cp -r /usr/local/bin /usr/local/cluster/7004 cp -r /usr/local/bin /usr/local/cluster/7005 |
6:修改配置文件redis.conf
cp /usr/local/redis3.0/redis.conf /usr/local/cluster vi redis.conf ##修改配置文件中的下面选项 port 7000 daemonize yes cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes ##修改完redis.conf配置文件中的这些配置项之后把这个配置文件分别拷贝到7000/7001/7002/7003/7004/7005目录下面 cp /usr/local/cluster/redis.conf /usr/local/cluster/7000/bin cp /usr/local/cluster/redis.conf /usr/local/cluster/7001/bin cp /usr/local/cluster/redis.conf /usr/local/cluster/7002/bin cp /usr/local/cluster/redis.conf /usr/local/cluster/7003/bin cp /usr/local/cluster/redis.conf /usr/local/cluster/7004/bin cp /usr/local/cluster/redis.conf /usr/local/cluster/7005/bin
##注意:拷贝完成之后要修改7001/7002/7003/7004/7005目录下面redis.conf文件中的port参数,分别改为对应的文件夹的名称
|
7:分别启动这6个redis实例
cd /usr/local/cluster/7000/bin redis-server redis.conf cd /usr/local/cluster/7001/bin redis-server redis.conf cd /usr/local/cluster/7002/bin redis-server redis.conf cd /usr/local/cluster/7003/bin redis-server redis.conf cd /usr/local/cluster/7004/bin redis-server redis.conf cd /usr/local/cluster/7005/bin redis-server redis.conf
##启动之后使用命令查看redis的启动情况ps -ef|grep redis 如下图显示则说明启动成功
|
8:执行redis的创建集群命令创建集群
cd /usr/local/redis3.0/src ./redis-trib.rb create –replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 |
8.1执行上面的命令的时候会报错,因为是执行的ruby的脚本,需要ruby的环境
错误内容:/usr/bin/env: ruby: No such file or directory
所以需要安装ruby的环境,这里推荐使用yum install ruby安装
yum install ruby |
8.2然后再执行第8步的创建集群命令,还会报错,提示缺少rubygems组件,使用yum安装
错误内容:
./redis-trib.rb:24:in `require’: no such file to load – rubygems (LoadError)
from ./redis-trib.rb:24
yum install rubygems |
8.3再次执行第8步的命令,还会报错,提示不能加载redis,是因为缺少redis和ruby的接口,使用gem 安装
错误内容:
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require’: no such file to load – redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require’
from ./redis-trib.rb:25
gem install redis –version 3.0.0 |
注意:gem install redis –version 3.0.0 失败的话,需要修改一下gem的源
gem sources –remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
8.4 再次执行第8步的命令,正常执行
输入yes,然后配置完成。
至此redis集群即搭建成功!
9:使用redis-cli命令进入集群环境
redis-cli -c -p 7000 |