redis cluster command set

Reprinted from: https://www.cnblogs.com/kevingrace/p/7910692.html

Table of contents

1. Redis Cluster cluster

cluster

node

slot

key

2. Offline and recovery of the Master in the cluster

3. Add nodes

3. Manually change slave

4. Delete nodes

5. Copy migration

6. Upgrade nodes

7. Redis cache cleaning


1. Redis Cluster cluster

The following commands are unique to the Redis Cluster cluster. To execute the following commands, you need to log in to redis first:
[root@manage redis]# redis-cli -c -p 6382 -h 192.168.10.12 (Client command: redis-cli -c - p port -h ip)
192.168.10.12:6382> After logging in to redis, you can perform the following command operations in it

cluster


cluster info: Print cluster information
cluster nodes: List all nodes currently known to the cluster, as well as related information about these nodes.


node


cluster meet <ip> <port>: Add the node specified by ip and port to the cluster and make it a member of the cluster.
cluster forget <node_id>: Remove the node specified by node_id from the cluster.
cluster replicate <master_node_id>: Set the current slave node to the slave node of the master node specified by node_id. Can only be operated on slave nodes.
cluster saveconfig: Save the node configuration file to the hard disk.


slot


cluster addslots <slot> [slot ...]: Assign one or more slots to the current node.
cluster delslots <slot> [slot ...]: Removes one or more slots assigned to the current node.
cluster flushslots: Remove all slots assigned to the current node, turning the current node into a node with no slots assigned to it.
cluster setslot <slot> node <node_id>: Assign the slot to the node specified by node_id. If the slot has been assigned to
another node, let the other node delete the slot first and then assign it.
cluster setslot <slot> migrating <node_id>: Migrate the slot of this node to the node specified by node_id.
cluster setslot <slot> importing <node_id>: Import slots from the node specified by node_id to this node.
cluster setslot <slot> stable: Cancel the import or migration of the slot.


key


cluster keyslot <key>: Calculate which slot the key key should be placed on.
cluster countkeysinslot <slot>: Returns the number of key-value pairs currently contained in the slot.
cluster getkeysinslot <slot> <count>: Returns the keys in count slots.

2. Offline and recovery of the Master in the cluster


1) After the Master goes offline, its corresponding Slaver node will automatically become the Master node, as shown in the screenshot below:

2) The original Master becomes a Slaver node after restarting, and is the Slaver node of the original Master node.

 

3. Add nodes

 

1)新配置二个测试节点
# cd /etc/redis  

//新增配置  
# cp redis-6379.conf redis-6378.conf && sed -i "s/6379/6378/g" redis-6378.conf  
# cp redis-6382.conf redis-6385.conf && sed -i "s/6382/6385/g" redis-6385.conf  
   
//启动  
# redis-server /etc/redis/redis-6385.conf > /var/log/redis/redis-6385.log 2>&1 &  
# redis-server /etc/redis/redis-6378.conf > /var/log/redis/redis-6378.log 2>&1 &  

2)添加主节点
# redis-trib.rb add-node 192.168.10.219:6378 192.168.10.219:6379  
注释:
192.168.10.219:6378是新增的节点
192.168.10.219:6379集群任一个旧节点

3)添加从节点
# redis-trib.rb add-node --slave --master-id 03ccad2ba5dd1e062464bc7590400441fafb63f2 192.168.10.220:6385 192.168.10.219:6379  
注释:
--slave,表示添加的是从节点
--master-id 03ccad2ba5dd1e062464bc7590400441fafb63f2,主节点的node id,在这里是前面新添加的6378的node id
192.168.10.220:6385,新节点
192.168.10.219:6379集群任一个旧节点

4)重新分配slot
# redis-trib.rb reshard 192.168.10.219:6378               //下面是主要过程  
How many slots do you want to move (from 1 to 16384)? 1000 //设置slot数1000  
What is the receiving node ID? 03ccad2ba5dd1e062464bc7590400441fafb63f2 //新节点node 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:all                                      //表示全部节点重新洗牌  
Do you want to proceed with the proposed reshard plan (yes/no)? yes //确认重新分  

新增加的主节点,是没有slots的,
M: 03ccad2ba5dd1e062464bc7590400441fafb63f2 192.168.10.219:6378
slots:0-332,5461-5794,10923-11255 (0 slots) master
主节点如果没有slots的话,存取数据就都不会被选中。
可以把分配的过程理解成打扑克牌,all表示大家重新洗牌;输入某个主节点的node id,然后在输入done的话,就好比从某个节点,抽牌。

5)查看一下,集群情况
[root@slave2 redis]# redis-trib.rb check 192.168.10.219:6379  
Connecting to node 192.168.10.219:6379: OK  
Connecting to node 192.168.10.220:6385: OK  
Connecting to node 192.168.10.219:6378: OK  
Connecting to node 192.168.10.220:6382: OK  
Connecting to node 192.168.10.220:6383: OK  
Connecting to node 192.168.10.219:6380: OK  
Connecting to node 192.168.10.219:6381: OK  
Connecting to node 192.168.10.220:6384: OK  
>>> Performing Cluster Check (using node 192.168.10.219:6379)  
M: 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052 192.168.10.219:6379  
 slots:5795-10922 (5128 slots) master  
 1 additional replica(s)  
S: 9c240333476469e8e2c8e80b089c48f389827265 192.168.10.220:6385  
 slots: (0 slots) slave  
 replicates 03ccad2ba5dd1e062464bc7590400441fafb63f2  
M: 03ccad2ba5dd1e062464bc7590400441fafb63f2 192.168.10.219:6378  
 slots:0-332,5461-5794,10923-11255 (1000 slots) master  
 1 additional replica(s)  
M: 19b042c17d2918fade18a4ad2efc75aa81fd2422 192.168.10.220:6382  
 slots:333-5460 (5128 slots) master  
 1 additional replica(s)  
M: b2c50113db7bd685e316a16b423c9b8abc3ba0b7 192.168.10.220:6383  
 slots:11256-16383 (5128 slots) master  
 1 additional replica(s)  
S: 6475e4c8b5e0c0ea27547ff7695d05e9af0c5ccb 192.168.10.219:6380  
 slots: (0 slots) slave  
 replicates 19b042c17d2918fade18a4ad2efc75aa81fd2422  
S: 1ee01fe95bcfb688a50825d54248eea1e6133cdc 192.168.10.219:6381  
 slots: (0 slots) slave  
 replicates b2c50113db7bd685e316a16b423c9b8abc3ba0b7  
S: 9a2a1d75b8eb47e05eee1198f81a9edd88db5aa1 192.168.10.220:6384  
 slots: (0 slots) slave  
 replicates 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052  
[OK] All nodes agree about slots configuration.  
>>> Check for open slots...  
>>> Check slots coverage...  
[OK] All 16384 slots covered.

 

3. Manually change slave

Manually change the master node to which the slave node belongs (a slave can only belong to one master, and a master can have multiple slaves)

//查看一下6378的从节点  
# redis-cli -p 6378 cluster nodes | grep slave | grep 03ccad2ba5dd1e062464bc7590400441fafb63f2  
   
//将6385加入到新的master  
# redis-cli -c -p 6385 -h 192.168.10.220  
192.168.10.220:6385> cluster replicate 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052  //新master的node id 
OK  
192.168.10.220:6385> quit  
   
//查看新master的slave  
# redis-cli -p 6379 cluster nodes | grep slave | grep 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052

 

4. Delete nodes

 

1)删除从节点
# redis-trib.rb del-node 192.168.10.220:6385 '9c240333476469e8e2c8e80b089c48f389827265'  

2)删除主节点
如果主节点有从节点,将从节点转移到其他主节点
如果主节点有slot,去掉分配的slot,然后在删除主节点
# redis-trib.rb reshard 192.168.10.219:6378                             //取消分配的slot,下面是主要过程  
How many slots do you want to move (from 1 to 16384)? 1000              //被删除master的所有slot数量  
What is the receiving node ID? 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052 //接收6378节点slot的master  
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:03ccad2ba5dd1e062464bc7590400441fafb63f2                //被删除master的node-id  
Source node #2:done   
   
Do you want to proceed with the proposed reshard plan (yes/no)? yes    //取消slot后,reshard  


新增master节点后,也进行了这一步操作,当时是分配,现在去掉。反着的。
# redis-trib.rb del-node 192.168.10.219:6378 '03ccad2ba5dd1e062464bc7590400441fafb63f2'  

新的master节点被删除了,这样就回到了,就是这篇文章开头,还没有添加节点的状态

 

5. Copy migration

在redis集群中通过"cluster replicate <master_node_id> "命令可以将一个slave节点重新配置为另外一个master的slave。
注意:这个只是针对slave节点,即登录到slave节点的reids中,执行这个命令。

比如172.16.60.204:7003是172.16.60.202:7000主节点的slave节点,也可以把他设置成172.16.60.205:7004主节点的slave节点。
172.16.60.205:7004主节点的ID是48cbab906141dd26241ccdbc38bee406586a8d03

则操作为
[root@redis-new01 ~]# /data/redis-4.0.6/src/redis-cli -h 172.16.60.204 -c -p 7003 
172.16.60.204:7003> cluster replicate 48cbab906141dd26241ccdbc38bee406586a8d03
OK
172.16.60.204:7003>

这样172.16.60.204:7003节点就变成了172.16.60.205:7004主节点的slave节点,而不再是172.16.60.202:7000主节点的slave节点!

这样可以自动的将一个复制节点从一个master下移动到另外一个master下。 这种情况下的复制节点的自动重配置被称为复制迁移。
复制迁移可以提升系统的可靠性和抗灾性。

在某种情况下,你想让集群的复制节点从一个master迁移到另一个master的原因可能是:
集群的抗崩溃能力总是跟集群中master 拥有的平均slave数量成正比。
比如,如果一个集群中每个master只有一个slave,当master和slave都挂掉的时候这个集群就崩溃了。因为此时有一些哈希槽无法找到了。
虽然网络分裂会把一堆节点从集群中孤立出来(这样你一下就会知道集群出问题了),但是其他的更常见的硬件或者软件的问题并不会在多台机器上同时发生,
所以很 可能在你的这个集群(平均每个master只有一个slave)有一个slave在早上4点挂掉,然后他的master在随后的早上6点挂掉。这样依然会 导致集群崩溃。

可以通过给每个master都再多加一个slave节点来改进系统的可靠性,但是这样很昂贵。复制迁移允许只给某些master增加slave。比方说你的集群有20个节点,
10个master,每个master都有1个slave。然后你增加3个 slave到集群中并把他们分配给某几个master节点,这样某些master就会拥有多于1个slave。

当某个 master失去了slave的时候,复制迁移可以将slave节点从拥有富余slave的master旗下迁移给没有slave的master。所以当 你的slave在早上4点挂掉的时候,
另一个slave会被迁移过来取代它的位置,这样当master节点在早上5点挂掉的时候,依然有一个slave可 以被选举为master,集群依然可以正常运行。

所以简而言之,关于复制迁移应该注意下面几个方面:
-  集群在迁移的时候会尝试去迁移拥有最多slave数量的master旗下的slave。
-  想利用复制迁移特性来增加系统的可用性,你只需要增加一些slave节点给单个master(哪个master节点并不重要)。
-  复制迁移是由配置项cluster-migration-barrier控制的

6. Upgrade nodes

升级从服务器节点很简单,因为你只需要停止节点然后用已更新的Redis版本重启。如果有客户端使用从服务器节点分离读请求,它们应该能够在某个节点
不可用时重新连接另一个从服务器。
     
升级主服务器要稍微复杂一些,建议的步骤是:
1)使用cluster failover来触发一次手工故障转移主服务器(请看本文档的手工故障转移小节)。
2)等待主服务器变为从服务器。
3)像升级从服务器那样升级这个节点。
4)如果你想让你刚刚升级的节点成为主服务器,触发一次新的手工故障转移,让升级的节点重新变回主服务器。

可以按照这些步骤来一个节点一个节点的升级,直到全部节点升级完毕。

At present, the redis cluster can only be started with an empty node. When the node has data, there will be an err warning, but just fix it. redis-trib.rb fix Use this command to fix it and it will be OK.

7. Redis cache cleaning

1)登陆redis
# src/redis-cli -c -h 192.168.1.100 -p 6379

2)执行:
192.168.1.100:6379> dbsize

3)
192.168.1.100:6379> flushall

================================================================
redis集群指定key值得缓存清理:
1)登录至指定端口的redis服务器
# src/redis-cli -c -h 192.168.1.100 -p 6379

2)查看所有key值 (或者使用info命令也能查看出来)
keys *

3)删除指定索引的值
del key

 

Guess you like

Origin blog.csdn.net/harryptter/article/details/88242498