Redis Redis-Cluster cluster configuration

1 Introduction

Redis's master-slave replication and sentinel mode can improve the concurrency of reads, but the capacity of a single master is limited, and there will be a bottleneck when the data reaches a certain extent. At this time, it can be horizontally expanded to a multi-master cluster.
Redis-cluster is one of the commonly used redis cluster building methods. It can support multiple master-slaves, support massive data, and achieve high availability and high concurrency.
The sentinel mode is actually a kind of cluster. It can improve the concurrency of read requests, but there may be some problems in fault tolerance. For example, when the master synchronizes data to the slave, this is actually asynchronous replication. At this time, if the master is down, then the slave The data on the server is not new to the master, data synchronization takes time, and 1-2 seconds of data will be lost. After the master is restored and converted to slave, the new data is lost.

2 Features

1 Each node knows the relationship between each other and also knows its own role. Of course, they will also know that they exist in a cluster environment, and they can interact and communicate with each other, such as ping pong. Then these relationships will be saved in a configuration file, each node has, we will configure this when we build.
2 If the client wants to establish a connection with the cluster, it only needs to establish a relationship with one of them.
3 When a certain node is down, it is also detected by more than half of the nodes. After the objective is offline, the master-slave switch is the same as what we mentioned in the sentinel mode.
4 There are many slots in Redis, which can also be called slot nodes, which are used to store data.

The sentinel mode is actually a kind of cluster. It can improve the concurrency of read requests, but there may be some problems in fault tolerance. For example, when the master synchronizes data to the slave, this is actually asynchronous replication. At this time, if the master is down, then the slave The data on the server is not new to the master, data synchronization takes time, and 1-2 seconds of data will be lost. After the master is restored and converted to slave, the new data is lost.

The redis cluster has a fixed 16384 hash slots, calculate the CRC16 value for each key, and then take the modulo 16384 to get the hash slot corresponding to the key. Each master in the redis cluster will hold some slots. For example, if there are 3 masters, then each master may hold more than 5000 hash slots.

Hash slot makes it easy to add and remove nodes. Adding a master will move the hash slot of other masters to the past, and reducing a master will move its hash slot to other masters.

The cost of moving hash slots is very low.

The client's api can let them go to the same hash slot for the specified data, which is realized through hash tag.

3 Cluster fault tolerance

To build a Redis cluster, at least 3 nodes are required as masters to form a highly available cluster. In addition, each master needs to be equipped with a slave, so the entire cluster requires 6 nodes. This is also the most classic Redis cluster, which can also be called It is three masters and three slaves, with better fault tolerance. Therefore, 6 virtual machines are required when building. If it is virtual, you can configure a stand-alone version of Redis first, and then clone the virtual machine.

  • The cluster can also be built on a single server, which is called a pseudo-cluster, but the production environment is definitely real, so it is recommended to use six.
  • Be sure to close Redis after cloning.

4 Build a redis cluster

4.1 Environmental information

virtual machine IP
M1 192.168.51.101
M2 192.168.51.102
M3 192.168.51.103
S1 192.168.51.104
S2 192.168.51.105
S3 192.168.51.106

4.2 Modify the configuration file

修改redis.conf文件

# 开启集群模式
cluster-enabled yes
# 每一个节点需要有一个配置文件,需要6份。每个节点处于集群的角色都需要告知其他所有节点,彼此知道,这个文件用于存储集
cluster-config-file nodes-6379.conf 
# 超时时间,超时则认为master宕机,随后主备切换
cluster-node-timeout 5000
# 开启AOF
appendonly yes

4.3 Start redis

启动所有虚拟机的redis,, If an error is reported at startup, clear all RDB files

[root@localhost redis]# /etc/init.d/redis_init_script start

4.4 Create a cluster

#####
# 注意1:如果你使用的是redis3.x版本,需要使用redis-trib.rb来构建集群,最新版使用C语言来构建了,这个要注意
# 注意2:以下为新版的redis构建方式
#####
# 创建集群,主节点和从节点比例为1,1-3为主,4-6为从,1和4,2和5,3和6分别对应为主从关系,这也是最经典用的最多的集
redis-cli --cluster create ip1:port1 ip2:port2 ip3:port3 ip4:port4 ip5:port5 ip6:port6 --cluster-replicas 1

4.5 Check cluster information

[root@localhost redis]# redis-cli -a auskat --cluster check 192.168.51.101:6379

5 Related information

  • The blog post is not easy, everyone who has worked so hard to pay attention and praise, thank you

Guess you like

Origin blog.csdn.net/qq_15769939/article/details/113913457