redis高版本使用ruby实现了集群,所以需要ruby环境,安装ruby环境和redis的gem接口后,就可以使用redis的redis-trib.rb脚本创建集群。
先列一下大的步骤。
1.修改配置文件,启动多个redis实例。
2.安装ruby、rubygems、redis ruby接口。
安装gem 需要ruby的版本在 1.8.7 以上,默认的centos5 上都是1.8.5 版本,所以首先你的升级你的ruby
3.gem 安装redis ruby 接口
4.redis-trib.rb脚本创建集群
——————————————————————————————————————————————
本人使用两台桥接的虚拟机进行了分布式集群的搭建,系统为centos5。
具体操作过程:
1.修改配置文件,启动多个redis实例。
下载redis3.0以上版本
tar zxf 解压
make&&make install 安装
创建单独目录,我是mkdir /usr/local/cluster ,再在其下创建目录 7000、7001、7002、7003
将redis.conf分别拷贝到这几个目录下面,并分别修改redis.conf 中的端口号port和目录名一致。
daemonize修改为yes后台启动,daemonize yes
启动4个实例
cd /usr/local/cluster
cd 7000
redis-server redis.conf
cd ../7001
redis-server redis.conf
如上把7002 7003也启动起来
然后在另一台虚拟机下创建 9000 9001 两个目录也按上面的redis-server redis.conf方法分别启动。
ps -ef|grep redis 看下是否都启动起来了
[root@localhost ~]# ps -ef|grep redis
root 10957 1 0 00:15 ? 00:00:01 redis-server *:7000 [cluster]
root 10961 1 0 00:15 ? 00:00:01 redis-server *:7001 [cluster]
root 10965 1 0 00:15 ? 00:00:01 redis-server *:7002 [cluster]
root 10982 1 0 00:15 ? 00:00:01 redis-server *:7003 [cluster]
2.安装ruby、rubygems、redis ruby接口。
yum install ruby
yum install rubygems
gem install redis
理想情况下上面三行就搞定了,但是我的ruby版本太低,需要升级ruby。
安装gem 需要ruby的版本在 1.8.7 以上,默认的centos5 上都是1.8.5 版本:
所以需要升级zlib,升级ruby
先升级zlib
yum install -y gcc zlib zlib-devel
yum install ruby
查看下ruby的版本
ruby -v
rpm -ivh http://yum.puppetlabs.com/el/5/products/x86_64/puppetlabs-release-5-6.noarch.rpm
yum install ruby ruby-devel rubygems rpm-build
rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
vi /etc/yum.repos.d/epel.repo 修改 enabled=1
yum install rubygems
3.gem 安装redis ruby 接口,我是先下载下来放到本地安装的。
wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
sudo gem install -l ./redis-3.2.1.gem
4.redis-trib.rb脚本创建集群
cd /usr/local/redis3.0.5/
src/redis-trib.rb create --replicas 1 192.168.129.103:7000 192.168.129.103:7001 192.168.129.103:7002 192.168.129.103:7003 192.168.129.107:9000 192.168.129.107:9001
[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.129.103:7000 192.168.129.103:7001 192.168.129.103:7002 192.168.129.103:7003 192.168.129.107:9000 192.168.129.107:9001
>>> Creating cluster
Connecting to node 192.168.129.103:7000: OK
Connecting to node 192.168.129.103:7001: OK
Connecting to node 192.168.129.103:7002: OK
Connecting to node 192.168.129.103:7003: OK
Connecting to node 192.168.129.107:9000: OK
Connecting to node 192.168.129.107:9001: OK
>>> Performing hash slots allocation on 6 nodes...
集群创建完成,check一下看看:
[root@localhost src]# ./redis-trib.rb check 192.168.129.103:7000
Connecting to node 192.168.129.103:7000: OK
Connecting to node 192.168.129.103:7003: OK
Connecting to node 192.168.129.103:7001: OK
Connecting to node 192.168.129.107:9000: OK
Connecting to node 192.168.129.103:7002: OK
Connecting to node 192.168.129.107:9001: OK
>>> Performing Cluster Check (using node 192.168.129.103:7000)
M: 5f0adb50d0dd1aa69d34ec66b09413d984d124a1 192.168.129.103:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 4326d921c90559c9b26d84c19b151b8be43ee711 192.168.129.103:7003
slots: (0 slots) slave
replicates 25e7505b6647bc56781a6f8cf9addcae196d000c
S: b17b58c85c2a70ab781f35edc900d3031c7623b1 192.168.129.103:7001
slots: (0 slots) slave
replicates cc319d119402df317df41bc60b38cd8bb80e78bf
M: cc319d119402df317df41bc60b38cd8bb80e78bf 192.168.129.107:9000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 966450aef1072b222b876c046f9a78ab7837fcd8 192.168.129.103:7002
slots: (0 slots) slave
replicates 5f0adb50d0dd1aa69d34ec66b09413d984d124a1
M: 25e7505b6647bc56781a6f8cf9addcae196d000c 192.168.129.107:9001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost src]#
没问题,再客户端连接上去操作下,连接集群需要加-c参数
[root@localhost src]# redis-cli -c -p 7000
127.0.0.1:7000> set key v
-> Redirected to slot [12539] located at 192.168.129.107:9001
OK
192.168.129.107:9001> set key5 v5
-> Redirected to slot [9057] located at 192.168.129.103:7000
OK
192.168.129.103:7000>
如果想修改集群,可以使用add-node,迁移数据槽使用reshard,暂不详细描述了。
参考资料:
1.百度文库里的《redis3.0.2 分布式集群安装详细步骤》
2.centos 安装 redis3.0.0 集群
http://blog.csdn.net/lifeiaidajia/article/details/45370377
3.ruby升级不堪回首的往事
http://www.tuicool.com/articles/yauaM3
4.安装 ruby gems
http://storysky.blog.51cto.com/628458/1155353/