Redis Cluster Daily Operating Commands

Posted by abhikerl on Mon, 05 Aug 2019 13:32:50 +0200

Links to the original text: https://www.cnblogs.com/kevingrace/p/7910692.html

1. The following commands are unique to the Redis Cluster cluster. To execute the following commands, you need to login 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>  Sign in redis After that, you can do the following commands in it

colony

cluster info: Print Cluster Information
 cluster nodes: Lists all the nodes currently known to the cluster and the information about those nodes.

node

Cluster meet < ip > < port >: Add the nodes 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 of master node specified by node_id from the slave node. Only for slave nodes.
cluster saveconfig: Save the configuration file of the node to the hard disk.

Slot

Cluster addslots < slot > [slot...]: assign one or more slots to the current node.
Cluster delslots <slot> [slot...]: Remove the assignment of one or more slots to the current node.
cluster flushslots: Remove all slots assigned to the current node and make the current node a node without any slots assigned.
Cluster setslot < slot > node < node_id >: assign slot to the node specified by node_id if slot has been assigned to

For another node, let another node delete the slot > and then assign it.

Cluster setslot < slot > migrating < node_id >: Migrates the slot of this node to the node specified by node_id.
Cluster setslot < slot > importing < node_id >: Import slot from the node specified by node_id to the node.
Cluster setslot < slot > stable: Cancel the import or migrate of slot.
key
 Cluster keyslot < key >: Which slot should the calculation key be placed on?
Cluster countkeysinslot < slot >: Returns the number of key pairs currently contained in slot slots.
Cluster getkeysinslot < slot > < count >: Returns keys in countslot slots.

2. Downline and Recovery of Master in Cluster
1) When Master is offline, its corresponding Slaver node will automatically become Master node, as shown in the following screenshot:

2) The original Master is rebooted into Slaver node, and is the Slaver node of the original Master node.

Third, add nodes

1)New configuration of two test nodes

# cd /etc/redis 

//New configuration

# 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 

//Start-up

# 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)Add primary node

# redis-trib.rb add-node 192.168.10.219:6378 192.168.10.219:6379 

//Notes:

192.168.10.219:6378 New nodes

192.168.10.219:6379 Any old node in the cluster

3)Adding slave nodes

# redis-trib.rb add-node --slave --master-id 03ccad2ba5dd1e062464bc7590400441fafb63f2 192.168.10.220:6385 192.168.10.219:6379 

//Notes:

--slave,Represents the addition of slave nodes

--master-id 03ccad2ba5dd1e062464bc7590400441fafb63f2,Primary node node id,Here is the newly added 6378 node id

192.168.10.220:6385,New Node

192.168.10.219:6379 Any old node in the cluster

4)Redistribution slot

# redis-trib.rb reshard 192.168.10.219:6378

How many slots do you want to move (from 1 to 16384)? 1000 //Set the slot number 1000

What is the receiving node ID? 03ccad2ba5dd1e062464bc7590400441fafb63f2 //New 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 * All * All * All // Represents the reshuffling of all nodes.

Do you want to proceed with the proposed reshard plan (yes/no)? yes //Recognition and redistribution

//There are no slots in the new primary node.

M: 03ccad2ba5dd1e062464bc7590400441fafb63f2 192.168.10.219:6378

slots:0-332,5461-5794,10923-11255 (0 slots) master

//Access to data would not be selected if the primary node had no slots.

//The process of allocation can be understood as playing cards. all means that people shuffle cards again. Input the node id of a main node and then enter the done, it is like drawing cards from a certain node.

5)Take a look at the cluster situation

[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 the master master master master node to which slave slaves belong (a slave can only belong to one master, while a master can have multiple slaves)

//Look at the slave node of 6378

# redis-cli -p 6378 cluster nodes | grep slave | grep 03ccad2ba5dd1e062464bc7590400441fafb63f2 
  
//Add 6385 to the new master

# redis-cli -c -p 6385 -h 192.168.10.220 

192.168.10.220:6385> cluster replicate 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052  //node id of the new master

OK 

192.168.10.220:6385> quit 
  
//View the slave of the new master

# redis-cli -p 6379 cluster nodes | grep slave | grep 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052

4. Delete Nodes

1)Delete slave nodes

# redis-trib.rb del-node 192.168.10.220:6385 '9c240333476469e8e2c8e80b089c48f389827265' 

2)Delete primary node

//If the master node has a slave node, the slave node is transferred to another master node.

//If the primary node has slots, remove the allocated slots and then delete the primary node

# redis-trib.rb reshard 192.168.10.219:6378

How many slots do you want to move (from 1 to 16384)? 1000              //Number of all slot s deleted by master

What is the receiving node ID? 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052 //master receiving 6378 node slot

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:03 ccad2ba5dd1e062464 bc7590400441 fafb63f2 // deleted master's node-id

Source node #2:done  

Do you want to proceed with the proposed reshard plan (yes/no)? yes    //After canceling slot, reshard

//After adding master node, this step was also carried out. At that time, it was allocated, but now it is removed. On the contrary.

# redis-trib.rb del-node 192.168.10.219:6378 '03ccad2ba5dd1e062464bc7590400441fafb63f2' 

//The new master node has been deleted, so we're back to the beginning of this article, where we haven't added the status of the node yet.

5. Replication migration

In redis cluster, one slave node can be reconfigured to another master slave by using the "cluster replicate < master_node_id>" command.

Note: This is only for slave nodes, that is, to log in to reids of slave nodes and execute this command.

For example, 172.16.60.204:7003 is the slave node of 172.16.60.202:7000 master node, or it can be set to 172.16.60.205:7004 slave node of master node.

172.16.60.205:7004 Primary node ID is 48cbab 906141dd26241 ccdbc 38bee406586a8d03

Then the operation is

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

Thus 172.16.60.204:7003 nodes become 172.16.60.205:7004 slave nodes of the primary node, instead of 172.16.60.202:7000 slave nodes of the primary node!

This automatically moves a replicated node from one master to another. In this case, the automatic reconfiguration of replication nodes is called replication migration.

Replication migration can improve the reliability and disaster resistance of the system.

In some cases, the reason you want the replica nodes of the cluster to migrate from one master to another may be:

Cluster crash resistance is always proportional to the average number of slave s owned by master s in the cluster.

For example, if each master in a cluster has only one slave, the cluster collapses when both master and slave are suspended. Because some hash slots can't be found at this time.

Although network splitting isolates a bunch of nodes from the cluster (so you know that the cluster is out of order), other more common hardware or software problems do not occur on multiple machines at the same time.

So it's likely that in your cluster (with an average master having only one slave), one slave will hang up at 4 a.m., and then his master will hang up at 6 a.m. This will still lead to cluster collapse.

The reliability of the system can be improved by adding an additional slave node to each master, but this is expensive. Replication migration allows only slaves to be added to some masters. Let's say your cluster has 20 nodes.

Ten masters, one slave for each master. Then you add three slaves to the cluster and assign them to some master nodes so that some masters have more than one slave.

When a master loses slave, replication migration can migrate slave nodes from masters with redundant slaves to masters without slaves. So when your slave hangs up at 4 a.m.

Another slave will be moved to take its place, so that when the master node hangs up at 5 a.m., there will still be one slave that can be elected master, and the cluster will still function properly.

So in short, we should pay attention to the following aspects about replication migration:

- Clusters try to migrate slaves from master s with the largest number of slaves when migrating.

- To make use of the replication migration feature to increase system availability, you just need to add some slave nodes to a single master (which master node is not important).

- Replication migration is controlled by the configuration item cluster-migration-barrier

6. Upgrade Node

Upgrading the slave server node is simple, because you just need to stop the node and restart it with the updated Edits version. If clients use separate read requests from server nodes, they should be able to read requests at one node

Reconnect another slave server when unavailable.

Upgrading the primary server is slightly more complicated. The recommended steps are:

1) Use cluster failover to trigger a manual failover master server (see the manual failover section of this document).

2) Waiting for the master server to become the slave server.

3) Upgrade the node like upgrading from the server.

4) If you want your newly upgraded node to be the primary server, trigger a new manual failover, and make the upgraded node revert back to the primary server.

These steps can be followed to upgrade one node at a time until all nodes are upgraded.

At present, the start of redis cluster cluster can only be started by empty nodes. When the nodes have data, there will be err warning, but as long as fix, redis-trib.rb fix can use this command to repair OK.

Redis Cache Cleaning

1)Land redis

# src/redis-cli -c -h 192.168.1.100 -p 6379

2)Implementation:

192.168.1.100:6379> dbsize

3)

192.168.1.100:6379> flushall

================================================================

redis Cluster Designation key Cache cleaning is worthwhile:

1)Logged on to the specified port redis The server

# src/redis-cli -c -h 192.168.1.100 -p 6379

2)View all key Value (or use) info Commands can also be seen)

keys *

3)Delete the value of the specified index

del key

 

Topics: Redis network