为什么搭建redis集群
随着企业数据量的增多,Redis不论作为数据存储或是缓存,它的数据量也会逐渐增多,虽然Redis的速度非常可观,但随着其中的数据量的庞大,并且仅仅在一个设备或是一个Redis实例中,其存取速度也会大打折扣,所以我们需要在不同的设备或服务器上,搭建多个Redis实例仓库,将原来的Redis的所有的keys分发到各个服务器的Redis上,这就是现在所谓的Redis集群(Redis Cluster)
自己在两台服务器上搭建redis集群,每台服务器上创建3个实例,端口分别为 7000,7001,7002,7003,7004,7006。
将redis.conf文件分别复制到6个目录下面
安装ruby
1.yum -y install ruby ruby-devel rubygems rpm-build
2.gem install redis
2过程中出现(redis requires Ruby version >= 2.2.2)由于centos7中人version最高为2.0.0.所以采用rvm来更新ruby:
1.安装RVM:
gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
curl -L get.rvm.io | bash -s stable
find / -name rvm -print
/usr/local/rvm
/usr/local/rvm/src/rvm
/usr/local/rvm/src/rvm/bin/rvm
/usr/local/rvm/src/rvm/lib/rvm
/usr/local/rvm/src/rvm/scripts/rvm
/usr/local/rvm/bin/rvm
/usr/local/rvm/lib/rvm
/usr/local/rvm/scripts/rvm
source /usr/local/rvm/scripts/rvm
1
2.查看rvm库中已知的ruby版本
rvm list known
1
MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.6]
[ruby-]2.3[.3]
[ruby-]2.4[.0]
ruby-head
….
3.安装一个ruby版本
rvm install 2.3.3
1
No checksum for downloaded archive, recording checksum in user configuration.
ruby-2.3.3 - #extracting rubygems-2.6.12….
ruby-2.3.3 - #removing old rubygems………
ruby-2.3.3 - #installing rubygems-2.6.12…………………….
ruby-2.3.3 - #gemset created /usr/local/rvm/gems/ruby-2.3.3@global
ruby-2.3.3 - #importing gemset /usr/local/rvm/gemsets/global.gems…………..|
ruby-2.3.3 - #generating global wrappers……..
ruby-2.3.3 - #gemset created /usr/local/rvm/gems/ruby-2.3.3
ruby-2.3.3 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.3.3 - #generating default wrappers……..
ruby-2.3.3 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.3.3 - #complete
Ruby was built without documentation, to build it run: rvm docs generate-ri
4.使用一个ruby版本
rvm use 2.3.3
1
Using /usr/local/rvm/gems/ruby-2.3.3
[5].设置默认版本
rvm use 2.3.3 --default
1
Using /usr/local/rvm/gems/ruby-2.3.3
[6].卸载一个已知版本
rvm remove 2.0.0
1
查看ruby版本:
ruby --version
1
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
安装redis:
gem install redis
1
Fetching: redis-4.0.0.gem (100%)
Successfully installed redis-4.0.0
Parsing documentation for redis-4.0.0
Installing ri documentation for redis-4.0.0
Done installing documentation for redis after 1 seconds
1 gem installed
ok 测试一下!
/usr/local/redis-3.2.1/src/redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>
reshard host:port
--to <arg>
--yes
--slots <arg>
--from <arg>
check host:port
call host:port command arg arg .. arg
set-timeout host:port milliseconds
add-node new_host:new_port existing_host:existing_port
--master-id <arg>
--slave
del-node host:port node_id
fix host:port
import host:port
--from <arg>
help (show this help)
create host1:port1 ... hostN:portN
--replicas <arg>
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
此时我们分别启动7000,7001,...,7006端口(注意修改每个目录下面的配置文件里面的端口号,和端口文件,)
daemonize yes //redis后台运行
pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7002,7003
port 7000 //端口7000,7002,7003
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_7000.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout 5000 //请求超时 设置5秒够了
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
将bind改成0.0.0.0,不然创建集群的时候回报can't connect ip:port错误(出错之后修改配置文件后需要删除目录下面的node.conf不然回出现/usr/lib/ruby/gems/1.8/gems/redis-3.0.7/lib/redis/client.rb:97:in `call': ERR Slot 16011 is already busy (Redis::CommandError))
此时我们分别启动两台服务其的redis
然后创建集群