环境简介:
1:centos 7 ;
2:redis version 5.0.2 ;
3: 安装方式:编译安装 ;
4:。。。
创建集群
节点IP地址
10.42.166.105:6379
10.42.142.202:6379
10.42.11.183:6379
10.42.35.201:6379
10.42.181.22:6379
10.42.225.72:6379
使用公司redis docker 镜像启动6个实例后,开始创建集群:
create命令可选replicas参数,replicas表示需要有几个slave。最简单命令使用如下:
redis-cli --cluster create --cluster-replicas 1 \
10.42.166.105:6379 \
10.42.142.202:6379 \
10.42.11.183:6379 \
10.42.35.201:6379 \
10.42.181.22:6379 \
10.42.225.72:6379
查看集群节点
redis-cli cluster nodes
测试创建key
set testkey1 testvalue1
get testkey1
模拟故障,删除镜像
直接在rancher集群直接delete 10.42.141.119 节点
查看状态
root@redis-cluster-redis-cluster-test-1:/data# redis-cli cluster nodes
21322d2e62d027393a46add604a8eed37c53b460 10.42.1.140:6379@16379 slave 39cde9a04a9cfde37337840f31fa1bc1ab8fb37a 0 1546933324975 6 connected
dfa238fff8a7a49230cff7eb74f573f5645c8ec5 10.42.166.105:6379@16379 myself,master - 0 1546933324000 1 connected 0-5460
f1e99f5f57b2390cba33c8142cb79c2e062db854 10.42.158.17:6379@16379 master - 0 1546933325980 2 connected 5461-10922
3f20faa26d09fd263f249976fbdac859d26a511a 10.42.91.228:6379@16379 slave f1e99f5f57b2390cba33c8142cb79c2e062db854 0 1546933326981 5 connected
39cde9a04a9cfde37337840f31fa1bc1ab8fb37a 10.42.221.211:6379@16379 master - 0 1546933325000 3 connected 10923-16383
### 集群显示节点连接失败
9460c46ed7e8e9d809a896d966754823c7fb9e5d 10.42.141.119:6379@16379 slave,fail dfa238fff8a7a49230cff7eb74f573f5645c8ec5 1546933279432 1546933277829 4 connected
从集群彻底删除节点
从集群中移除 node_id 指定的节点。
redis-cli cluster forget dfa238fff8a7a49230cff7eb74f573f5645c8ec5
返回
OK
### 从集群中删除节点
del-node可以把某个节点从集群中删除。del-node只能删除没有分配slot的节点。删除命令传递两个参数:
host:port:从该节点获取集群信息。
node_id:需要删除的节点id。
redis-cli cluster del-node 10.42.141.211:6379 dfa238fff8a7a49230cff7eb74f573f5645c8ec5
添加新节点 (添加从节点)
--slave:设置该参数,则新节点以slave的角色加入集群
--master-id:这个参数需要设置了--slave才能生效,--master-id用来指定新节点的master节点。如果不设置该参数,则会随机为节点选择master节点。
redis-cli --cluster add-node 新节点IP地址:端口 存在节点IP:端口 --cluster-slave (从节点) --cluster-master-id (master节点的ID)
redis-cli --cluster add-node 10.42.141.119:6379 10.42.166.105:6379 --cluster-slave --cluster-master-id dfa238fff8a7a49230cff7eb74f573f5645c8ec5
返回结果如下:
>>> Adding node 10.42.141.119:6379 to cluster 10.42.166.105:6379
>>> Performing Cluster Check (using node 10.42.166.105:6379)
M: dfa238fff8a7a49230cff7eb74f573f5645c8ec5 10.42.166.105:6379
slots:[0-5460] (5461 slots) master
..........
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 10.42.141.119:6379 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 10.42.166.105:6379.
[OK] New node added correctly.
添加一组集群节点
节点如下:
10.42.42.180 (主)
10.42.22.184 (从)
方法:先添加主节点,然后添加从节点;
添加主节点
redis-cli --cluster add-node 新节点IP:端口 已存在节点IP:端口
实例:
redis-cli --cluster add-node 10.42.42.180:6379 10.42.166.105:6379
root@redis-cluster-redis-cluster-test-1:/data# redis-cli --cluster add-node 10.42.42.180:6379 10.42.166.105:6379
>>> Adding node 10.42.42.180:6379 to cluster 10.42.166.105:6379
>>> Performing Cluster Check (using node 10.42.166.105:6379)
M: 748273d7a0a186d229028edefdcff2711c1c0e0d 10.42.166.105:6379
...........
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 10.42.42.180:6379 to make it join the cluster. 《== 添加成功
[OK] New node added correctly.
验证
root@redis-cluster-redis-cluster-test-1:/data# redis-cli cluster nodes
........
b18bef6a47c06a624d1e2ba0f5d83d4c4ce97ff7 10.42.11.183:6379@16379 master - 0 1546941402518 3 connected 10923-16383
6764b152d0d7526b48a16621ff2a4c72f6494622 10.42.142.202:6379@16379 master - 0 1546941402000 2 connected 5461-10922
### 新节点
0488e5f3e006f0df2806bf0b178a2414b248b1e5 10.42.42.180:6379@16379 master - 0 1546941402000 0 connected
添加从节点
redis-cli --cluster add-node 新节点IP地址:端口 存在节点IP:端口 --cluster-slave (从节点) --cluster-master-id (master节点的ID)
实例:
redis-cli --cluster add-node 10.42.22.184:6379 10.42.42.180:6379 --cluster-slave --cluster-master-id 0488e5f3e006f0df2806bf0b178a2414b248b1e5
root@redis-cluster-redis-cluster-test-1:/data# redis-cli --cluster add-node 10.42.22.184:6379 10.42.42.180:6379 --cluster-slave --cluster-master-id 0488e5f3e006f0df2806bf0b178a2414b248b1e5
>>> Adding node 10.42.22.184:6379 to cluster 10.42.42.180:6379
>>> Performing Cluster Check (using node 10.42.42.180:6379)
M: 0488e5f3e006f0df2806bf0b178a2414b248b1e5 10.42.42.180:6379
slots: (0 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.
>>> Send CLUSTER MEET to node 10.42.22.184:6379 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 10.42.42.180:6379.
[OK] New node added correctly.
验证
root@redis-cluster-redis-cluster-test-1:/data# redis-cli cluster nodes
f41d8746a1fd31135b0af1737f69a77ec8e6cd26 10.42.225.72:6379@16379 slave b18bef6a47c06a624d1e2ba0f5d83d4c4ce97ff7 0 1546941516826 6 connected
.......
6764b152d0d7526b48a16621ff2a4c72f6494622 10.42.142.202:6379@16379 master - 0 1546941514822 2 connected 5461-10922
## 新加节点添加成功,但还没有分配槽位
0488e5f3e006f0df2806bf0b178a2414b248b1e5 10.42.42.180:6379@16379 master - 0 1546941515825 7 connected
f9bc8def897915d73569b140d1478fbf94d3dbef 10.42.22.184:6379@16379 slave 0488e5f3e006f0df2806bf0b178a2414b248b1e5 0 1546941515000 7 connected
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 --cluster rebalance 10.42.35.201:6379 --cluster-threshold 1 --cluster-use-empty-masters --pipeline 10
如果需要模拟执行添加 : --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 10.42.166.105: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 10.42.166.105: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
常用命令参考:
集群
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 槽中的键
参考文档:
http://weizijun.cn/2016/01/08/redis%20cluster%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B7redis-trib-rb%E8%AF%A6%E8%A7%A3/