redis cluster搭建

阅读更多

    redis 终于推出正式的集群解决方案,最新稳定版本v3.0.2。集群实现目的将不同的key分散到不同的节点上,传统哈希算法hash(key)%n,不适用于添加和删除节点场景,会导致大量的key无法命中,扩展比较困难。redis 没有采用一致性hash,而采用哈希槽( hash slot ) 方式来实现数据共享。

    具体算法:crc16( key )%16384,从而将 16384 个槽分配至不同的节点上。 

 

环境准备:

192.168.5.8     三个redis服务,分别监听 7000 70001 70002  端口

192.168.5.14   三个redis服务,分别监听 7003 70004 70005  端口  

 

一. 配置

#redis.conf
port 7000				#监听端口,集群间通信端口为 10000+7000
cluster-enabled yes		#启用集群
cluster-config-file nodes-7000.conf                    #指定节点配置文件,由redis自动生成管理
cluster-node-timeout 5000				#节点间通信最大超时时间
appendonly yes							#启动aof持久化模式
    

二. 运行

 

192.168.5.8 上启动三个redis实例,分别监听7000,7001,7002 三个端口。192.168.5.14启动实例监听7003,7004,7005三个端口。 

mkdir cluster-test
cd cluster-test
mkdir 7000 7001 7002 
 
mkdir 7003 7004 7005
 
cd 7000
./redis-server redis.conf 

 

三. 集群

cd $REDIS_SRC/src 
./redis-trib.rb create --replicas 1 192.168.5.14:7000 192.168.5.14:7001 192.168.5.14:7002 192.168.5.8:7003 192.168.5.8:7004 192.168.5.8:7005

   选项 --replicas 1 为每个主机创建一个相应的备机

 

运行该脚本报错,没有配置ruby环境。

./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)

from ./redis-trib.rb:24

 

方法一:用yum安装

 

#yum源安装ruby
yum install -y ruby
yum install -y rubygems
 
# 安装redis库
gem install redis   
ERROR:  could not find gem redis locally or in a repository       #又被墙了,可恶
 
#下载redis库并安装
wget https://rubygems.org/downloads/redis-3.2.1.gem
gem source
gem source -a http://rubygems.org/
gem install redis-3.2.1.gem 
    运行上述命令,报错:[ERR] Sorry, can't connect to node 192.168.5.14:7000,ruby 1.8.5 (2006-08-25) [i386-linux]。运行客户端 ./redis-cli -p 7000 能正常交互,搜索发现是redis官方需要较高的ruby版本。

 

方法二:源码安装

既然用yum不能安装最新版本,还是老老实实用源码安装 

 

#源码安装ruby
wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.2.tar.gz
tar -xvf ruby-2.2.2.tar.gz
./configure --prefix=/usr/local/ruby-2.2.2
make && make install 
 
gem install redis
    运行上述命令正常,发现 7000,7001,7003 变成主机,7002,7004,7005成为备机。

  

[root@pclient redis]# ./redis-trib.rb create --replicas 1 192.168.5.14:7000 192.168.5.14:7001 192.168.5.14:7002 192.168.5.8:7003 192.168.5.8:7004 192.168.5.8:7005
>>> Creating cluster
Connecting to node 192.168.5.14:7000: OK
Connecting to node 192.168.5.14:7001: OK
Connecting to node 192.168.5.14:7002: OK
Connecting to node 192.168.5.8:7003: OK
Connecting to node 192.168.5.8:7004: OK
Connecting to node 192.168.5.8:7005: OK
>>> Performing hash slots allocation on 6 nodes...
......
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

 

测试:

$ redis-cli -c -p 7000#集群交互必须加上-c选项,否则执行出错。

redis 127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
redis 127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7000
OK
redis 127.0.0.1:7000> get foo
-> Redirected to slot [12182] located at 127.0.0.1:7002
"bar"
redis 127.0.0.1:7000> get hello
-> Redirected to slot [866] located at 127.0.0.1:7000
"world"

  

参考资料:

http://redis.io/topics/cluster-tutorial

 

你可能感兴趣的:(redis,cluster,ruby)