Redis Cluster is a distributed solution of Redis, which is officially launched in version 3.0 and effectively solves the requirements of Redis distributed. Cluster architecture can be used to achieve load balancing when encountering bottlenecks such as single memory, concurrency and flow.

1. Reference materials

Redis Development and Operation

2. Cluster principles

Redis Cluster uses virtual slot partitions. All keys are mapped to integer slots 0 to 16383 based on hash functions. Slot = CRC16(key) &16383. Each node is responsible for maintaining a portion of slots and the key data mapped to the slots.

Redis virtual slot partitioning features:

  • Decoupling the relationship between data and nodes simplifies node expansion and contraction
  • The node maintains slot mapping, and does not require the client or proxy service to maintain slot partition metadata
  • Supports query of mappings between nodes, slots, and keys for data routing and online scaling.

3. Create a cluster

3.1 Preparing Nodes

ip port role Configuration file name
192.168.51.145 6379 The master node
192.168.51.146 6379 The master node cluster.conf
192.168.51.147 6379 The master node cluster.conf
192.168.51.148 6379 From the node cluster.conf
192.168.51.149 6379 From the node cluster.conf
192.168.51.141 6379 From the node cluster.conf

3.2 Starting a Node

Run the following commands on each server

cd /usr/local/redis

redis-server ./conf/cluster.conf
Copy the code

3.3 Node Handshake

Run the following commands on 192.168.51.145

CD /usr/local/redis redis-cli -a foobared cluster meet 192.168.51.146 6379 cluster meet 192.168.51.147 6379 cluster meet 192.168.51.148 6379 Cluster meet 192.168.51.149 6379 Cluster meet 192.168.51.141 6379Copy the code

3.4 distribution channel

The Cluster addslots command is used to allocate slots to nodes. In this experiment, 16,384 slots are evenly allocated to 192.168.51.145, 192.168.51.146, 192.168.51.147

Execute the following command

Redis -cli -h 192.168.51.145 -a foobared -p 6379 cluster addslots {0.. 5461} redis-cli -h 192.168.51.146 -a foobared -p 6379 cluster addslots {5462.. 10922} redis-cli -h 192.168.51.147 -a foobared -p 6379 cluster addslots {10923.. 16383}Copy the code

3.5 Binding an Active Node to a secondary Node

Up to now, there are still three unused nodes: 192.168.51.148, 192.168.51.149, and 192.168.51.141. As a complete cluster, each node responsible for processing slots should have slave nodes to ensure automatic failover when it fails. In cluster mode, the roles of Redis nodes are divided into master node and slave node. The node that is started for the first time and the node that has a slot assigned to it are both primary nodes. Secondary nodes copy slot information and related data of the primary node. Use the redis replicate {nodeId} command to make a node a slave node.

The master node IP The master node port The node from the IP The node from the port
192.168.51.145 6379 192.168.51.148 6379
192.168.51.146 6379 192.168.51.149 6379
192.168.51.147 6379 192.168.51.141 6379

Log in to 192.168.51.148 and run the following command

CD /usr/local/redis redis-cli -a foobared // cluster replicate {192.168.51.145 Node ID} Cluster replicate 9b5a996bd1bfde43adcc8923d3a183053346dd75Copy the code

Log in to 192.168.51.149 and run the following command

CD /usr/local/redis redis-cli -a foobared // cluster replicate {192.168.51.146 node ID} Cluster replicate 72c3f823a265004ce1595b09df2d143e553fd487Copy the code

Log in to 192.168.51.141 and run the following command

CD /usr/local/redis redis-cli -a foobared // cluster replicate {192.168.51.147 node ID} Cluster replicate 46d3b15d1cab7191d0051e6d1ed84397eec62d0bCopy the code

At this point, the redis cluster cluster is set up. You can run the cluster nodes and cluster info commands to view the effect

4. Set up cluster relationships using automatic scripts

If you run the following command on 192.168.51.145, the script automatically allocates slot information and primary/secondary information

// Redis-cluster automatic allocation script: Redis -cli -a foobared --cluster create --cluster-replicas 1 192.168.51.145:6379 192.168.51.146:6379 192.168.51.147:6379 192.168.51.141 192.168.51.148:6379 192.168.51.149:6379:6379Copy the code

5. Configure the file address

Link: https://pan.baidu.com/s/1cBNyRH-iMq5PA7R-CZkLAQ

Extraction code: C0AK