创建6个redis节点,其中3个为主节点,3个为从节点。
redis节点的ip和端口对应关系如下:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
127.0.0.1:7004
127.0.0.1:7005
下载redis3.0.4。(2.*不支持集群模式)
官网下载地址: http://download.redis.io/releases/redis-3.0.4.tar.gz安装redis
若服务器已联网则通过下面命令进行安装,否则就通过官网下载地址下载上传到服务器。(我安装在/usr/local/src/目录下)#下载测试redis是否安装成功。
[root@localhost src]# wget http://download.redis.io/releases/redis-3.0.4.tar.gz
[root@localhost src]# tar zxvf redis-3.0.4.tar.gz
[root@localhost src]# cd redis-3.0.4
#如果不加参数,linux下会报错
[root@localhost src]# make MALLOC=libc
[root@localhost src]# make install
#启动redis成功后显示如下:
[root@localhost redis-3.0.4]# src/redis-server
#关闭redis
[root@localhost redis-3.0.4]# src/redis-cli shutdown
测试redis:
[root@localhost redis-3.0.4]# src/redis-cli关闭redis。
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379>
[root@localhost redis-3.0.4]# src/redis-cli
127.0.0.1:6379> set foo csdn
OK
127.0.0.1:6379> get foo
"csdn"
127.0.0.1:6379>
redis cluster集群搭建
1:创建集群所需目录
[root@localhost cluster]# mkdir /usr/local/src/cluster修改配置文件中的下面选项
[root@localhost src]# cd cluster/
[root@localhost cluster]# mkdir 7001
[root@localhost cluster]# cp /usr/local/src/redis-3.0.4/redis.conf /usr/local/src/cluster/7001/
[root@localhost cluster]# cp /usr/local/src/redis-3.0.4/src/redis-server /usr/local/src/cluster/7001/
[root@localhost cluster]# vi 7001/redis.conf
port 7001修改完redis.conf配置文件中的配置项后,执行下面命令:
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
[root@localhost cluster]# cp -r 7001/ 7002注意:复制完文件夹后,修改每个文件夹下的redis.conf文件中的port配置项。
[root@localhost cluster]# cp -r 7001/ 7003
[root@localhost cluster]# cp -r 7001/ 7004
[root@localhost cluster]# cp -r 7001/ 7005
[root@localhost cluster]# cp -r 7001/ 7006
2:创建启动脚本:
[root@localhost cluster]# vi redis-start.sh保存启动脚本后,授予权限:
cd /usr/local/src/cluster/7001/
redis-server redis.conf
cd /usr/local/src/cluster/7002/
redis-server redis.conf
cd /usr/local/src/cluster/7003/
redis-server redis.conf
cd /usr/local/src/cluster/7004/
redis-server redis.conf
cd /usr/local/src/cluster/7005/
redis-server redis.conf
cd /usr/local/src/cluster/7006/
redis-server redis.conf
#给sh文件授权权限
[root@localhost cluster]# chmod +x redis-start.sh
#启动所有redis节点
[root@localhost cluster]# ./redis-start.sh
3:执行redis的创建集群命令创建集群
[root@localhost cluster]# cd /usr/local/src/redis-3.0.4/src/3.1执行上面的命令的时候会报错,因为是执行的ruby的脚本,需要ruby的环境
[root@localhost src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
错误内容:/usr/bin/env: ruby: No such file or directory
所以需要安装ruby的环境,这里推荐使用yum install ruby安装
[root@localhost cluster]# yum install ruby3.2然后再执行第步的创建集群命令,还会报错,提示缺少rubygems组件
错误内容:
./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
from ./redis-trib.rb:24
[root@localhost cluster]# yum install rubygems3.3再次执行第步的命令,还会报错,提示不能加载redis,是因为缺少redis的接口
错误内容:
/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
[root@localhost cluster]# gem install redis这里可能无法安装,因为无法连接gem服务器:
[root@localhost src]# gem install redis --version 3.0.0需要手工下载并安装:(下载链接: http://download.csdn.net/detail/ltr15036900300/9172823)
ERROR: Could not find a valid gem 'redis' (= 3.0.0) in any repository
ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError)
gem install -l ./redis-3.2.1.gem3.4再次执行第步的命令,正常执行
[root@localhost src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
输入yes,然后配置完成。[root@localhost src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7000>>> Creating clusterConnecting to node 127.0.0.1:7001: OKConnecting to node 127.0.0.1:7002: OKConnecting to node 127.0.0.1:7003: OKConnecting to node 127.0.0.1:7004: OKConnecting to node 127.0.0.1:7005: OKConnecting to node 127.0.0.1:7006: OK>>> Performing hash slots allocation on 6 nodes...Using 3 masters:127.0.0.1:7001127.0.0.1:7002127.0.0.1:7003Adding replica 127.0.0.1:7004 to 127.0.0.1:7001Adding replica 127.0.0.1:7005 to 127.0.0.1:7002Adding replica 127.0.0.1:7006 to 127.0.0.1:7003M: 2022f24d581b4a7c3342e3245c32927cbd5ec16d 127.0.0.1:7001 slots:0-5460 (5461 slots) masterM: 37b7008f80f8c21a698da8cb1f1b32db8c0c415c 127.0.0.1:7002 slots:5461-10922 (5462 slots) masterM: ac6dc5fa96e856b34c1ba4c3814394e4ebb698dd 127.0.0.1:7003 slots:10923-16383 (5461 slots) masterS: b5b76d70bbb0dbf3e7df8a38f1259e95e2054721 127.0.0.1:7004 replicates 2022f24d581b4a7c3342e3245c32927cbd5ec16dS: 6881f8fef9c25da486f320ebf2ead39c1502db4c 127.0.0.1:7005 replicates 37b7008f80f8c21a698da8cb1f1b32db8c0c415cS: f090526d32cced97731eef2a2e1722a7bac7d9ea 127.0.0.1:7006 replicates ac6dc5fa96e856b34c1ba4c3814394e4ebb698ddCan I set the above configuration? (type 'yes' to accept): yes>>> Nodes configuration updated>>> Assign a different config epoch to each node>>> Sending CLUSTER MEET messages to join the clusterWaiting for the cluster to join...>>> Performing Cluster Check (using node 127.0.0.1:7001)M: 2022f24d581b4a7c3342e3245c32927cbd5ec16d 127.0.0.1:7001 slots:0-5460 (5461 slots) masterM: 37b7008f80f8c21a698da8cb1f1b32db8c0c415c 127.0.0.1:7002 slots:5461-10922 (5462 slots) masterM: ac6dc5fa96e856b34c1ba4c3814394e4ebb698dd 127.0.0.1:7003 slots:10923-16383 (5461 slots) masterM: b5b76d70bbb0dbf3e7df8a38f1259e95e2054721 127.0.0.1:7004 slots: (0 slots) master replicates 2022f24d581b4a7c3342e3245c32927cbd5ec16dM: 6881f8fef9c25da486f320ebf2ead39c1502db4c 127.0.0.1:7005 slots: (0 slots) master replicates 37b7008f80f8c21a698da8cb1f1b32db8c0c415cM: f090526d32cced97731eef2a2e1722a7bac7d9ea 127.0.0.1:7006 slots: (0 slots) master replicates ac6dc5fa96e856b34c1ba4c3814394e4ebb698dd[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
4:redis-cli命令进入集群环境
[root@localhost cluster]# redis-cli -c -p 7001
参考文章: