redis集群添加新节点
需求:新项目上线,需要一套redis集群,而之前的一套redis集群面临着资源的问题,所以借此机会添加3组节点
原集群节点:
172.19.11.120
172.19.11.121
172.19.11.122
172.19.11.123
172.19.11.124
172.19.11.125
### 添加一组集群节点
节点如下:
172.19.11.74(主)
172.19.11.75(从)
===============
172.19.11.76(主)
172.19.11.77(从)
===============
172.19.11.78(主)
172.19.11.79(从)
方法:先添加主节点,然后添加从节点;
### 添加主节点
先找一台现有节点:
```
127.0.0.1:6379> CLUSTER NODES
b72c22c693c657d11861c0d2a7aaf0e544e23a2c 172.19.11.125:6379@16379 slave 94dd58429804a973daa476e9bdda1d1c919bf433 0 1583837607104 15 connected
46210618ba79df5f65268371f1dda046dd4c85c2 172.19.11.122:6379@16379 master - 0 1583837605093 13 connected 0-5460
94dd58429804a973daa476e9bdda1d1c919bf433 172.19.11.124:6379@16379 master - 0 1583837603000 15 connected 10923-16383
1e675b323b6d3f255ac397ec57510a79e6f19035 172.19.11.121:6379@16379 slave d93c9f5556487b839ba7433514d1a7dbc1c88db4 0 1583837606000 12 connected
d93c9f5556487b839ba7433514d1a7dbc1c88db4 172.19.11.120:6379@16379 myself,master - 0 1583837604000 12 connected 5461-10922
0727e8d32ceed2efeb737d162573cf31c9ab5b60 172.19.11.123:6379@16379 slave 46210618ba79df5f65268371f1dda046dd4c85c2 0 1583837606099 13 connected
选择种子节点:
d93c9f5556487b839ba7433514d1a7dbc1c88db4 172.19.11.120
```
### 添加第一组:
172.19.11.74(主)
172.19.11.75(从)
redis-cli --cluster add-node 新节点IP:端口 已存在节点IP:端口
实例:
redis-cli --cluster add-node 172.19.11.74:6379 172.19.11.120:6379
```
root@redis-cluster-redis-cluster-test-1:/data#
redis-cli --cluster add-node 172.19.11.74:6379 172.19.11.120:6379
root@jpvsredis-11-120:/data# redis-cli -p 6379 -a ***** --cluster add-node 172.19.11.74:6379 172.19.11.120:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.19.11.74:6379 to cluster 172.19.11.120:6379
>>> Performing Cluster Check (using node 172.19.11.120:6379)
M: d93c9f5556487b839ba7433514d1a7dbc1c88db4 172.19.11.120:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: b72c22c693c657d11861c0d2a7aaf0e544e23a2c 172.19.11.125:6379
slots: (0 slots) slave
replicates 94dd58429804a973daa476e9bdda1d1c919bf433
M: 46210618ba79df5f65268371f1dda046dd4c85c2 172.19.11.122:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 94dd58429804a973daa476e9bdda1d1c919bf433 172.19.11.124:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 1e675b323b6d3f255ac397ec57510a79e6f19035 172.19.11.121:6379
slots: (0 slots) slave
replicates d93c9f5556487b839ba7433514d1a7dbc1c88db4
S: 0727e8d32ceed2efeb737d162573cf31c9ab5b60 172.19.11.123:6379
slots: (0 slots) slave
replicates 46210618ba79df5f65268371f1dda046dd4c85c2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.19.11.74:6379 to make it join the cluster.
[OK] New node added correctly.
```
### 验证
```
root@jpvsredis-11-120:/data# redis-cli -p 6379 -a engageprod cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
b72c22c693c657d11861c0d2a7aaf0e544e23a2c 172.19.11.125:6379@16379 slave 94dd58429804a973daa476e9bdda1d1c919bf433 0 1583838205487 15 connected
46210618ba79df5f65268371f1dda046dd4c85c2 172.19.11.122:6379@16379 master - 0 1583838206493 13 connected 0-5460
94dd58429804a973daa476e9bdda1d1c919bf433 172.19.11.124:6379@16379 master - 0 1583838204470 15 connected 10923-16383
1e675b323b6d3f255ac397ec57510a79e6f19035 172.19.11.121:6379@16379 slave d93c9f5556487b839ba7433514d1a7dbc1c88db4 0 1583838205000 12 connected
d93c9f5556487b839ba7433514d1a7dbc1c88db4 172.19.11.120:6379@16379 myself,master - 0 1583838205000 12 connected 5461-10922
0727e8d32ceed2efeb737d162573cf31c9ab5b60 172.19.11.123:6379@16379 slave 46210618ba79df5f65268371f1dda046dd4c85c2 0 1583838207497 13 connected
### 新节点
c8e6a5fa54b4ab3ddd30184c8983ebf1b83e7927 172.19.11.74:6379@16379 master - 0 1583838206000 0 connected
```
### 添加从节点
redis-cli --cluster add-node 新节点IP地址:端口 存在节点IP:端口 --cluster-slave (从节点) --cluster-master-id (master节点的ID)
实例:
redis-cli -p 6379 -a ***** --cluster add-node 172.19.11.75:6379 172.19.11.74:6379 --cluster-slave --cluster-master-id c8e6a5fa54b4ab3ddd30184c8983ebf1b83e7927
```
redis-cli -p 6379 -a ***** --cluster add-node 172.19.11.75:6379 172.19.11.74:6379 --cluster-slave --cluster-master-id c8e6a5fa54b4ab3ddd30184c8983ebf1b83e7927
>>> Adding node 172.19.11.75:6379 to cluster 172.19.11.74:6379
>>> Performing Cluster Check (using node 172.19.11.74:6379)
M: c8e6a5fa54b4ab3ddd30184c8983ebf1b83e7927 172.19.11.74:6379
slots: (0 slots) master
S: 1e675b323b6d3f255ac397ec57510a79e6f19035 172.19.11.121:6379
slots: (0 slots) slave
replicates d93c9f5556487b839ba7433514d1a7dbc1c88db4
M: 46210618ba79df5f65268371f1dda046dd4c85c2 172.19.11.122:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: d93c9f5556487b839ba7433514d1a7dbc1c88db4 172.19.11.120:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 94dd58429804a973daa476e9bdda1d1c919bf433 172.19.11.124:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: b72c22c693c657d11861c0d2a7aaf0e544e23a2c 172.19.11.125:6379
slots: (0 slots) slave
replicates 94dd58429804a973daa476e9bdda1d1c919bf433
S: 0727e8d32ceed2efeb737d162573cf31c9ab5b60 172.19.11.123:6379
slots: (0 slots) slave
replicates 46210618ba79df5f65268371f1dda046dd4c85c2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.19.11.75:6379 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 172.19.11.74:6379.
[OK] New node added correctly.
```
### 验证
```
root@jpvsredis-11-120:/data# redis-cli -p 6379 -a ****** cluster nodesWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
b72c22c693c657d11861c0d2a7aaf0e544e23a2c 172.19.11.125:6379@16379 slave 94dd58429804a973daa476e9bdda1d1c919bf433 0 1583838464779 15 connected
46210618ba79df5f65268371f1dda046dd4c85c2 172.19.11.122:6379@16379 master - 0 1583838465783 13 connected 0-5460
94dd58429804a973daa476e9bdda1d1c919bf433 172.19.11.124:6379@16379 master - 0 1583838461000 15 connected 10923-16383
1e675b323b6d3f255ac397ec57510a79e6f19035 172.19.11.121:6379@16379 slave d93c9f5556487b839ba7433514d1a7dbc1c88db4 0 1583838463000 12 connected
d93c9f5556487b839ba7433514d1a7dbc1c88db4 172.19.11.120:6379@16379 myself,master - 0 1583838461000 12 connected 5461-10922
0727e8d32ceed2efeb737d162573cf31c9ab5b60 172.19.11.123:6379@16379 slave 46210618ba79df5f65268371f1dda046dd4c85c2 0 1583838466790 13 connected
## 新加节点添加成功,但还没有分配槽位
c8e6a5fa54b4ab3ddd30184c8983ebf1b83e7927 172.19.11.74:6379@16379 master - 0 1583838465000 0 connected
2561739bd9a999fb725255d6c94331e8b758df11 172.19.11.75:6379@16379 slave c8e6a5fa54b4ab3ddd30184c8983ebf1b83e7927 0 1583838464000 0 connected
```
#### 其它节点方法同上
172.19.11.76(主)
172.19.11.77(从)
redis-cli -p 6379 -a ****** --cluster add-node 172.19.11.76:6379 172.19.11.120:6379
redis-cli -p 6379 -a ****** cluster nodes |grep 172.19.11.76
redis-cli -p 6379 -a ****** --cluster add-node 172.19.11.77:6379 172.19.11.76:6379 --cluster-slave --cluster-master-id a81d8a30e07c54e471986e3571c845e2bbb50dfe
redis-cli -p 6379 -a ****** cluster nodes
172.19.11.78(主)
172.19.11.79(从)
redis-cli -p 6379 -a ****** --cluster add-node 172.19.11.78:6379 172.19.11.120:6379
redis-cli -p 6379 -a ****** cluster nodes |grep 172.19.11.78
redis-cli -p 6379 -a ****** --cluster add-node 172.19.11.79:6379 172.19.11.78:6379 --cluster-slave --cluster-master-id 1c4580768f77e2e79f316beea22afea7201ccd12
redis-cli -p 6379 -a ****** cluster nodes
### rebalance平衡集群节点slot数量
### #语法:
```
rebalance host:port
--weight <arg>
--auto-weights
--threshold <arg>
--use-empty-masters
--timeout <arg>
--simulate
--pipeline <arg>
host:port:这个是必传参数,用来从一个节点获取整个集群信息,相当于获取集群信息的入口。
--weight <arg>:节点的权重,格式为node_id=weight,如果需要为多个节点分配权重的话,需要添加多个--weight <arg>参数,即--weight b31e3a2e=5 --weight 60b8e3a1=5,node_id可为节点名称的前缀,只要保证前缀位数能唯一区分该节点即可。没有传递–weight的节点的权重默认为1。
--auto-weights:这个参数在rebalance流程中并未用到。
--threshold <arg>:只有节点需要迁移的slot阈值超过threshold,才会执行rebalance操作。具体计算方法可以参考下面的rebalance命令流程的第四步。
--use-empty-masters:rebalance是否考虑没有节点的master,默认没有分配slot节点的master是不参与rebalance的,设置--use-empty-masters可以让没有分配slot的节点参与rebalance。
--timeout <arg>:设置migrate命令的超时时间。
--simulate:设置该参数,可以模拟rebalance操作,提示用户会迁移哪些slots,而不会真正执行迁移操作。
--pipeline <arg>:与reshar的pipeline参数一样,定义cluster getkeysinslot命令一次取出的key数量,不传的话使用默认值为10。
```
### 实例:
redis-cli -p 6379 -a ****** --cluster rebalance 172.19.11.122:6379 --cluster-threshold 1 --cluster-use-empty-masters
如果需要模拟执行添加 : --cluster-simulate
```
root@redis-cluster-redis-cluster-test-1:/data# redis-cli --cluster rebalance 10.42.35.201:6379 --cluster-threshold 1 --cluster-use-empty-masters
>>> Performing Cluster Check (using node 10.42.35.201:6379)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 4 nodes. Total weight = 4.00
Moving 1366 slots from 10.42.142.202:6379 to 10.42.42.180:6379
###############################################################################################
Moving 1365 slots from 172.19.11.120:6379 to 10.42.42.180:6379
#############################################################################################
Moving 1365 slots from 10.42.11.183:6379 to 10.42.42.180:6379
#############################################################################################
```
### 验证rebalance 结果:
redis-cli cluster nodes
```
root@redis-cluster-redis-cluster-test-1:/data# redis-cli cluster nodes
f41d8746a1fd31135b0af1737f69a77ec8e6cd26 10.42.225.72:6379@16379 slave b18bef6a47c06a624d1e2ba0f5d83d4c4ce97ff7 0 1546942598000 6 connected
e538c397a1c8ca6217561a88d72b08b0e513da84 10.42.181.22:6379@16379 slave 6764b152d0d7526b48a16621ff2a4c72f6494622 0 1546942598000 5 connected
39aceb27ccdce533c5cfbf51eb13928d2515fadc 10.42.35.201:6379@16379 slave 748273d7a0a186d229028edefdcff2711c1c0e0d 0 1546942599807 4 connected
6764b152d0d7526b48a16621ff2a4c72f6494622 10.42.142.202:6379@16379 master - 0 1546942598803 2 connected 6827-10922
748273d7a0a186d229028edefdcff2711c1c0e0d 172.19.11.120:6379@16379 myself,master - 0 1546942597000 1 connected 1365-5460
b18bef6a47c06a624d1e2ba0f5d83d4c4ce97ff7 10.42.11.183:6379@16379 master - 0 1546942598000 3 connected 12288-16383
## 新加节点已经有分配槽位
f9bc8def897915d73569b140d1478fbf94d3dbef 10.42.22.184:6379@16379 slave 0488e5f3e006f0df2806bf0b178a2414b248b1e5 0 1546942600809 7 connected
0488e5f3e006f0df2806bf0b178a2414b248b1e5 10.42.42.180:6379@16379 master - 0 1546942597000 7 connected 0-1364 5461-6826 10923-12287
```
#### 注意事项:
1:rancher 环境不能使用容器域名组装集群:
2:集群收缩未测试;
### 常用命令参考:
```
集群
cluster info :打印集群的信息
cluster nodes :列出集群当前已知的所有节点( node),以及这些节点的相关信息。
节点
cluster meet <ip> <port> :将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。
cluster forget <node_id> :从集群中移除 node_id 指定的节点。
cluster replicate <node_id> :将当前节点设置为 node_id 指定的节点的从节点。
cluster saveconfig :将节点的配置文件保存到硬盘里面。
槽(slot)
cluster addslots <slot> [slot ...] :将一个或多个槽( slot)指派( assign)给当前节点。
cluster delslots <slot> [slot ...] :移除一个或多个槽对当前节点的指派。
cluster flushslots :移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。
cluster setslot <slot> node <node_id> :将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。
cluster setslot <slot> migrating <node_id> :将本节点的槽 slot 迁移到 node_id 指定的节点中。
cluster setslot <slot> importing <node_id> :从 node_id 指定的节点中导入槽 slot 到本节点。
cluster setslot <slot> stable :取消对槽 slot 的导入( import)或者迁移( migrate)。
键
cluster keyslot <key> :计算键 key 应该被放置在哪个槽上。
cluster countkeysinslot <slot> :返回槽 slot 目前包含的键值对数量。
cluster getkeysinslot <slot> <count> :返回 count 个 slot 槽中的键
```