安装Redis
1、下载Redis,由于现在最新的Redis版本已经到了3.0.4,所以测试使用的版本就是3.0.4.
下载地址:http://redis.io/
2、下载的文件名为:redis-3.0.3.tar.gz,使用下面命令安装redis:
ytc:local ytc$ cd /usr/local/
ytc:local ytc$ mv ~/Downloads/redis-3.0.3.tar.gz .
ytc:local ytc$ tar xzf redis-3.0.3.tar.gz
ytc:local ytc$ cd redis-3.0.3
ytc:redis-3.0.3 ytc$ make && make install
ytc:local ytc$ cd /usr/local/
ytc:local ytc$ mv redis-3.0.3 redis
修改/etc/bashrc,在文件末尾增加一行:
PATH=$PATH:/usr/local/redis/src
如上Redis安装完成。
安装Redis Cluster
安装Redis完成后,尝试搭建Redis Cluster:
ytc:local ytc$ cd /opt/
ytc:opt ytc$ mkdir redisDir
ytc:opt ytc$ cd redisDir
ytc:redisDir ytc$ mkdir 9001
ytc:redisDir ytc$ mkdir 9002
ytc:redisDir ytc$ mkdir 9003
ytc:redisDir ytc$ mkdir 9004
在900*各个目录下新建文件config.conf,文件内容如下:
daemonize yes
port 9001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
第二行的端口号数字与目录名一致。
ytc:redisDir ytc$ cd 9001
ytc:9001 ytc$ redis-server config.conf
ytc:9001 ytc$ cd ../9002
ytc:9002 ytc$ redis-server config.conf
ytc:9002 ytc$ cd ../9003
ytc:9003 ytc$ redis-server config.conf
ytc:9003 ytc$ cd ../9004
ytc:9004 ytc$ redis-server config.conf
使用“ps -fax”命令查看进程,可以看到如下4个进程,它们就是刚刚启动的4个Redis实例:
501 16287 1 0 3:46PM ?? 0:00.05 redis-server *:9001 [cluster]
501 16289 1 0 3:46PM ?? 0:00.04 redis-server *:9002 [cluster]
501 16291 1 0 3:47PM ?? 0:00.04 redis-server *:9003 [cluster]
501 16293 1 0 3:47PM ?? 0:00.04 redis-server *:9004 [cluster]
报上面错误说明Ruby未安装Redis部分,使用gem安装redis部分:
ytc:9004 ytc$ gem install redis
ERROR: While executing gem ... (Gem::Exception)
Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources
gem源找不到Redis,修改源:
ytc:9004 ytc$ gem sources
*** CURRENT SOURCES ***
https://rubygems.org/
ytc:9004 ytc$ gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources
ytc:9004 ytc$ gem sources -a http://ruby.taobao.org/
http://ruby.taobao.org/ added to sources
ytc:9004 ytc$ gem install redis
Fetching: redis-3.2.1.gem (100%)
Successfully installed redis-3.2.1
Parsing documentation for redis-3.2.1
Installing ri documentation for redis-3.2.1
Done installing documentation for redis after 1 seconds
1 gem installed
ytc:9004 ytc$
然后重新构建Redis集群:
ytc:9004 ytc$ redis-trib.rb create --replicas 0 127.0.0.1:9001 127.0.0.1:9002 127.0.0.1:9003
>>> Creating cluster
Connecting to node 127.0.0.1:9001: OK
Connecting to node 127.0.0.1:9002: OK
Connecting to node 127.0.0.1:9003: OK
>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
127.0.0.1:9001
127.0.0.1:9002
127.0.0.1:9003
M: 6df75f479092037a21ec556431e7251ad89fdf88 127.0.0.1:9001
slots:0-5460 (5461 slots) master
M: 61dfcda57e67a89e31af0d49bf81c8cb1e09beb7 127.0.0.1:9002
slots:5461-10922 (5462 slots) master
M: 60c3b50d8464ca40acef55aa33293044a608baa0 127.0.0.1:9003
slots:10923-16383 (5461 slots) master
Can 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 cluster
Waiting for the cluster to join.
>>> Performing Cluster Check (using node 127.0.0.1:9001)
M: 6df75f479092037a21ec556431e7251ad89fdf88 127.0.0.1:9001
slots:0-5460 (5461 slots) master
M: 61dfcda57e67a89e31af0d49bf81c8cb1e09beb7 127.0.0.1:9002
slots:5461-10922 (5462 slots) master
M: 60c3b50d8464ca40acef55aa33293044a608baa0 127.0.0.1:9003
slots:10923-16383 (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
ytc:9004 ytc$
构建成功,登录集群时需要使用“-c”参数:
ytc:9004 ytc$ redis-cli -c -p 9001
127.0.0.1:9001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_sent:180
cluster_stats_messages_received:180
127.0.0.1:9001>
127.0.0.1:9001>
127.0.0.1:9001> cluster nodes
60c3b50d8464ca40acef55aa33293044a608baa0 127.0.0.1:9003 master - 0 1441958332495 3 connected 10923-16383
6df75f479092037a21ec556431e7251ad89fdf88 127.0.0.1:9001 myself,master - 0 0 1 connected 0-5460
61dfcda57e67a89e31af0d49bf81c8cb1e09beb7 127.0.0.1:9002 master - 0 1441958331990 2 connected 5461-10922
127.0.0.1:9001>
可以看出集群中有3个node,每个node覆盖了大概1/3的slot,下面我们增加一个node,并且重新将所有的slots平均分布到4个node上。
增加新node:
127.0.0.1:9001> cluster meet 127.0.0.1 9004
OK
127.0.0.1:9001>
为新node分配节点:
ytc:9004 ytc$ redis-trib.rb -reshard 127.0.0.1:9004
Unknown redis-trib subcommand '-reshard'
ytc:9004 ytc$ redis-trib.rb --reshard 127.0.0.1:9004
Unknown redis-trib subcommand '--reshard'
ytc:9004 ytc$ redis-trib.rb reshard 127.0.0.1:9004
Connecting to node 127.0.0.1:9004: OK
Connecting to node 127.0.0.1:9002: OK
Connecting to node 127.0.0.1:9003: OK
Connecting to node 127.0.0.1:9001: OK
>>> Performing Cluster Check (using node 127.0.0.1:9004)
M: 7b8fee600f898662bc3f065747f590c986c10a0f 127.0.0.1:9004
slots: (0 slots) master
0 additional replica(s)
M: 61dfcda57e67a89e31af0d49bf81c8cb1e09beb7 127.0.0.1:9002
slots:5461-10922 (5462 slots) master
0 additional replica(s)
M: 60c3b50d8464ca40acef55aa33293044a608baa0 127.0.0.1:9003
slots:10923-16383 (5461 slots) master
0 additional replica(s)
M: 6df75f479092037a21ec556431e7251ad89fdf88 127.0.0.1:9001
slots:0-5460 (5461 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4000
What is the receiving node ID? 7b8fee600f898662bc3f065747f590c986c10a0f
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1:61dfcda57e67a89e31af0d49bf81c8cb1e09beb7
Source node #2:60c3b50d8464ca40acef55aa33293044a608baa0
Source node #3:6df75f479092037a21ec556431e7251ad89fdf88
Source node #4:done
Moving slot 1329 from 6df75f479092037a21ec556431e7251ad89fdf88
Moving slot 1330 from 6df75f479092037a21ec556431e7251ad89fdf88
…………
Moving slot 1331 from 6df75f479092037a21ec556431e7251ad89fdf88
Moving slot 1332 from 6df75f479092037a21ec556431e7251ad89fdf88
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 1330 from 127.0.0.1:9001 to 127.0.0.1:9004:
Moving slot 1331 from 127.0.0.1:9001 to 127.0.0.1:9004:
Moving slot 1332 from 127.0.0.1:9001 to 127.0.0.1:9004:
ytc:9004 ytc$
上面是指从9001、9002、9003中取出4000个slots放到9004上。
执行完成后,4个node的slot分布情况为:
ytc:9004 ytc$ redis-cli -c -p 9001
127.0.0.1:9001> cluster nodes
60c3b50d8464ca40acef55aa33293044a608baa0 127.0.0.1:9003 master - 0 1441959130090 3 connected 12256-16383
6df75f479092037a21ec556431e7251ad89fdf88 127.0.0.1:9001 myself,master - 0 0 1 connected 1333-5460
61dfcda57e67a89e31af0d49bf81c8cb1e09beb7 127.0.0.1:9002 master - 0 1441959132106 2 connected 6795-10922
7b8fee600f898662bc3f065747f590c986c10a0f 127.0.0.1:9004 master - 0 1441959131098 4 connected 0-1332 5461-6794 10923-12255
127.0.0.1:9001>