redis学习系列(二十)使用ruby 创建redis集群

2016-12-03 14:06:42来源:网络收集作者:Max人点击

第七城市

使用redis-trib.rb来操作集群
redis-trib.rb是Redis源码中提供的一个辅助工具,可以非常方便的来操作集群,它是用
ruby写的,因此需要在服务器上安装相应环境
1:安装Ruby
(1)下载ruby安装包,地址/2014th7cj/d/file/p/20161203/c45pxxudguh
(2)然后分别configure、make、make install
安装步骤:

cd /usr/common
tar -zxvf ruby-2.2.2.tar.gz
cd /usr/common/ruby-2.2.2
./configure -prefix=/usr/common/ruby
make && make install
export PATH=/usr/common/ruby/bin:$PATH
source /etc/profile

刷新环境变量
(3)安装后通过ruby -v 查看一下版本,看是否正常
效果:

ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]

ok安装成功
2:还需要安装rubygems
(1)下载包,地址/2014th7cj/d/file/p/20161203/k1lejn4foxi
(2)解压后进入解压文件夹,运行ruby setup.rb
(3)安装后通过gem –v查看一下版本,看是否正常
安装步骤:

tar -zxvfrubygems-1.8.5.tgz
cd /usr/common/rubygems-1.8.5
[root@iZ25ed9nobgZ rubygems-1.8.5]# ruby setup.rb
RubyGems 1.8.5 installed
[root@iZ25ed9nobgZ common]# gem -v
1.8.5

3:还需要安装redis的ruby library
(1)由于连接国外源不太稳定,请先删除,如gem sources –remove /2014th7cj/d/file/p/20161203/2cajropc5i3 ,然后添加gem sources -a /2014th7cj/d/file/p/20161203/5xsf15xoenw
(2)可以通过gem sources -l 查看源
(3)运行gem install redis
安装步骤:

[root@iZ25ed9nobgZ common]# gem sources -l
*** CURRENT SOURCES ***
/2014th7cj/d/file/p/20161203/yxs40qo3wtn
1. gem sources --remove /2014th7cj/d/file/p/20161203/yxs40qo3wtn
2. gem sources -a /2014th7cj/d/file/p/20161203/5xsf15xoenw
[root@iZ25ed9nobgZ common]#gem sources -l
*** CURRENT SOURCES ***
/2014th7cj/d/file/p/20161203/5xsf15xoenw
3.
[root@iZ25ed9nobgZ common]# gem install redis
Successfully installed redis-3.2.1
1 gem installed
Installing ri documentation for redis-3.2.1...
Installing RDoc documentation for redis-3.2.1..

4:使用redis-trib.rb来初始化集群,形如:

ruby redis-trib.rb create --replicas 1 127.0.0.1:6381
127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385
127.0.0.1:6386

create表示要初始化集群,–replicas 1表示每个驻数据库拥有的从数据库为1个
代码如下:
1.启动redis

/usr/local/bin/redis-server /usr/common/redis3/redis6381.conf
/usr/local/bin/redis-server /usr/common/redis3/redis6382.conf
/usr/local/bin/redis-server /usr/common/redis3/redis6383.conf
/usr/local/bin/redis-server /usr/common/redis3/redis6384.conf
/usr/local/bin/redis-server /usr/common/redis3/redis6385.conf
/usr/local/bin/redis-server /usr/common/redis3/redis6386.conf

2.查看集群状态

127.0.0.1:6381> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0

3.进入/usr/common/redis-3.0.3/src目录
查找redis-trib.rb命令
4.初始化集群

ruby redis-trib.rb create --replicas 1 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385 127.0.0.1:6386

效果如下:

>>> Creating cluster
Connecting to node 127.0.0.1:6381: OK
Connecting to node 127.0.0.1:6382: OK
Connecting to node 127.0.0.1:6383: OK
Connecting to node 127.0.0.1:6384: OK
Connecting to node 127.0.0.1:6385: OK
Connecting to node 127.0.0.1:6386: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:6381
127.0.0.1:6382
127.0.0.1:6383
Adding replica 127.0.0.1:6384 to 127.0.0.1:6381
Adding replica 127.0.0.1:6385 to 127.0.0.1:6382
Adding replica 127.0.0.1:6386 to 127.0.0.1:6383
M: 4730dd15e767a9ca1b845576d46339658a87864c 127.0.0.1:6381
slots:0-5460 (5461 slots) master
M: 672012b47699617ed26083281ffff9d00f7f55eb 127.0.0.1:6382
slots:5461-10922 (5462 slots) master
M: e3f4ebbca376cf7748cd0efbc3aba923cd68cc99 127.0.0.1:6383
slots:10923-16383 (5461 slots) master
S: c69b9e01bbc5d25e79d8d799a23bfed5ab86c837 127.0.0.1:6384
replicates 4730dd15e767a9ca1b845576d46339658a87864c
S: 5fdec46e718236ea03f5c15798f8b4e6f94491bb 127.0.0.1:6385
replicates 672012b47699617ed26083281ffff9d00f7f55eb
S: b49a83fb24262ccee856e0cb0576633466ebc232 127.0.0.1:6386
replicates e3f4ebbca376cf7748cd0efbc3aba923cd68cc99
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:6381)
M: 4730dd15e767a9ca1b845576d46339658a87864c 127.0.0.1:6381
slots:0-5460 (5461 slots) master
M: 672012b47699617ed26083281ffff9d00f7f55eb 127.0.0.1:6382
slots:5461-10922 (5462 slots) master
M: e3f4ebbca376cf7748cd0efbc3aba923cd68cc99 127.0.0.1:6383
slots:10923-16383 (5461 slots) master
M: c69b9e01bbc5d25e79d8d799a23bfed5ab86c837 127.0.0.1:6384
slots: (0 slots) master
replicates 4730dd15e767a9ca1b845576d46339658a87864c
M: 5fdec46e718236ea03f5c15798f8b4e6f94491bb 127.0.0.1:6385
slots: (0 slots) master
replicates 672012b47699617ed26083281ffff9d00f7f55eb
M: b49a83fb24262ccee856e0cb0576633466ebc232 127.0.0.1:6386
slots: (0 slots) master
replicates e3f4ebbca376cf7748cd0efbc3aba923cd68cc99
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

5.查看集群的状态

127.0.0.1:6381> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:271
cluster_stats_messages_received:271

集群的初始化ok
5:使用redis-trib.rb来迁移插槽,如下:
(1)执行ruby redis-trib.rb reshard ip:port ,这就告诉Redis要重新分片,ip:port可以是集群中任何一个节点
(2)然后按照提示去做就可以了
(3)这种方式不能指定要迁移的插槽号
代码如下:
1.查看默认的分片情况more nodes-6381.conf

b49a83fb24262ccee856e0cb0576633466ebc232 127.0.0.1:6386 slave e3f4ebbca376cf7748cd0efbc3aba923cd68cc99 0 1441680002385 6 connected
5fdec46e718236ea03f5c15798f8b4e6f94491bb 127.0.0.1:6385 slave 672012b47699617ed26083281ffff9d00f7f55eb 0 1441680003386 5 connected
4730dd15e767a9ca1b845576d46339658a87864c 127.0.0.1:6381 myself,master - 0 0 1 connected 0-5460
672012b47699617ed26083281ffff9d00f7f55eb 127.0.0.1:6382 master - 0 1441680001383 2 connected 5461-10922
c69b9e01bbc5d25e79d8d799a23bfed5ab86c837 127.0.0.1:6384 slave 4730dd15e767a9ca1b845576d46339658a87864c 0 1441680004388 4 connected
e3f4ebbca376cf7748cd0efbc3aba923cd68cc99 127.0.0.1:6383 master - 0 1441680002686 3 connected 10923-16383
vars currentEpoch 6 lastVoteEpoch 0

2.迁移
k1在83 现在迁移到81

127.0.0.1:6383> get k1
"v1"

3.执行ruby redis-trib.rb reshard 127.0.0.1:6381 效果

root@iZ25ed9nobgZ src]# ruby redis-trib.rb reshard 127.0.0.1:6381
Connecting to node 127.0.0.1:6381: OK
Connecting to node 127.0.0.1:6386: OK
Connecting to node 127.0.0.1:6385: OK
Connecting to node 127.0.0.1:6382: OK
Connecting to node 127.0.0.1:6384: OK
Connecting to node 127.0.0.1:6383: OK
>>> Performing Cluster Check (using node 127.0.0.1:6381)
M: 4730dd15e767a9ca1b845576d46339658a87864c 127.0.0.1:6381
slots:0-5462 (5463 slots) master
1 additional replica(s)
S: b49a83fb24262ccee856e0cb0576633466ebc232 127.0.0.1:6386
slots: (0 slots) slave
replicates e3f4ebbca376cf7748cd0efbc3aba923cd68cc99
S: 5fdec46e718236ea03f5c15798f8b4e6f94491bb 127.0.0.1:6385
slots: (0 slots) slave
replicates 672012b47699617ed26083281ffff9d00f7f55eb
M: 672012b47699617ed26083281ffff9d00f7f55eb 127.0.0.1:6382
slots:5463-10922 (5460 slots) master
1 additional replica(s)
S: c69b9e01bbc5d25e79d8d799a23bfed5ab86c837 127.0.0.1:6384
slots: (0 slots) slave
replicates 4730dd15e767a9ca1b845576d46339658a87864c
M: e3f4ebbca376cf7748cd0efbc3aba923cd68cc99 127.0.0.1:6383
slots:10923-16383 (5461 slots) master
1 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)? 2
What is the receiving node ID? 4730dd15e767a9ca1b845576d46339658a87864c
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:all
Ready to move 2 slots.
Source nodes:
M: 672012b47699617ed26083281ffff9d00f7f55eb 127.0.0.1:6382
slots:5463-10922 (5460 slots) master
1 additional replica(s)
M: e3f4ebbca376cf7748cd0efbc3aba923cd68cc99 127.0.0.1:6383
slots:10923-16383 (5461 slots) master
1 additional replica(s)
Destination node:
M: 4730dd15e767a9ca1b845576d46339658a87864c 127.0.0.1:6381
slots:0-5462 (5463 slots) master
1 additional replica(s)
Resharding plan:
Moving slot 10923 from e3f4ebbca376cf7748cd0efbc3aba923cd68cc99
Moving slot 10924 from e3f4ebbca376cf7748cd0efbc3aba923cd68cc99
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 10923 from 127.0.0.1:6383 to 127.0.0.1:6381:
Moving slot 10924 from 127.0.0.1:6383 to 127.0.0.1:6381:

4.再次查看分片情况more nodes-6381.conf

127.0.0.1:6382> cluster nodes
4730dd15e767a9ca1b845576d46339658a87864c 127.0.0.1:6381 master - 0 1441681410290 7 connected 0-5462 10923-10924
5fdec46e718236ea03f5c15798f8b4e6f94491bb 127.0.0.1:6385 slave 672012b47699617ed26083281ffff9d00f7f55eb 0 1441681411292 5 connected
b49a83fb24262ccee856e0cb0576633466ebc232 127.0.0.1:6386 slave e3f4ebbca376cf7748cd0efbc3aba923cd68cc99 0 1441681412294 6 connected
672012b47699617ed26083281ffff9d00f7f55eb 127.0.0.1:6382 myself,master - 0 0 2 connected 5463-10922
c69b9e01bbc5d25e79d8d799a23bfed5ab86c837 127.0.0.1:6384 slave 4730dd15e767a9ca1b845576d46339658a87864c 0 1441681413296 7 connected
e3f4ebbca376cf7748cd0efbc3aba923cd68cc99 127.0.0.1:6383 master - 0 1441681407287 3 connected 10925-16383

区间已经变化 插槽分配ok


Redis预分区思路:


为了实现在线动态扩容和数据分区,Redis的作者提出了预分区的方案,实际就是在同一台机器上部署多个Redis实例,当容量不够时将多个实例拆分到不同的机器上,这样就达到了扩容的效果。拆分过程如下:
1:在新机器上启动好对应端口的Redis实例
2:配置新端口为待迁移端口的从库
3:待复制完成,与主库完成同步后,切换所有客户端配置到新的从库的端口
4:配置从库为新的主库
5:移除老的端口实例
6:重复上述过程把要迁移的数据库转移到指定服务器上
以上拆分流程是Redis作者提出的一个平滑迁移的过程,不过该拆分方法还
是很依赖Redis本身的复制功能的,如果主库快照数据文件过大,这个复制的过
程也会很久,同时会给主库带来压力。

版权声明:本文为博主原创文章,未经博主允许不得转载。

第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台