首页 > 代码库 > 用redis-trib.rb搭建redis集群

用redis-trib.rb搭建redis集群

1,准备ruby环境

安装ruby

 wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.gz
 tar xvf ruby-2.4.1.tar.gz 
 cd ruby-2.4.1.tar.gz
 cd ruby-2.4.1
 ./configure --prefix=/usr/local/ruby
 make
 make install
 cp bin/ruby /usr/local/bin/
 cp bin/gem /usr/local/bin/

 

安装rubygem redis依赖:

 wget http://rubygems.org/downloads/redis-3.3.0.gem
 gem install -l redis-3.3.0.gem 
 gem list | grep redis

 

安装redis-trib.rb:

cp /opt/redis/redis-3.2.4/src/redis-trib.rb /usr/local/bin/

/opt/redis/redis-3.2.4/目录对应的redis源码包解压目录


2,准备集群节点

在普通redis配置文件中加上这三项即可:

cluster-enabled yes  #开启群集模式
cluster-node-timeout 15000  #节点超时时间,单位为毫秒
cluster-config-file "nodes-6379.conf"  #指定集群内部的配置文件
ll /data/app/redis3.2.4/conf
-rw-r--r--. 1 root  root  2229 Jul 11 01:50 redis_6479.conf
-rw-r--r--. 1 root  root  2229 Jul 11 01:50 redis_6480.conf
-rw-r--r--. 1 root  root  2229 Jul 11 01:51 redis_6481.conf
-rw-r--r--. 1 root  root  2229 Jul 11 01:51 redis_6482.conf
-rw-r--r--. 1 root  root  2229 Jul 11 01:51 redis_6483.conf
-rw-r--r--. 1 root  root  2229 Jul 11 01:51 redis_6484.conf


启动节点:

redis-server redis_6479.conf
redis-server redis_6480.conf
redis-server redis_6481.conf
redis-server redis_6482.conf
redis-server redis_6483.conf
redis-server redis_6484.conf


3,创建集群

redis-trib.rb create --replicas 1 127.0.0.1:6479 127.0.0.1:6480 127.0.0.1:6481 127.0.0.1:6482 127.0.0.1:6483 127.0.0.1:6484

# --replicas 1 代表每个主节点配置多少个从节点


>>> Creating cluster

/usr/local/ruby/lib/ruby/gems/2.4.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated

>>> Performing hash slots allocation on 6 nodes...

Using 3 masters:

127.0.0.1:6479

127.0.0.1:6480

127.0.0.1:6481

Adding replica 127.0.0.1:6482 to 127.0.0.1:6479

Adding replica 127.0.0.1:6483 to 127.0.0.1:6480

Adding replica 127.0.0.1:6484 to 127.0.0.1:6481

M: 78581cf153d821c734c238d00f4de264fc95072e 127.0.0.1:6479

   slots:0-5460 (5461 slots) master

M: d0d4a89f567aed7caa4b87e78be2b6b1b86d02d3 127.0.0.1:6480

   slots:5461-10922 (5462 slots) master

M: e71eae6b79e529997fb7716da6d20be68e76830d 127.0.0.1:6481

   slots:10923-16383 (5461 slots) master

S: cd2f4e96f575b7b84d48144e832bf7cd6fcd8fc1 127.0.0.1:6482

   replicates 78581cf153d821c734c238d00f4de264fc95072e

S: 89e54b0f1fd33adf5ef624104e7d17bb93785b3d 127.0.0.1:6483

   replicates d0d4a89f567aed7caa4b87e78be2b6b1b86d02d3

S: b1ad93513254bb6bcb3cc6166afc7d4a89458802 127.0.0.1:6484

   replicates e71eae6b79e529997fb7716da6d20be68e76830d

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:6479)

M: 78581cf153d821c734c238d00f4de264fc95072e 127.0.0.1:6479

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: cd2f4e96f575b7b84d48144e832bf7cd6fcd8fc1 127.0.0.1:6482

   slots: (0 slots) slave

   replicates 78581cf153d821c734c238d00f4de264fc95072e

S: b1ad93513254bb6bcb3cc6166afc7d4a89458802 127.0.0.1:6484

   slots: (0 slots) slave

   replicates e71eae6b79e529997fb7716da6d20be68e76830d

M: d0d4a89f567aed7caa4b87e78be2b6b1b86d02d3 127.0.0.1:6480

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

M: e71eae6b79e529997fb7716da6d20be68e76830d 127.0.0.1:6481

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 89e54b0f1fd33adf5ef624104e7d17bb93785b3d 127.0.0.1:6483

   slots: (0 slots) slave

   replicates d0d4a89f567aed7caa4b87e78be2b6b1b86d02d3

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.



4,集群完整性检查

redis-trib.rb check 127.0.0.1:6479

/usr/local/ruby/lib/ruby/gems/2.4.0/gems/redis-3.3.0/lib/redis/client.rb:459: warning: constant ::Fixnum is deprecated

>>> Performing Cluster Check (using node 127.0.0.1:6479)

M: 78581cf153d821c734c238d00f4de264fc95072e 127.0.0.1:6479

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: cd2f4e96f575b7b84d48144e832bf7cd6fcd8fc1 127.0.0.1:6482

   slots: (0 slots) slave

   replicates 78581cf153d821c734c238d00f4de264fc95072e

S: b1ad93513254bb6bcb3cc6166afc7d4a89458802 127.0.0.1:6484

   slots: (0 slots) slave

   replicates e71eae6b79e529997fb7716da6d20be68e76830d

M: d0d4a89f567aed7caa4b87e78be2b6b1b86d02d3 127.0.0.1:6480

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

M: e71eae6b79e529997fb7716da6d20be68e76830d 127.0.0.1:6481

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 89e54b0f1fd33adf5ef624104e7d17bb93785b3d 127.0.0.1:6483

   slots: (0 slots) slave

   replicates d0d4a89f567aed7caa4b87e78be2b6b1b86d02d3

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

当看到[OK] All 16384 slots covered.时代表集群所有的槽位都已经分配出去。

本文出自 “一直在路上” 博客,请务必保留此出处http://chenql.blog.51cto.com/8732050/1947291

用redis-trib.rb搭建redis集群