Redis cluster basic commands and expansion commands


Preface

One: Redis basic commands

1. Create a cluster:

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

2. Remote login redis database:

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

3. View cluster information:

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

4. View the distribution information of cluster slave, slot, and 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.

Two: redis expansion command

1. Add the redis node to the cluster: enter a node in the cluster to add

[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. Delete the node:

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

Delete the node still need to delete the data file of the node

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

3. Migrate the redis slot

[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. Check the balance of each node slot after migration

[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. Add slave nodes to the expanded master node

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. Balance the number of node slots

[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.

Guess you like

Origin blog.csdn.net/BIGmustang/article/details/108498721