redis集群节点添加

Redis 动态添加节点

 

redis可以动态热部署,数据动态迁移

 

新建两个服务,跟创建集群时创建节点一样

1.创建两个节点文件夹6004 7004

2.拷贝一个节点的redis部署文件到6004 7004 文件下

cp -rf 6001/*   7001

3.拷贝redis.conf 到文件夹下。

sh-3.2# cp 6001/redis.conf 6004

sh-3.2# cp 6001/redis.conf 7004

sh-3.2# vi 6004/redis.conf 【修改配置】

sh-3.2# vi 7004/redis.conf 

 

4.启动服务

/usr/local/bin/redis-server /usr/local/cluster-redis/6004/redis.conf

/usr/local/bin/redis-server /usr/local/cluster-redis/7004/redis.conf

此时查看服务会发现他们时单独的master节点,并没有slave节点

 

5.将节点动态加入到集群中

直接敲命令可以看到帮助信息 

./redis-trib.rb

 

(1)Create -创建集群环境

./redis-trib.rb create 1 ip1:port1 ip2:port2 ……

(2)Call -可以执行redis命令

(3)add-node -可以添加一个节点到集群,第一个节点为新节点的ip:port , 第二个参数为集群中任意一个极点的ip;port

(4)del-node -删除一个节点

(5)reshard -重新分配

(6)check -检查集群状态

 

6.增加一个主节点6004

/usr/local/redis-3.2.8/src/redis-trib.rb add-node 192.168.99.171:6004 192.168.99.171:6001

 

执行结果如下:

sh-3.2# /usr/local/redis-3.2.8/src/redis-trib.rb add-node 192.168.99.171:6004 192.168.99.171:6001

>>> Adding node 192.168.99.171:6004 to cluster 192.168.99.171:6001

>>> Performing Cluster Check (using node 192.168.99.171:6001)

M: 41040ebe0a355446e8aacb1ece9363ae1cd52a2a 192.168.99.171:6001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: 7df9adb8cb22702a37adf78cf638b2237dbb295d 192.168.99.171:6002

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

S: ec6b05010a22dc4002ddee7c7a11d8f408c8a471 192.168.99.171:7003

   slots: (0 slots) slave

   replicates 4ac4444bbdbe31270be1c162233ef663899047a6

M: 4ac4444bbdbe31270be1c162233ef663899047a6 192.168.99.171:6003

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 21d0cde48313ca062ac059b94230665c1e27924a 192.168.99.171:7002

   slots: (0 slots) slave

   replicates 7df9adb8cb22702a37adf78cf638b2237dbb295d

S: bc472066eebfec7751ff0feaad4afc81e84c4d9c 192.168.99.171:7001

   slots: (0 slots) slave

   replicates 41040ebe0a355446e8aacb1ece9363ae1cd52a2a

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

>>> Send CLUSTER MEET to node 192.168.99.171:6004 to make it join the cluster.

[OK] New node added correctly.

 

 

7.查看节点

/usr/local/bin/redis-cli -h 192.168.99.171 -p 6004

192.168.99.171:6004> cluster nodes

ec6b05010a22dc4002ddee7c7a11d8f408c8a471 192.168.99.171:7003 slave 4ac4444bbdbe31270be1c162233ef663899047a6 0 1488727437088 3 connected

41040ebe0a355446e8aacb1ece9363ae1cd52a2a 192.168.99.171:6001 master - 0 1488727434564 8 connected 0-5460

21d0cde48313ca062ac059b94230665c1e27924a 192.168.99.171:7002 slave 7df9adb8cb22702a37adf78cf638b2237dbb295d 0 1488727436582 2 connected

bc472066eebfec7751ff0feaad4afc81e84c4d9c 192.168.99.171:7001 slave 41040ebe0a355446e8aacb1ece9363ae1cd52a2a 0 1488727431541 8 connected

4ac4444bbdbe31270be1c162233ef663899047a6 192.168.99.171:6003 master - 0 1488727437594 3 connected 10923-16383

522813ab806b2a6d0739275306dce1fa685761a0 192.168.99.171:6004 myself,master - 0 0 0 connected

7df9adb8cb22702a37adf78cf638b2237dbb295d 192.168.99.171:6002 master - 0 1488727435572 2 connected 5461-10922

 

(注意) 新增加的节点没有任何数据,因为它没有分配任何slot,需要手工为新增加的节点分配槽

 

8.分配slot

(1) 使用redis-trib.rb reshard 命令,找到需要拆分slot的主节点。对其进行重新分配

sh-3.2# redis-trib.rb reshard 192.168.99.171:6001

>>> Performing Cluster Check (using node 192.168.99.171:6001)

M: 41040ebe0a355446e8aacb1ece9363ae1cd52a2a 192.168.99.171:6001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

M: 7df9adb8cb22702a37adf78cf638b2237dbb295d 192.168.99.171:6002

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

S: ec6b05010a22dc4002ddee7c7a11d8f408c8a471 192.168.99.171:7003

   slots: (0 slots) slave

   replicates 4ac4444bbdbe31270be1c162233ef663899047a6

M: 522813ab806b2a6d0739275306dce1fa685761a0 192.168.99.171:6004

   slots: (0 slots) master

   0 additional replica(s)

M: 4ac4444bbdbe31270be1c162233ef663899047a6 192.168.99.171:6003

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 21d0cde48313ca062ac059b94230665c1e27924a 192.168.99.171:7002

   slots: (0 slots) slave

   replicates 7df9adb8cb22702a37adf78cf638b2237dbb295d

S: bc472066eebfec7751ff0feaad4afc81e84c4d9c 192.168.99.171:7001

   slots: (0 slots) slave

   replicates 41040ebe0a355446e8aacb1ece9363ae1cd52a2a

[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)?  200

 

你想要移动多少个槽

 

What is the receiving node ID?  522813ab806b2a6d0739275306dce1fa685761a0

你的接受这些槽的节点id时多少?

 

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.

All 表示从所有现有节点上随机移动200个槽

Done 表示从指定节点上移动200个槽

 

    Moving slot 10986 from 4ac4444bbdbe31270be1c162233ef663899047a6

    Moving slot 10987 from 4ac4444bbdbe31270be1c162233ef663899047a6

.

.执行计划打印

.

    Moving slot 10988 from 4ac4444bbdbe31270be1c162233ef663899047a6

Do you want to proceed with the proposed reshard plan (yes/no)?  yes

你想要执行这个目标计划吗?

执行成功后就可以用cluster nodes命令查看到6004节点上有槽

 

 

添加另外一个节点(从节点),不需要分配槽

(1)创建节点

sh-3.2# /usr/local/redis-3.2.8/src/redis-trib.rb add-node 192.168.99.171:7004 192.168.99.171:6001

(2)登录7004节点

sh-3.2# /usr/local/bin/redis-cli -h 192.168.99.171 -p 7004

(3)7004 与刚建好的 6004 节点主从关联,看到ok则说明执行成功

192.168.99.171:7004> cluster replicate 522813ab806b2a6d0739275306dce1fa685761a0

OK

(4)查看节点状态 cluster nodes

能看到7004后面关联着的主节点是6004的id

猜你喜欢

转载自e-e.iteye.com/blog/2360577