summary
Redis sharding cluster is to cope with the data growth brought by business growth and realize online dynamic horizontal expansion and contraction. Each sharding master node in the cluster processes a subset of 16384 hash slots; In order to stabilize high availability deployment, enable one or more replica nodes for each partition node; When the partition master node hangs up, its corresponding replica node is automatically promoted to a new master node. When the original master node goes online again, it will automatically become the replica node of the new master node; When a pair of partitioned master-slave nodes fail, the whole cluster cannot work normally. Manual intervention is required to restore and start the failed node
Fragment cluster
1. Create a cluster with 3 sharded nodes
# Create a cluster with 3 shard nodes and 1 replica for each shard # redis-cli --cluster create 192.168.31.17:6384 192.168.31.18:6384 192.168.31.19:6384 --cluster-replicas 1 # Create a cluster with 3 shard nodes. Each shard has no replica by default redis-cli --cluster create 192.168.31.17:6384 192.168.31.18:6384 192.168.31.19:6384 > >>> Performing hash slots allocation on 3 nodes... > Master[0] -> Slots 0 - 5460 > Master[1] -> Slots 5461 - 10922 > Master[2] -> Slots 10923 - 16383 > M: 1db08d1048b20b22dfbf5c5d724d95f46a843d29 192.168.31.17:6384 > slots:[0-5460] (5461 slots) master > M: 3f9d82efdf04bb5fcf3741de4ea9314925bbeefd 192.168.31.18:6384 > slots:[5461-10922] (5462 slots) master > M: 16a0c6947fcb7d498b7c96aa5b5a70491298e966 192.168.31.19:6384 > slots:[10923-16383] (5461 slots) master > Can I set the above configuration? (type 'yes' to accept): yes > >>> Nodes configuration updated > >>> Assign a different config epoch to each node > >>> Sending CLUSTER MEET messages to join the cluster > Waiting for the cluster to join > .. > >>> Performing Cluster Check (using node 192.168.31.17:6384) > M: 1db08d1048b20b22dfbf5c5d724d95f46a843d29 192.168.31.17:6384 > slots:[0-5460] (5461 slots) master > M: 16a0c6947fcb7d498b7c96aa5b5a70491298e966 192.168.31.19:6384 > slots:[10923-16383] (5461 slots) master > M: 3f9d82efdf04bb5fcf3741de4ea9314925bbeefd 192.168.31.18:6384 > slots:[5461-10922] (5462 slots) master > [OK] All nodes agree about slots configuration. > >>> Check for open slots... > >>> Check slots coverage... > [OK] All 16384 slots covered.
verification
1. View the status of the specified redis node (including the number of keys, memory usage, requests, connections, etc.)
redis-cli --stat -p 6384 -h 192.168.31.17 > ------- data ------ --------------------- load -------------------- - child - > keys mem clients blocked requests connections > 30022 9.25M 2 0 30518 (+2) 59 > 30022 9.25M 2 0 30520 (+2) 59 > 30022 9.25M 2 0 30522 (+2) 59 > 30022 9.25M 2 0 30524 (+2) 59 > 30022 9.25M 2 0 30526 (+2) 59 > 30022 9.25M 2 0 30528 (+2) 59 > 30022 9.25M 2 0 30530 (+2) 59 2,View the distribution of hash slots and keys of each master node, And the number of copies and the total number of keys redis-cli --cluster info 192.168.31.17:6384 > 192.168.31.18:6384 (79c19006...) -> 49888 keys | 5462 slots | 1 slaves. > 192.168.31.17:6385 (3faafe60...) -> 50115 keys | 5461 slots | 1 slaves. > 192.168.31.19:6384 (df7d9f8e...) -> 49995 keys | 5461 slots | 1 slaves. > [OK] 149998 keys in 3 masters. > 9.16 keys per slot on average.
Cluster management
1. Redis cli interactive command line
redis-cli -p 6384 -h 192.168.31.17 # View cluster node information cluster nodes # meet add nodes, and any node can execute cluster meet 192.168.31.17 6385 # forget deletes a node according to the node ID. you need to execute this command on all nodes cluster forget 82d14741e161d07012783ae7c9bf1ef20ddc28a0 # Configure the current cluster node as the replica node of the specified master node (1db08d1048b20b22dfbf5c5d724d95f46a843d29) cluster replicate 1db08d1048b20b22dfbf5c5d724d95f46a843d29 # View the replica node of the specified master node (1db08d1048b20b22dfbf5c5d724d95f46a843d29) cluster replicas 1db08d1048b20b22dfbf5c5d724d95f46a843d29 cluster reset # Reset the current node cluster status file nodes.conf
2. Redis cli – cluster subcommand
# Execute the command client list on all nodes in the cluster redis-cli --cluster call 192.168.31.17:6384 client list # Add node 192.168.31.17:6385 as a new master node. You need to migrate some slots from other master nodes to the new master node to work normally redis-cli --cluster add-node 192.168.31.17:6385 192.168.31.17:6384 # Add node 192.168.31.17:6385 to the slave node of the master node (1db08b20b22dfbf5c5d724d95f46a843d29) redis-cli --cluster add-node 192.168.31.17:6385 192.168.31.17:6384 --cluster-slave --cluster-master-id 1db08d1048b20b22dfbf5c5d724d95f46a843d29 # Delete node 82d14741e161d07012783ae7c9bf1ef20ddc28a0 and close the node redis-cli --cluster del-node 192.168.31.17:6384 82d14741e161d07012783ae7c9bf1ef20ddc28a0 # Migrate 1024 slots from the specified node to the target node 073ad3a6dfe64289be66c6d8e302b29afc4b0ec7 redis-cli --cluster reshard 192.168.31.17:6385 --cluster-from 1db08d1048b20b22dfbf5c5d724d95f46a843d29 --cluster-to 073ad3a6dfe64289be66c6d8e302b29afc4b0ec7 --cluster-slots 1024 # Use the following commands to enter the interactive mode and enter the corresponding parameter values for slot migration between nodes redis-cli --cluster reshard 192.168.31.17:6384 # When the slots of each node are not balanced, execute this command to automatically balance the slots of each node redis-cli --cluster rebalance 192.168.31.17:6384
Capacity expansion
1. The capacity of the cluster master node is expanded from 3 master nodes to 5 master nodes
// Add new node 192.168.31. {18,19}: 6386 redis-cli --cluster add-node 192.168.31.18:6386 192.168.31.17:6384 redis-cli --cluster add-node 192.168.31.19:6386 192.168.31.17:6384 // Automatically balance the distribution of hash slots in each primary node of the cluster redis-cli --cluster rebalance 192.168.31.17:6384 --cluster-use-empty-masters redis-cli --cluster info 192.168.31.17:6384 > 192.168.31.18:6386 (c64357f2...) -> 29894 keys | 3278 slots | 0 slaves. > 192.168.31.18:6384 (79c19006...) -> 29927 keys | 3276 slots | 1 slaves. > 192.168.31.17:6385 (3faafe60...) -> 29835 keys | 3276 slots | 1 slaves. > 192.168.31.19:6384 (df7d9f8e...) -> 30083 keys | 3276 slots | 1 slaves. > 192.168.31.19:6386 (a0f71de7...) -> 30259 keys | 3278 slots | 0 slaves. > [OK] 149998 keys in 5 masters. > 9.16 keys per slot on average.
Volume reduction
1. The capacity of the cluster master node is expanded from 5 master nodes to 3 master nodes
// Migrate hash slot of node 192.168.31.18:6386 redis-cli --cluster reshard 192.168.31.17:6384 \ --cluster-from c64357f288902d1209fdf91f4804bf13b2a59fcf \ --cluster-to 79c19006ae3dcd12522090ddcc2b5b917b0feb2a \ --cluster-slots 3278 \ --cluster-yes // Migrate hash slot of node 192.168.31.19:6386 redis-cli --cluster reshard 192.168.31.17:6384 \ --cluster-from a0f71de78c2355f99de2b4c19796afb02e23c623 \ --cluster-to df7d9f8e2d7dbad37bdd1094c2770d1feabbd228 \ --cluster-slots 3278 \ --cluster-yes // View the distribution of hash slots and keys of each master node redis-cli --cluster info 192.168.31.17:6384 > 192.168.31.18:6386 (c64357f2...) -> 0 keys | 0 slots | 0 slaves. > 192.168.31.18:6384 (79c19006...) -> 59821 keys | 6554 slots | 1 slaves. > 192.168.31.17:6385 (3faafe60...) -> 29835 keys | 3276 slots | 1 slaves. > 192.168.31.19:6384 (df7d9f8e...) -> 60342 keys | 6554 slots | 1 slaves. > 192.168.31.19:6386 (a0f71de7...) -> 0 keys | 0 slots | 0 slaves. > [OK] 149998 keys in 5 masters. > 9.16 keys per slot on average. // Automatically balance the distribution of hash slots in each primary node of the cluster redis-cli --cluster rebalance 192.168.31.17:6384 // Delete node 192.168.31. {18,19}: 6386 redis-cli --cluster del-node 192.168.31.17:6384 c64357f288902d1209fdf91f4804bf13b2a59fcf redis-cli --cluster del-node 192.168.31.17:6384 a0f71de78c2355f99de2b4c19796afb02e23c623 // View the distribution of hash slots and keys of each master node redis-cli --cluster info 192.168.31.17:6384 > 192.168.31.18:6384 (79c19006...) -> 49762 keys | 5461 slots | 1 slaves. > 192.168.31.17:6385 (3faafe60...) -> 50060 keys | 5462 slots | 1 slaves. > 192.168.31.19:6384 (df7d9f8e...) -> 50176 keys | 5461 slots | 1 slaves. > [OK] 149998 keys in 3 masters. > 9.16 keys per slot on average.
transfer
Redis shake is a tool for synchronizing data between two redis. It supports Redis Cluster Online and offline migration (backup file import)
It supports four functions: parsing, recovery, backup and synchronization
1. Online migration
Full synchronization for the first time, and then incremental pull (via psync command)
# Create the migration target redis cluster redis-cli --cluster create 192.168.31.17:6386 192.168.31.18:6386 192.168.31.19:6386 # cat redis-shake.conf is the main configuration item on the source and target side. Other configuration items can be selected by default source.type: cluster # master can be changed to slave, which means synchronizing data from the replica to the target. 192.168.31.19 is any source redis node source.address: master@192.168.31.19:6384 target.type: cluster target.address: master@192.168.31.19:6386
./redis-shake -type sync -conf redis-shake.conf
2. Offline migration
It is applicable to scenarios where the network of the source instance and the target instance cannot be connected, or the source instance is deployed in Redis services of other cloud manufacturers, and online migration cannot be realized
# cat redis-shake.conf is the main configuration item on the source and target side. Other configuration items can be selected by default source.address: master@192.168.31.19:6384 # restore the backup file specified when importing data source.rdb.input: local_dump.0;local_dump.1;local_dump.2 target.type: cluster target.address: master@192.168.31.19:6386
Backup:. / redis shake - type dump - conf redis shake.conf
Restore:. / redis shake - type restore - conf redis shake.conf
Source: Redis Cluster deployment, online capacity expansion, capacity reduction, migration and cluster management practice - Wu muyixianhttps://www.iwmyx.cn/redisclusterbszxa.html
Source: operation and maintenance technical help