下载安装:
wget http://download.redis.io/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
cd redis-stable
make
make install
部署配置
要让集群正常工作至少需要3个主节点,创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下:
192.168.45.214:7000
192.168.45.214:7001
192.168.45.214:7002
192.168.45.214:7003
192.168.45.214:7004
192.168.45.214:7005
安装目录:/opt/nosql/redis-3.0.5
集群配置目录:/opt/nosql/cluster
cluster目录下创建各个端口实例目录:7000、7001、7002、7003、7004、7005
拷贝安装目录下的redis.conf文件到各个端口实例目录下,并修改对应的端口号port值
主要修改的配置:
#修改配置文件中的下面选项
port 7000
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
启动管理
进入各个端口实例目录,启动:
redis-server redis.conf
启动后使用命令检测是否启动成功:
[root@storm214 cluster]# ps -ef|grep redis
root 5535 1 0 17:24 ? 00:00:00 redis-server *:7000 [cluster]
root 5545 1 0 17:25 ? 00:00:00 redis-server *:7001 [cluster]
root 5554 1 0 17:25 ? 00:00:00 redis-server *:7003 [cluster]
root 5565 1 0 17:26 ? 00:00:00 redis-server *:7002 [cluster]
root 5746 1 0 17:35 ? 00:00:00 redis-server *:7004 [cluster]
root 5750 1 0 17:35 ? 00:00:00 redis-server *:7005 [cluster]
root 5788 1883 0 17:45 pts/0 00:00:00 redis-cli -c -p 7000
创建集群,使用redis-trib进行管理
[root@storm214 src]# ./redis-trib.rb create --replicas 1 192.168.45.214:7000192.168.45.214:7001 192.168.45.214:7002 192.168.45.214:7003 192.168.45.214:7004192.168.45.214:7005
>>>Creating cluster
Connectingto node 192.168.45.214:7000: OK
Connectingto node 192.168.45.214:7001: OK
Connectingto node 192.168.45.214:7002: OK
Connectingto node 192.168.45.214:7003: OK
Connectingto node 192.168.45.214:7004: OK
Connectingto node 192.168.45.214:7005: OK
>>>Performing hash slots allocation on 6 nodes...
Using3 masters:
192.168.45.214:7000
192.168.45.214:7001
192.168.45.214:7002
Addingreplica 192.168.45.214:7003 to 192.168.45.214:7000
Addingreplica 192.168.45.214:7004 to 192.168.45.214:7001
Addingreplica 192.168.45.214:7005 to 192.168.45.214:7002
M:00d4045eff75c2472c12fb3486d9025b9d238adb 192.168.45.214:7000
slots:0-5460 (5461 slots) master
M:2d41985140681924f093b441ef995860213ba8d6 192.168.45.214:7001
slots:5461-10922 (5462 slots) master
M:30821b8d0de21d5ada36ef00054abf24a4e03800 192.168.45.214:7002
slots:10923-16383 (5461 slots) master
S:23f21c2f94a50be1708c37fc5adee9c9d82f0249 192.168.45.214:7003
replicates00d4045eff75c2472c12fb3486d9025b9d238adb
S:7dc6d9ca769187341fd2694ec1cbacfa996cecdf 192.168.45.214:7004
replicates2d41985140681924f093b441ef995860213ba8d6
S:b98f737a84b0d6dd0991e2e1238ff8e4ab552a77 192.168.45.214:7005
replicates30821b8d0de21d5ada36ef00054abf24a4e03800
Can Iset 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 cluster
Waitingfor the cluster to join.....
>>>Performing Cluster Check (using node 192.168.45.214:7000)
M:00d4045eff75c2472c12fb3486d9025b9d238adb 192.168.45.214:7000
slots:0-5460 (5461 slots) master
M: 2d41985140681924f093b441ef995860213ba8d6192.168.45.214:7001
slots:5461-10922 (5462 slots) master
M:30821b8d0de21d5ada36ef00054abf24a4e03800 192.168.45.214:7002
slots:10923-16383 (5461 slots) master
M:23f21c2f94a50be1708c37fc5adee9c9d82f0249 192.168.45.214:7003
slots: (0 slots) master
replicates00d4045eff75c2472c12fb3486d9025b9d238adb
M:7dc6d9ca769187341fd2694ec1cbacfa996cecdf 192.168.45.214:7004
slots: (0 slots) master
replicates2d41985140681924f093b441ef995860213ba8d6
M:b98f737a84b0d6dd0991e2e1238ff8e4ab552a77 192.168.45.214:7005
slots: (0 slots) master
replicates30821b8d0de21d5ada36ef00054abf24a4e03800
[OK]All nodes agree about slots configuration.
>>>Check for open slots...
>>>Check slots coverage...
[OK]All 16384 slots covered.
依赖报错
-
错误内容:
/usr/bin/env: ruby:No such file or directory
因为是执行的ruby的脚本,需要ruby的环境,推荐使用yum install ruby安装
yum install ruby
2.错误内容:
./redis-trib.rb:24:in`require': no such file to load -- rubygems (LoadError)
from./redis-trib.rb:24
缺少rubygems组件,使用yum安装
yum install rubygems
3.错误内容:
/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
缺少rubygems组件,使用yum安装
gem install redis
连接集群查看使用
[root@storm214 src]# redis-cli -c -p 7000
127.0.0.1:7000> cluster nodes
b98f737a84b0d6dd0991e2e1238ff8e4ab552a77192.168.45.214:7005 slave 30821b8d0de21d5ada36ef00054abf24a4e03800 01449654314454 6 connected
23f21c2f94a50be1708c37fc5adee9c9d82f0249192.168.45.214:7003 slave 00d4045eff75c2472c12fb3486d9025b9d238adb 01449654313454 4 connected
7dc6d9ca769187341fd2694ec1cbacfa996cecdf192.168.45.214:7004 slave 2d41985140681924f093b441ef995860213ba8d6 0 14496543124545 connected
00d4045eff75c2472c12fb3486d9025b9d238adb192.168.45.214:7000 myself,master - 0 0 1 connected 0-5460
2d41985140681924f093b441ef995860213ba8d6192.168.45.214:7001 master - 0 1449654309454 2 connected 5461-10922
30821b8d0de21d5ada36ef00054abf24a4e03800192.168.45.214:7002 master - 0 1449654315454 3 connected 10923-16383
127.0.0.1:7000>
在7000上执行命令:
127.0.0.1:7000> set bar 111
OK
127.0.0.1:7000> get bar
"111"
在7002上执行命令(MOVED错误并跳转到7000):
[root@storm214 cluster]# redis-cli -c -p 7002
127.0.0.1:7002> keys *
(empty list or set)
127.0.0.1:7002> get bar
-> Redirected to slot [5061] located at 192.168.45.214:7000
"111"
127.0.0.1:7000>
每个端口实例都会生成一些文件(nodes.conf里会实时更新集群中所有节点信息)
[root@storm214 7006]# pwd
/opt/nosql/cluster/7006
[root@storm214 7006]# ll
total 56
-rw-r--r--. 1 root root 86 Dec 9 23:17 appendonly.aof
-rw-r--r--. 1 root root 37 Dec 9 23:17 dump.rdb
-rw-r--r--. 1 root root 745 Dec 9 23:17 nodes.conf
-rw-r--r--. 1 root root 41551 Dec 9 23:17 redis.conf
[root@storm214 7006]# cat nodes.conf
30821b8d0de21d5ada36ef00054abf24a4e03800192.168.45.214:7002 master - 0 1449674113990 3 connected 10923-16383
00d4045eff75c2472c12fb3486d9025b9d238adb192.168.45.214:7000 myself,slave 23f21c2f94a50be1708c37fc5adee9c9d82f0249 0 0 1connected
b98f737a84b0d6dd0991e2e1238ff8e4ab552a77192.168.45.214:7005 slave 30821b8d0de21d5ada36ef00054abf24a4e03800 01449674113990 6 connected
2d41985140681924f093b441ef995860213ba8d6192.168.45.214:7001 slave 7dc6d9ca769187341fd2694ec1cbacfa996cecdf 01449674113990 7 connected
7dc6d9ca769187341fd2694ec1cbacfa996cecdf192.168.45.214:7004 master - 1449674113989 1449674113983 7 connected 5461-10922
23f21c2f94a50be1708c37fc5adee9c9d82f0249192.168.45.214:7003 master - 0 1449674113990 8 connected 0-5460
varscurrentEpoch 8 lastVoteEpoch 7
集群添加节点
1:首先把需要添加的节点配置并启动(同上)
例如:cluster目录下创建 7006,拷贝redis.conf文件,修改端口号port为7006
启动:redis-serverredis.conf
2:执行以下命令,将这个新节点添加到集群中
cd /opt/nosql/redis-3.0.5/src
./redis-trib.rb add-node192.168.45.214:7006 192.168.45.214:7000
3:执行命令redis-cli -c -p 7000 cluster nodes,查看刚才新增的节点
4:增加了新的节点之后,这个新的节点可以成为主节点或者是从节点
4.1把这个节点变成主节点,使用redis-trib程序,将集群中的某些哈希槽移动到新节点里面,这个新节点就成为真正的主节点了。
执行下面的命令对集群中的哈希槽进行移动
./redis-trib.rb reshard 192.168.45.214:7000
系统会提示我们要移动多少哈希槽,这里移动1000个,然后还需要指定把这些哈希槽转移到哪个节点上,输入我们刚才新增的节点的ID:f32dc088c881a6b930474fc5b52832ba2ff71899
然后需要我们指定转移哪几个几点的哈希槽,输入all 表示从所有的主节点中随机转移,凑够1000个哈希槽,然后再输入yes,redis集群就开始分配哈希槽了。
至此,一个新的主节点就添加完成了,执行命令查看现在的集群中节点的状态
redis-cli -c -p 7000 cluster nodes
4.2:把这个节点变成从节点
前面我们已经把这个新节点添加到集群中了,现在我们要让新节点成为192.168.45.214:7001的从节点,只需要执行下面的命令就可以了,命令后面的节点ID就是192.168.45.214:7001的节点ID
redis-cli -c -p 7006 cluster replicate23f21c2f94a50be1708c37fc5adee9c9d82f0249
集群删除节点
1:如果删除的节点是主节点,这里我们删除192.168.45.214:7006节点,这个节点有1000个哈希槽
首先要把节点中的哈希槽转移到其他节点中,执行下面的命令
./redis-trib.rb reshard 192.168.45.214:7000
系统会提示我们要移动多少哈希槽,这里移动1000个,因为192.168.45.214:7006节点有1000个哈希槽
然后系统提示我们输入要接收这些哈希槽的节点的ID,这里使用192.168.45.214:7001的节点ID
然后要我们选择从那些节点中转出哈希槽,这里一定要输入192.168.45.214:7006这个节点的ID,最后输入done 表示输入完毕
最后一步,使用下面的命令把这个节点删除,从集群中删除指定ID的节点
./redis-trib.rb del-node192.168.45.214:7000 ef55a9f273273517eb6a10e55f2200c606be3ce8
2:如果节点是从节点的,直接使用下面的命令删除即可。
./redis-trib.rb del-node192.168.45.214:7000 0826c82a136432bf5c5233dc62d4de85c44a1a40