测试我们选择2台服务器,分别为:192.168.56.56,192.168.56.57每个服务器有3个节点。
在192.168.56.56创建3个节点
创建集群的目录
[root@localhost ~]# cd /
[root@localhost /]# mkdir redis_cluster
[root@localhost /]# cd redis_cluster/
创建节点的目录
[root@localhost redis_cluster]# mkdir 7000
[root@localhost redis_cluster]# mkdir 7001
[root@localhost redis_cluster]# mkdir 7002
把配置文件复制到每个节点目录中
[root@localhost redis_cluster]# cp /redis/redis-3.2.1/redis.conf /redis_cluster/7000
[root@localhost redis_cluster]# cp /redis/redis-3.2.1/redis.conf /redis_cluster/7001
[root@localhost redis_cluster]# cp /redis/redis-3.2.1/redis.conf /redis_cluster/7002
依次修改以上的每个redis.conf文件
vi redis.conf
daemonize yes //redis后台运行
pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002
port 7000 //端口7000,7001,7002
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes-7000.conf //集群的配置 配置文件首次启动自动生成
cluster-node-timeout 5000 //请求超时 设置5秒够了
bind 192.168.56.56 //绑定本机的IP
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
依次启动每个节点
[root@localhost src]# ./redis-server /redis_cluster/7000/redis.conf
[root@localhost src]# ./redis-server /redis_cluster/7001/redis.conf
[root@localhost src]# ./redis-server /redis_cluster/7002/redis.conf
在192.168.56.57服务器也创建3个节点7003、7004、7005,和上面操作一致,这里不再做演示。
查看redis启动情况
在192.168.56.56查看redis启动情况,无问题
[root@shanxi 7002]# ps -ef|grep redis
root 15665 1 015:46 ? 00:00:04 ./redis-server 192.168.56.56:7000 [cluster]
root 15680 1 015:48 ? 00:00:04 ./redis-server 192.168.56.56:7001 [cluster]
root 15684 1 015:48 ? 00:00:04 ./redis-server 192.168.56.56:7002 [cluster]
在192.168.56.57查看redis启动情况,无问题
[root@localhost 7003]# ps -ef|grep redis
root 28183 1 0 15:50 ? 00:00:03 ./redis-server 192.168.56.57:7003 [cluster]
root 28187 1 015:50 ? 00:00:03 ./redis-server 192.168.56.57:7004 [cluster]
root 28191 1 0 15:50 ? 00:00:03 ./redis-server 192.168.56.57:7005 [cluster]
注:Redis是用./redis-trib.rb来创建集群的,需要ruby执行,详见附一ruby的安装
1、参数如下:
[root@localhost src]# ./redis-trib.rb
Usage: redis-trib
create host1:port1 ... hostN:portN
--replicas
check host:port
info host:port
fix host:port
--timeout
reshard host:port
--from
--to
--slots
--yes
--timeout
--pipeline
rebalance host:port
--weight
--auto-weights
--use-empty-masters
--timeout
--simulate
--pipeline
--threshold
add-node new_host:new_portexisting_host:existing_port
--slave
--master-id
del-node host:port node_id
set-timeout host:portmilliseconds
call host:port commandarg arg .. arg
import host:port
--from
--copy
--replace
help (show this help)
For check, fix, reshard, del-node,set-timeout you can specify the host and port of any working node in thecluster.
2、创建集群
[root@localhost src]# /redis/redis-3.2.1/src/redis-trib.rb create --replicas 1 192.168.56.56:7000 192.168.56.56:7001 192.168.56.56:7002 192.168.56.57:7003 192.168.56.57:7004 192.168.56.57:7005
>>> Creating cluster
>>> Performing hash slotsallocation on 6 nodes...
Using 3 masters:
192.168.56.56:7000
192.168.56.57:7003
192.168.56.56:7001
Adding replica 192.168.56.57:7004 to 192.168.56.56:7000
Adding replica 192.168.56.56:7002 to192.168.56.57:7003
Adding replica 192.168.56.57:7005 to192.168.56.56:7001
M: d2bf521e5a9b53c069da7507aafd69538ef693b5192.168.56.56:7000
slots:0-5460 (5461 slots) master
M: 3eb2c64654dc0559e83498753cd674805ba5fa42192.168.56.56:7001
slots:10923-16383 (5461 slots) master
S: d2ced0c0f1cf9158cdd40cdd910dacd4283ff6c1192.168.56.56:7002
replicates 5b75b320e858ec83fc39a40666700964ff598729
M: 5b75b320e858ec83fc39a40666700964ff598729192.168.56.57:7003
slots:5461-10922 (5462 slots) master
S: 20061dd97978756fa93dfe969a1d3acb3721b7a1192.168.56.57:7004
replicates d2bf521e5a9b53c069da7507aafd69538ef693b5
S: 70648f5b00ae17f2ca85adc405066aa7ef4d8007192.168.56.57:7005
replicates 3eb2c64654dc0559e83498753cd674805ba5fa42
Can I set the above configuration? (type'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different configepoch to each node
>>> Sending CLUSTER MEET messagesto join the cluster
Waiting for the cluster to join..
>>> Performing Cluster Check(using node 192.168.56.56:7000)
M: d2bf521e5a9b53c069da7507aafd69538ef693b5192.168.56.56:7000
slots:0-5460 (5461 slots) master
M: 3eb2c64654dc0559e83498753cd674805ba5fa42192.168.56.56:7001
slots:10923-16383 (5461 slots) master
M: d2ced0c0f1cf9158cdd40cdd910dacd4283ff6c1192.168.56.56:7002
slots: (0 slots) master
replicates 5b75b320e858ec83fc39a40666700964ff598729
M: 5b75b320e858ec83fc39a40666700964ff598729192.168.56.57:7003
slots:5461-10922 (5462 slots) master
M: 20061dd97978756fa93dfe969a1d3acb3721b7a1192.168.56.57:7004
slots: (0 slots) master
replicates d2bf521e5a9b53c069da7507aafd69538ef693b5
M: 70648f5b00ae17f2ca85adc405066aa7ef4d8007192.168.56.57:7005
slots: (0 slots) master
replicates 3eb2c64654dc0559e83498753cd674805ba5fa42
[OK] All nodes agree about slotsconfiguration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
3、测试
在192.168.56.56主机登录192.168.56.57的端口为7005的redis服务,设置一个key为cluster,value为hahaha。
[root@shanxi src]# ./redis-cli -c -h 192.168.56.57 -p 7005
192.168.56.57:7005> set cluster"hahaha"
-> Redirected to slot [14041] located at192.168.56.56:7001
OK
在192.168.56.57主机登录192.168.56.56的端口为7000的redis服务,可以取得cluster的值。
[root@localhost src]# ./redis-cli -c -h 192.168.56.56 -p 7000
192.168.56.56:7000> get cluster
-> Redirected to slot [14041] located at192.168.56.56:7001
"hahaha"
1、安装ruby , ruby下载地址:http://www.ruby-lang.org/en/downloads/
下载 Ruby 之后,解压到新创建的目录下:
[root@localhost redis]# tar -xvf ruby-2.3.1.tar
[root@localhost redis]# cd ruby-2.3.1
现在,配置并编译源代码,如下所示:
[root@localhost ruby-2.3.1]# ./configure
[root@localhost ruby-2.3.1]# make
[root@localhost ruby-2.3.1]# sudo make install
安装后,通过在命令行中输入以下命令来确保一切工作正常:
[root@localhost ruby-2.3.1]# ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768)[x86_64-linux]
2、安装RubyGems, RubyGems下载地址:https://rubygems.org/pages/download
下载 RubyGems 之后,解压到新创建的目录下:
[root@localhost redis]# unzip rubygems-2.6.8.zip
执行命令
[[email protected]]# ruby setup.rb
3、安装RubyGems 后,用gem来安装 redis接口 gem是ruby的一个工具包
[root@localhost rubygems-2.6.8]# gem install redis
ERROR: While executing gem ... (Gem::Exception)
Unable torequire openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPSsources
如上报错,没有redis包,到taobao下载一个,下载地址:https://rubygems.org/gems/redis
再次安装无问题。
[root@localhost gems]# gem install redis
Successfully installed redis-3.3.1
Parsing documentation for redis-3.3.1
Done installing documentation for redisafter 0 seconds
1 gem installed