redis-cli --cluster way to build a cluster
Redis cluster
Cluster Introduction
Current issues: peak bottlenecks encountered in the process of business development
- The service OPS provided by Redis can reach 100,000/second, and the current business OPS has reached 200,000/second
- The memory capacity of a single machine reaches 256G, and the current business demand memory capacity is 1T
The use of clusters can quickly solve the above problems
cluster architecture
The cluster is to use the network to connect several computers together, and provide a unified management method, so that it can present the service effect of a stand-alone computer externally.
cluster effect
- Distribute the access pressure of a single server to achieve load balancing
- Distribute the storage pressure of a single server to achieve scalability
- Reduce the business disaster caused by the downtime of a single server
Redis cluster structure design
data storage design
- Through algorithm design, calculate the location where the key should be saved
- Cut all storage space plans into 16384 copies, and save a part for each host
- Each copy represents a storage space, not a key storage space
- Put the key into the corresponding storage space according to the calculated result
- Enhanced scalability
Cluster internal communication design
- Each database communicates with each other and saves the number data of the slots in each library
- one hit, return directly
- Primary unhit, notification specific position
Cluster cluster structure construction
Construction method
- Native installation (single command)
- Configuration server (3 masters and 3 slaves)
- Establish communication (Meet)
- Slot (Slot)
- Build master-slave
- Tool installation (batch)
Cluster configuration
- add node
# 启用redis-cluster集群 cluster-enabled yes|no
- The name of the cluster configuration file, which is automatically generated and is only used to quickly find the file and query the file content
# 集群节点配置文件 # 该文件无需手工修改,由redis自动维护(创建和更新) # 需要注意,单机运行多实例时,确保该文件没有被其他实例覆盖(不允许重名) cluster-config-file <filename>
- Node service response timeout, used to determine whether the node is offline or switched to a slave node
# 节点超时时长(毫秒) cluster-node-timeout <milliseconds>
- The minimum number of slaves connected by the master
cluster-migration-barrier <count>
Cluster node operation commands
- View cluster node information
cluster nodes
- Enter a slave node redis, switch its master node
cluster replicate <master-id>
- Discover a new node, add a new primary node
cluster meet ip:port
- Ignore a node without a solt
cluster forget <id>
- manual failover
cluster failover
redis-cli --cluster command
- add node
redis-cli --cluster add-node new_host:new_port existing_host:existing_port --cluster-slav --cluster-master-id <arg>
- delete node
redis-cli --cluster del-node host:port node_id
- reshard
redis-cli --cluster reshard host:port --cluster-from <arg> --cluster-to <arg> --cluster-slots <arg> --cluster-yes --cluster-timeout <arg> --cluster-pipeline <arg> --cluster-replace
- More commands can be viewed through the following commands
redis-cli --cluster help
Specific steps to build a Cluster cluster structure
1. Configure the cluster in the server configuration file.
2. After configuring the cluster, modify the port and configuration file name to copy multiple configuration files
sed "s/6379/6380/g" redis-6379.conf > redis-6380.conf
3. Start all servers 6379 to 6384.
4. Use the following command to build a cluster cluster
redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1
–cluster-replicas 1 followed by 1 represents a master connected to a slave
–cluster-replicas 2 followed by 2 represents a master connected to two slaves
5. Test whether the cluster is built successfully. After building the cluster, you need to add -c to connect to the server
For example, the command to connect to the 6379 server is as follows:
redis-cli -c -p 6379
Connect to 6379 server to set data
Connect to 6380 server to get data
[root@192 conf]# redis-cli -c -p 6379
127.0.0.1:6379> set name qingbo
-> Redirected to slot [5798] located at 127.0.0.1:6380
OK
127.0.0.1:6380>
It can be seen that when a string-type key-value pair name = qingbo is stored on the server on port 6379, the operation is redirected to the server on port 6380, and the key-value pair name = qingbo is finally stored on 6380 port in the server.
Similarly, when obtaining data, it will also be redirected to the server where the corresponding data is actually stored, and then operate on the server.
[root@192 ~]# redis-cli -c -p 6380
127.0.0.1:6380> get name
"qingbo"
127.0.0.1:6380>
Problems and solutions in cluster construction
1.
[ERR] Node 127.0.0.1:6379 is not empty when building a Redis cluster, Either the node already knows other
nodes
The solution:
1. Modify the configuration file, and comment out the two lines dbfilename dump-6379.rdb and appendfilename "appendonly-6379.aof", and so on, in the configuration file of each node of the server used to build the cluster later Comment out or delete the corresponding .rdb and .aof
2. Shut down the process of each instance that was started before the Redis cluster, and restart it.
3. After starting, execute the following command again to build the cluster
redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1
2. (error) MOVED 5798 127.0.0.1:6380 error occurs when the client connects to redis-cluster (Redis cluster) to operate
data
The reason is: because the client does not set the cluster mode when starting redis-cli when connecting to the Redis server, use the -c parameter to start the cluster mode when starting, that is, bring the parameter -c to connect to the cluster cluster, the command as follows:
redis-cli -c -p 6379
[root@192 conf]# redis-cli -c -p 6379
127.0.0.1:6379> set name qingbo
-> Redirected to slot [5798] located at 127.0.0.1:6380
OK
127.0.0.1:6380>
It can be seen that when a string-type key-value pair name = qingbo is stored on the server on port 6379, the operation is redirected to the server on port 6380, and the key-value pair name = qingbo is finally stored on 6380 port in the server.
Similarly, when obtaining data, it will also be redirected to the server where the corresponding data is actually stored, and then operate on the server.
[root@192 ~]# redis-cli -c -p 6380
127.0.0.1:6380> get name
"qingbo"
127.0.0.1:6380>
3. Error [ERR] Not all 16384 slots are covered by nodes when building a cluster.
The reason:
This is often because the main node is removed, but the slots on the node are not removed, resulting in the total number of slots not reaching 16384. In fact, the distribution of slots is incorrect. Therefore, when deleting a node, you must pay attention to whether the deleted node is the Master node.
Solution:
1. Use the following command to repair the cluster
# redis-cli --cluster fix host:port
redis-cli --cluster fix 127.0.0.1:6379
2. After the repair is completed, use the check command to check whether it is correct. The command is as follows:
# redis-cli --cluster check host:port
redis-cli --cluster check 127.0.0.1:6379
3. If the distribution is uneven, you can use the following method to redistribute the slot
# redis-cli --cluster reshar host:port
redis-cli --cluster reshard 127.0.0.1:6379
Indeed, the command is as follows:
# redis-cli --cluster check host:port
redis-cli --cluster check 127.0.0.1:6379
3. If the distribution is uneven, you can use the following method to redistribute the slot
# redis-cli --cluster reshar host:port
redis-cli --cluster reshard 127.0.0.1:6379