One: Redis installation

1.1 Prepare the installation program

yum -y install gcc tcl
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
echo "source /opt/rh/devtoolset-9/enable" >> /etc/profile
Copy the code

1.2 To download Redis, go to the official website [Redis. IO /] Download the tar package, or…

Wget HTTP: / / https://download.redis.io/releases/redis-6.0.0.tar.gzCopy the code

1.3 Decompress the Redis installation package and go to the installation directory to initialize Redis

Gz CD redis-6.0.0/ make MALLOC=libc make test & make installCopy the code

Two: Redis cluster deployment

2.1 Cluster Deployment overview, application scenarios, and advantages

Redis clustering deployment is mainly used for large cache architectures. For general small architectures, redis master-slave configuration + Sentinel cluster can cope with system pressure. Using Redis cluster can easily and quickly dynamically expand the cluster, dynamically add and delete nodes, reshard, and with automatic fault recovery function.

2.2 Cluster Deployment and Common Operations and Faults

2.2.1 Directory Structure Configuration

Generally, the Redis cluster uses three master and three slave servers, and tries to ensure that the master server and the slave server are not on the same machine to prevent the cluster paralysis caused by machine failure. Each master server matches a slave server to ensure the high availability of the cluster.

Redis_7001 # redis configuration file: /etc/redis/701. conf # redis-cluster Cluster node status file: / etc/redis cluster/node - 7001. The conf # redis backup file storage directory: / var/redis / 7001 / appendonly aof&dump. RDB # redis log file storage directory: /var/run/redis_7001. Pid = /var/run/redis_7001Copy the code

Copy the redis startup script to the /utils/redis_init_script script in the redis installation directory, and then change the corresponding port number and configuration file directory. The corresponding configuration file changes are described in the next section; The configuration file that stores the cluster status cannot be changed. The configuration file automatically changes with the cluster status during the cluster running.

2.2.2 Configuration File Configuration
Daemonize yes # port 7001 # bind local IP address # redis directory dir /var/redis/7001/ # enable aOF Appendonly yes # start cluster cluster-enabled yes # cluster status configuration file directory cluster-config-file /etc/redis-cluster/node-701. conf # Node timeout, Cluster-node-timeout 15000 # redis instance process file pidfile /var/run/redis_701. pidCopy the code
2.2.3 Creating a Cluster

In redis3.0 and 4.0, clusters are still created using redis-trib.rb, but after 5.0, you can directly create clusters using redis-CLI. In redis 6.0, you can directly create clusters using redis-CLI

redis-cli --cluster create ip1:7001 ip1:7002 ip2:7003 ip2:7004 ip3:7005 ip3:7006 --cluster-replicas 1
Copy the code

Redis -cli is followed by the redis instance address list. The –cluster-replicas 1 parameter indicates that each primary server is expected to have a secondary server. The Redis cluster tries to allocate the primary and secondary servers on different machines

[OK] All 16384 slots covered
Copy the code

If the preceding information is displayed, the cluster is successfully created

2.2.4 Adding a Node

When the cluster is under heavy pressure, you can use dynamic capacity expansion to improve the concurrent write/read capability of the cluster

Redis -cli --cluster add-node ip3:7007 ip1:7001Copy the code

The first address parameter is the address of the redis instance to be added, and the second parameter is the address of any instance in the cluster. The node will be added as the master node by default. Note that the master node has two characteristics compared to other master nodes

  • It has no data because it has no hash slots assigned
  • Because it is a device with no slots allocated, it does not participate in the election process when the slave node is considered the master node

Add a successful identifier

Redis -cli --cluster add-node ip3:7007 ip1:7001 --cluster-slaveCopy the code

Above the ip3:7007 examples as cluster from the node to join in the cluster, will choose have the least number of master server from the server, and then put the new node as the master server from the node, then with the above two kinds of nodes are added to the cluster, you can use the command to see whether the node is added to the cluster

cluster nodes
Copy the code

As shown in the preceding command output, port 7007 has been added to the cluster instance and no slot is allocated

2.2.5 Rearranging the Cluster

After the primary node is added in the preceding way, the node is empty and no slot is allocated. In this case, you need to perform the reshard operation to allocate slots to the newly added primary node to share cluster pressure and improve the concurrency capability

  • 1: Enables the reshard operation
redis-cli -h ip1 -p 7001 --cluster reshard ip1:7001
Copy the code
  • 2: Specifies the number of slots to reshard

  • 3: specifies the destination node ID of the slot that receives these slots

  • 4: Specifies the source node ID of the slots assigned to the slots. Enter done as the terminator
Source node #1:ca33b3d7a60f8df7b74473f86c11f84df609fa45
Source node #2:done
Copy the code
2.2.6 Deleting a Node

When the system load is low, you can dynamically delete nodes to avoid resource consumption

redis-cli -h ip1 -p 7001 --cluster del-node ip1:7001 node-id
Copy the code

When you delete a node in the preceding way, you can delete the secondary node directly. When you delete the primary node, the primary node must not be empty, that is, the slot of the primary node and the data in the slot must be allocated to other primary nodes

2.2.7 Copy Migration

Copy of migration in the cluster concept, that is in some scenarios, may need to put a master server migration from the node to another master server, this situation is generally used in the case of from the node, in order to improve the operation of the high availability of the system, such as when there are three main server, there are three from one of the main server nodes, From a master server, there are two nodes, a master server from only one node, if the Lord from the pair of instance failure occurs at the same time, will cause the cluster is not available, this kind of circumstance, can take a main re-understanding of the pair of examples from a node moves to a master from the pair of instance, can avoid the above situation, to increase the availability of the system, The code for replica migration is shown below

Execute cluster replicate <master-node-id> on the corresponding slave nodeCopy the code

2.3 Common Cluster commands

List all known nodes in the cluster. Cluster Nodes # Set the current node to node_id. Cluster Replicate <node_id> # Calculate the slot on which the key should be placed cluster keyslot <key>Copy the code