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

来源:转载


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

cd /usr/commontar -zxvf ruby-2.2.2.tar.gzcd /usr/common/ruby-2.2.2./configure -prefix=/usr/common/rubymake && make installexport PATH=/usr/common/ruby/bin:$PATHsource /etc/profile

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

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

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

tar -zxvf rubygems-1.8.5.tgz cd /usr/common/rubygems-1.8.5 [[email protected] rubygems-1.8.5]# ruby setup.rb RubyGems 1.8.5 installed [[email protected] common]# gem -v 1.8.5

3:还需要安装redis的ruby library
(1)由于连接国外源不太稳定,请先删除,如gem sources –remove https://rubygems.org/ ,然后添加gem sources -a https://ruby.taobao.org/
(2)可以通过gem sources -l 查看源
(3)运行gem install redis
安装步骤:

[[email protected] common]# gem sources -l*** CURRENT SOURCES ***http://rubygems.org/ 1. gem sources --remove http://rubygems.org/ 2. gem sources -a https://ruby.taobao.org/ [[email protected] common]# gem sources -l*** CURRENT SOURCES ***https://ruby.taobao.org/ 3. [[email protected] common]# gem install redisSuccessfully installed redis-3.2.11 gem installedInstalling 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:6381127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 127.0.0.1:6385127.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 infocluster_state:failcluster_slots_assigned:0cluster_slots_ok:0cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:1cluster_size:0cluster_current_epoch:0cluster_my_epoch:0cluster_stats_messages_sent:0cluster_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 clusterConnecting to node 127.0.0.1:6381: OKConnecting to node 127.0.0.1:6382: OKConnecting to node 127.0.0.1:6383: OKConnecting to node 127.0.0.1:6384: OKConnecting to node 127.0.0.1:6385: OKConnecting to node 127.0.0.1:6386: OK>>> Performing hash slots allocation on 6 nodes...Using 3 masters:127.0.0.1:6381127.0.0.1:6382127.0.0.1:6383Adding replica 127.0.0.1:6384 to 127.0.0.1:6381Adding replica 127.0.0.1:6385 to 127.0.0.1:6382Adding replica 127.0.0.1:6386 to 127.0.0.1:6383M: 4730dd15e767a9ca1b845576d46339658a87864c 127.0.0.1:6381 slots:0-5460 (5461 slots) masterM: 672012b47699617ed26083281ffff9d00f7f55eb 127.0.0.1:6382 slots:5461-10922 (5462 slots) masterM: e3f4ebbca376cf7748cd0efbc3aba923cd68cc99 127.0.0.1:6383 slots:10923-16383 (5461 slots) masterS: c69b9e01bbc5d25e79d8d799a23bfed5ab86c837 127.0.0.1:6384 replicates 4730dd15e767a9ca1b845576d46339658a87864cS: 5fdec46e718236ea03f5c15798f8b4e6f94491bb 127.0.0.1:6385 replicates 672012b47699617ed26083281ffff9d00f7f55ebS: b49a83fb24262ccee856e0cb0576633466ebc232 127.0.0.1:6386 replicates e3f4ebbca376cf7748cd0efbc3aba923cd68cc99Can 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:6381)M: 4730dd15e767a9ca1b845576d46339658a87864c 127.0.0.1:6381 slots:0-5460 (5461 slots) masterM: 672012b47699617ed26083281ffff9d00f7f55eb 127.0.0.1:6382 slots:5461-10922 (5462 slots) masterM: e3f4ebbca376cf7748cd0efbc3aba923cd68cc99 127.0.0.1:6383 slots:10923-16383 (5461 slots) masterM: c69b9e01bbc5d25e79d8d799a23bfed5ab86c837 127.0.0.1:6384 slots: (0 slots) master replicates 4730dd15e767a9ca1b845576d46339658a87864cM: 5fdec46e718236ea03f5c15798f8b4e6f94491bb 127.0.0.1:6385 slots: (0 slots) master replicates 672012b47699617ed26083281ffff9d00f7f55ebM: 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 infocluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:6cluster_my_epoch:1cluster_stats_messages_sent:271cluster_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 connected5fdec46e718236ea03f5c15798f8b4e6f94491bb 127.0.0.1:6385 slave 672012b47699617ed26083281ffff9d00f7f55eb 0 1441680003386 5 connected4730dd15e767a9ca1b845576d46339658a87864c 127.0.0.1:6381 myself,master - 0 0 1 connected 0-5460672012b47699617ed26083281ffff9d00f7f55eb 127.0.0.1:6382 master - 0 1441680001383 2 connected 5461-10922c69b9e01bbc5d25e79d8d799a23bfed5ab86c837 127.0.0.1:6384 slave 4730dd15e767a9ca1b845576d46339658a87864c 0 1441680004388 4 connectede3f4ebbca376cf7748cd0efbc3aba923cd68cc99 127.0.0.1:6383 master - 0 1441680002686 3 connected 10923-16383vars 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 效果

[email protected] src]# ruby redis-trib.rb reshard 127.0.0.1:6381Connecting to node 127.0.0.1:6381: OKConnecting to node 127.0.0.1:6386: OKConnecting to node 127.0.0.1:6385: OKConnecting to node 127.0.0.1:6382: OKConnecting to node 127.0.0.1:6384: OKConnecting 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 e3f4ebbca376cf7748cd0efbc3aba923cd68cc99S: 5fdec46e718236ea03f5c15798f8b4e6f94491bb 127.0.0.1:6385 slots: (0 slots) slave replicates 672012b47699617ed26083281ffff9d00f7f55ebM: 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 4730dd15e767a9ca1b845576d46339658a87864cM: 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)? 2What is the receiving node ID? 4730dd15e767a9ca1b845576d46339658a87864cPlease 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:allReady 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 e3f4ebbca376cf7748cd0efbc3aba923cd68cc99Do you want to proceed with the proposed reshard plan (yes/no)? yesMoving 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 nodes4730dd15e767a9ca1b845576d46339658a87864c 127.0.0.1:6381 master - 0 1441681410290 7 connected 0-5462 10923-109245fdec46e718236ea03f5c15798f8b4e6f94491bb 127.0.0.1:6385 slave 672012b47699617ed26083281ffff9d00f7f55eb 0 1441681411292 5 connectedb49a83fb24262ccee856e0cb0576633466ebc232 127.0.0.1:6386 slave e3f4ebbca376cf7748cd0efbc3aba923cd68cc99 0 1441681412294 6 connected672012b47699617ed26083281ffff9d00f7f55eb 127.0.0.1:6382 myself,master - 0 0 2 connected 5463-10922c69b9e01bbc5d25e79d8d799a23bfed5ab86c837 127.0.0.1:6384 slave 4730dd15e767a9ca1b845576d46339658a87864c 0 1441681413296 7 connectede3f4ebbca376cf7748cd0efbc3aba923cd68cc99 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本身的复制功能的,如果主库快照数据文件过大,这个复制的过
程也会很久,同时会给主库带来压力。



分享给朋友:
您可能感兴趣的文章:
随机阅读: