redis集群基础命令与扩容命令


前言

一:redis基础命令

1、创建集群:

redis-cli --cluster create --cluster-replicas 1 节点IP地址1:端口... 节点IP地址n:端口

2、远程登录redis数据库:

redis-cli -h 192.168.233.100 -p 6379 -c
-c:连接集群结点时使用,此选项可防止moved和ask异常

3、查看集群信息:

可登陆redis数据库中,输入cluster info查询集群状态,
也可使用命令 redis-cli --cluster info 节点ip:6379

4、查看集群slave、slot、key分布信息情况

[root@master1 ~]# redis-cli --cluster info 192.168.100.41:6379
192.168.100.41:6379 (7f112f82...) -> 4 keys | 5461 slots | 1 slaves.
192.168.100.42:6379 (df195a34...) -> 1 keys | 5462 slots | 1 slaves.
192.168.100.43:6379 (a233a235...) -> 2 keys | 5461 slots | 1 slaves.
[OK] 7 keys in 3 masters.
0.00 keys per slot on average.

二:redis扩容命令

1、将redis节点添加到集群:进入集群中一个节点添加

[root@master1 ~]# redis-cli --cluster add-node 192.168.100.47:6379 192.168.100.41:6379	'//redis-cli --cluster add-node 新节点IP:端口号 当前集群中已存在的任意节点IP:端口号'
    

2、删除节点:

redis-cli --cluster del-node 集群任意目标节点IP:端口号 要删除的节点ID

删除节点仍需删除节点的数据文件

[root@master1 ~]# cd /var/lib/redis/6379/
[root@master1 6379]# ls
appendonly.aof  dump.rdb  nodes-6379.conf
[root@master1 6379]# rm -rf *

3、迁移redis槽位

[root@master1 ~]# redis-cli --cluster reshard 192.168.100.41:6379
How many slots do you want to move (from 1 to 16384)? 4096  '//要迁移多少个槽'

What is the receiving node ID? 99521b7fd126b694bcb9a22ffa5a490f31f66543  '//迁移到哪个节点,数据节点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.

'//要求输入源节点的id,这里因为原本有三个节点,输入done表示结束'
Source node #1: 7f112f82bcf28a5d0627ea81b48cb76f4ea8605d
Source node #2: df195a34a91d756157a0fda7c71e99d5bd8fad09
Source node #3: a233a23541f431107fed79908318394d8bb30b51
Source node #4: done

'//最后会有一个迁移方案,输入yes表示同意,迁移开始。输入no表示不同意,重新设置迁移方案。'

'//确认是否迁移成功'
[root@master1 ~]# redis-cli -h 192.168.100.41 -p 6379
192.168.100.41:6379> cluster nodes
    ...省略内容

4、迁移后检测各个节点槽的均衡性

[root@master1 ~]# redis-cli --cluster rebalance 192.168.100.41:6379
>>> Performing Cluster Check (using node 192.168.100.41:6379)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 2.00% threshold.
可以看出,节点负责的槽数据差异在2%以内,因此槽分配均衡

5、为扩容的主节点添加从节点

root@master1 ~]# redis-cli -h 192.168.100.48 -p 6379	'//登陆redis数据库'
192.168.100.48:6379> cluster replicate 99521b7fd126b694bcb9a22ffa5a490f31f66543	'//添加从节点id'
OK
192.168.100.48:6379> cluster nodes	'//查看节点信息'
    ...省略内容

6、平衡各节点槽数量

[root@master1 ~]# redis-cli --cluster rebalance --cluster-threshold 1 192.168.100.41:6379
>>> Performing Cluster Check (using node 192.168.100.41:6379)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 1.00% threshold.

猜你喜欢

转载自blog.csdn.net/BIGmustang/article/details/108498721