Redis cluster Add Delete redistribution node example

Source: Internet
Author: User
Tags hash redis shuffle redis cluster

Redis cluster configuration is good, and after running for some time, we want to add nodes, or delete nodes, what to do.


One, redis cluster command line

Cluster (cluster)
CLUSTER Info Print cluster information
CLUSTER NODES Lists all nodes (node) currently known to the cluster and information about those nodes.

Nodes (node)
CLUSTER MEET <ip> <port> adds the nodes specified by IP and port to the cluster, making it a part of the cluster.
CLUSTER Forget <node_id> removes the node specified by node_id from the cluster.
CLUSTER REPLICATE <node_id> Sets the current node to the node from which NODE_ID is specified.
CLUSTER Saveconfig Saves the node's configuration file to the hard disk.

Groove (Slot)
CLUSTER addslots <slot> [slot ...] assigns one or more slots (slot) to the current node (assign).
CLUSTER delslots <slot> [slot ...] removes one or more slots for assignment to the current node.
CLUSTER Flushslots removes all slots assigned to the current node and makes the current node A node that does not have any slots assigned to it.
CLUSTER setslot <slot> node <node_id> assigns slot slot to the node specified by node_id, and if the slot is already assigned to another node, let the other node delete the slot and then assign it.
CLUSTER setslot <slot> Migrating <node_id> migrates the slot slot for this node to the node specified by node_id.
CLUSTER setslot <slot> importing <node_id> import slots slot to this node from node_id-specified nodes.
CLUSTER Setslot <slot> Stable cancels the import (import) or migration (migrate) of slot slot.

Key (Key)
CLUSTER keyslot <key> Compute key should be placed on which slot.
CLUSTER Countkeysinslot <slot> return slot slot the number of key value pairs currently contained.
CLUSTER Getkeysinslot <slot> <count> returns the keys in count slot slots.
These commands are unique to the cluster. To log in before executing the above command

[Root@manage redis]# redis-cli-c-P 6382-h 192.168.10.220//Login
192.168.10.220:6382> Cluster info//view cluster situation
Cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
Cluster_known_nodes:6
Cluster_size:3
Cluster_current_epoch:8
Cluster_my_epoch:4
cluster_stats_messages_sent:82753
cluster_stats_messages_received:82754


Two, add nodes


1, a 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
# 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 the master node


# redis-trib.rb Add-node 192.168.10.219:6378 192.168.10.219:6379 
Comments:
192.168.10.219:6378 is the new node
192.168.10.219:6379 cluster Any old node
3, add from Node
# redis-trib.rb Add-node--slave--master-id 03CCAD2BA5DD1E062464BC7590400441FAFB63F2 192.168.10.220:6385 192.168.10.219:6379 
Note:
--slave, Represents the addition of the node ID from the nodes
--master-id 03ccad2ba5dd1e062464bc7590400441fafb63f2, the primary node, here is the newly added 6378 node ID
192.168.10.220:6385, new node
192.168.10.219:6379 cluster any old node


4, Reassign slot

# redis-trib.rb Reshard 192.168.10.219:6378//Below is the main process

How many slots does you want to move (from 1 to 16384)? 1000//Set slot number 1000
What is the receiving node ID? 03CCAD2BA5DD1E062464BC7590400441FAFB63F2//New Node ID
Please enter all the source node IDs.
Type ' All ' to the nodes as source nodes for the hash slots.
Type ' Done ' once your entered all source nodes IDs.
Source node #1: All//means shuffle all nodes
Do your want to proceed with the proposed Reshard plan (yes/no)? Yes//confirm a new score


The newly added master node is not slots,


M:03ccad2ba5dd1e062464bc7590400441fafb63f2 192.168.10.219:6378
slots:0-332,5461-5794,10923-11255 (0 Slots) Master
If the master node is not slots, the access data will not be selected.
The allocation process can be understood as playing poker, all means shuffle; Enter the node ID of a master node, and then enter the done word, as if from a node, draw a card.


5, 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.


Three, change the master from the node

Check out the 6378 from the node
# 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//new master Node ID
Ok
192.168.10.220:6385> quit

View new Master's Slave
# redis-cli-p 6379 cluster Nodes | grep slave | grep 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052


Four, delete the node


1, remove from node

# redis-trib.rb Del-node 192.168.10.220:6385 ' 9c240333476469e8e2c8e80b089c48f389827265 '
2, delete the master node
If the primary node has a from node, it will be transferred from the node to the other master node
If the primary node has slot, remove the assigned slot, and then delete the primary node

# redis-trib.rb Reshard 192.168.10.219:6378//unassign slot, below is the main process

How many slots does you want to move (from 1 to 16384)? 1000//Total number of slot deleted master
What is the receiving node ID? 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052//Receive 6378-node slot master
Please enter all the source node IDs.
Type ' All ' to the nodes as source nodes for the hash slots.
Type ' Done ' once your entered all source nodes IDs.
Source node #1:03ccad2ba5dd1e062464bc7590400441fafb63f2//Deleted master Node-id
Source node #2:d One

Do your want to proceed with the proposed Reshard plan (yes/no)? Yes//Cancel slot, Reshard
After adding the master node, this was done, and then it was allocated, now removed. The opposite.

# redis-trib.rb Del-node 192.168.10.219:6378 ' 03ccad2ba5dd1e062464bc7590400441fafb63f2 '
The new master node is removed, so it goes back to the beginning of this article and has not yet added the state of the node

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.