最近同事需要搭建一套redis集群做测试,完了给我一台机器,让我搭建一下。本着学习的目的,果断搞起。 期间踩了好多坑,特此记录一下,过程如下:
OS版本是:Centos 6.6 x86_64
redis版本是:redis-3.2.4
安装依赖包
yum -y install gcc openssl-devel libyaml-devel libffi-devel readline-devel zlib-devel gdbm-devel ncurses-devel gcc-c++ automake autoconf
下载软件并解压
cd /opt
wget http://download.redis.io/releases/redis-3.2.4.tar.gz
tar -zxvf redis-3.2.4.tar.gz
编译安装
cd redis-3.2.4
make && make install
将相关软件复制到/usr/bin/目录下
cd /opt/redis-3.2.4/src
cp redis-server /usr/bin/
cp redis-cli /usr/bin/
cp redis-trib.rb /usr/bin/
创建redis节点
1,先创建单一节点的redis.conf配置文件
mkdir -p /opt/redis_cluster/{6000,6001,6002,6003,6004,6005}
cp /opt/redis-3.2.4/redis.conf /opt/redis_cluster/6000/redis_6000.conf
sed -i 's/^port 6379/port 6000/g' /opt/redis_cluster/6000/redis_6000.conf
sed -i 's/^bind 127.0.0.1/bind 10.0.0.230/g' /opt/redis_cluster/6000/redis_6000.conf
sed -i 's/^daemonize no/daemonize yes/g' /opt/redis_cluster/6000/redis_6000.conf
sed -i 's!^pidfile /var/run/redis_6379.pid!pidfile /var/run/redis_6000.pid!g' /opt/redis_cluster/6000/redis_6000.conf
sed -i 's/^# cluster-enabled yes/cluster-enabled yes/g' /opt/redis_cluster/6000/redis_6000.conf
sed -i 's!^# cluster-config-file nodes-6379.conf!cluster-config-file /opt/redis_cluster/6000/nodes_6000.conf!g' /opt/redis_cluster/6000/redis_6000.conf
sed -i 's/^# cluster-node-timeout 15000/cluster-node-timeout 15000/g' /opt/redis_cluster/6000/redis_6000.conf
sed -i 's/^appendonly no/appendonly yes/g' /opt/redis_cluster/6000/redis_6000.conf
sed -i 's!^dir ./!dir /opt/redis_cluster/6000/!g' /opt/redis_cluster/6000/redis_6000.conf
2,复制到其他节点配置文件
cp /opt/redis_cluster/6000/redis_6000.conf /opt/redis_cluster/6001/redis_6001.conf
cp /opt/redis_cluster/6000/redis_6000.conf /opt/redis_cluster/6002/redis_6002.conf
cp /opt/redis_cluster/6000/redis_6000.conf /opt/redis_cluster/6003/redis_6003.conf
cp /opt/redis_cluster/6000/redis_6000.conf /opt/redis_cluster/6004/redis_6004.conf
cp /opt/redis_cluster/6000/redis_6000.conf /opt/redis_cluster/6005/redis_6005.conf
3,修改其他节点的配置文件
sed -i 's/6000/6001/g' /opt/redis_cluster/6001/redis_6001.conf
sed -i 's/6000/6002/g' /opt/redis_cluster/6002/redis_6002.conf
sed -i 's/6000/6003/g' /opt/redis_cluster/6003/redis_6003.conf
sed -i 's/6000/6004/g' /opt/redis_cluster/6004/redis_6004.conf
sed -i 's/6000/6005/g' /opt/redis_cluster/6005/redis_6005.conf
4,至此节点配置信息创建完毕,然后启动所有节点
redis-server /opt/redis_cluster/6000/redis_6000.conf
redis-server /opt/redis_cluster/6001/redis_6001.conf
redis-server /opt/redis_cluster/6002/redis_6002.conf
redis-server /opt/redis_cluster/6003/redis_6003.conf
redis-server /opt/redis_cluster/6004/redis_6004.conf
redis-server /opt/redis_cluster/6005/redis_6005.conf
5,检查redis是否启动成功
ps -ef|grep redis
创建集群
redis-trib.rb create --replicas 1 10.0.0.230:6000 10.0.0.230:6001 10.0.0.230:6002 10.0.0.230:6003 10.0.0.230:6004 10.0.0.230:6005
创建集群的时候会报错,因为是执行的ruby的脚本,需要ruby的环境 错误内容:
/usr/bin/env: ruby: No such file or directory
所以需要安装ruby的环境,这里推荐使用yum install ruby安装
yum install ruby
然后再执行创建集群命令,还会报错,提示缺少rubygems组件,使用yum安装 错误内容:
./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
from ./redis-trib.rb:24
yum install rubygems
再次执行创建集群的命令,还会报错,提示不能加载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
需要手工下载并安装:
wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
gem install -l ./redis-3.2.1.gem
最后执行创建集群命令,输入yes即可
redis-trib.rb create --replicas 1 10.0.0.230:6000 10.0.0.230:6001 10.0.0.230:6002 10.0.0.230:6003 10.0.0.230:6004 10.0.0.230:6005
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏