redis安装详见安装文档,目前部署3.2.3,不同版本并不一定能够兼容!安装文档如下:
# redis-cli --version
redis-cli 3.2.3
1.在新的机器上启动新的两个节点,并记录其ID
#启动新的节点
redis-server /mnt/redis-cluster-7029/redis.conf
redis-server /mnt/redis-cluster-7030/redis.conf
#执行以下命令查看7029节点信息
redis-cli -c -p 7029 cluster nodes
7029输出以下内容
4ce0a2cc06dbc435c90c45ff891a262ef58a1cfd :7029 myself,master - 0 0 0 connected
#执行以下命令查看7030节点信息
redis-cli -c -p 7030 cluster nodes
7030输出以下内容:
923e869b3fda8add429fb200ea00ce94bdbc84e6 :7030 myself,master - 0 0 0 connected
2.将新机器上的两个新实例加入到需要扩容的集群中
“10.25.157.78:7022″为需要扩容的集群中任意主节点
“10.80.82.74:7029&7030″为新添加的服务器中的新实例
redis-trib.rb add-node 10.80.82.74:7029 10.25.157.78:7022
redis-trib.rb add-node 10.80.82.74:7030 10.25.157.78:7022
查看一下是否加入成功
redis-cli -c -p 7029 cluster nodes
3.将10.80.82.74:7029设为master,10.80.82.74:7030设为slave
在10.80.82.74上执行:(其中4ce0a2cc06dbc435c90c45ff891a262ef58a1cfd为7029实例的ID)
redis-cli -c -p 7030
cluster replicate 4ce0a2cc06dbc435c90c45ff891a262ef58a1cfd
查看一下,发现设置成功:
[root@tuling10-80-82-74 ~]# redis-cli -c -p 7029 cluster nodes
1f7f9825b2ed6253d50fdcbc8db6a10c43fd2ad4 10.45.144.56:7027 master – 0 1538969449311 9 connected 0-4095
6eadec56705c1e8a174e122145afde836cc412f5 10.25.157.78:7024 slave 1ae708c9458a8e97d199f461b21b1ac5c422dc18 0 1538969449310 6 connected
1ae708c9458a8e97d199f461b21b1ac5c422dc18 10.45.144.56:7026 master – 0 1538969449410 6 connected 12288-16383
35123eb1a9e564794eebd84de0b68993d76bce2a 10.45.144.56:7025 master – 0 1538969447809 5 connected 4096-8191
a9dddb597278bc0956f88c3b25ebad4edac975c9 10.25.157.78:7021 slave 1f7f9825b2ed6253d50fdcbc8db6a10c43fd2ad4 0 1538969448808 9 connected
102489d5d6e03731174876bd29b86c44cfc8190d 10.25.157.78:7023 slave 35123eb1a9e564794eebd84de0b68993d76bce2a 0 1538969449310 5 connected
408525165ed0cb15f3fea7a865879d0968d00b04 10.25.157.78:7022 master – 0 1538969448307 2 connected 8192-12287
d3d9c35ee4602b40276d5fc45865c1d736eaa757 10.45.144.56:7028 slave 408525165ed0cb15f3fea7a865879d0968d00b04 0 1538969448809 2 connected
4ce0a2cc06dbc435c90c45ff891a262ef58a1cfd 10.80.82.74:7029 myself,master – 0 0 10 connected
923e869b3fda8add429fb200ea00ce94bdbc84e6 10.80.82.74:7030 slave 4ce0a2cc06dbc435c90c45ff891a262ef58a1cfd 0 1538969447307 10 connected
4.迁移槽位
我们原集群中有4个master,总共16384个槽位,新增一台master后即需要从原4个实例上每个迁移819个槽位。
在集群中任意一台机器上执行:(这里我们在10.25.157.78上执行)
redis-trib.rb reshard --timeout 7200 10.25.157.78:7022
How many slots do you want to move (from 1 to 16384)? 819
What is the receiving node ID? 4ce0a2cc06dbc435c90c45ff891a262ef58a1cfd #新实例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.
Source node #1:408525165ed0cb15f3fea7a865879d0968d00b04 #原实例ID
Source node #2:done
5.验证一下:
redis-trib.rb info 10.25.157.78:7022
6.缩容:删除节点:删除的顺序是先删除Slave节点,再删除Master主节点
例:删除从节点10.80.82.74:7030 (删除7030从节点,输入del-node命令,指定删除节点的IP地址和Port端口号,同时还要提供该从节点ID名称。)
redis-trib.rb del-node 10.80.82.74:7030 923e869b3fda8add429fb200ea00ce94bdbc84e6