Redis Cluster has many modes, including master-slave mode, Sentinel mode, Cluster mode and so on.

Redis first uses the master/slave mode for clustering. In this mode, if the master fails, you need to manually configure the slave to master.

Based on the above shortcomings, Redis proposed the Sentinel mode, which uses the sentinel mechanism to monitor the survival of the master, and if the master fails, the sentinel mechanism will automatically elect and switch the master.

The disadvantage of sentinel mechanism is that it cannot dynamically expand the space, so Redis proposed the Cluster mode in 3.x version. This article mainly explains the Cluster mode configuration and various operations.

Cluster Mode introduction

Cluster mode, also known as redis-cluster Cluster, adopts a centrless structure. Each node stores partial data and status information of the whole Cluster, and each node has connections with other nodes.

Cluster Partitions provide a degree of availability that allows the Cluster to continue processing command requests even if some of the nodes in the Cluster fail or fail to communicate.

Cluster Cluster configuration

1. Configure multiple Redis instances

To ensure fault tolerance of the configuration file, I will perform operations in the copied configuration file.

> mkdir /usr/local/redis/redis_cluster Conf > cp /opt/ redis-6.2.3 /conf/redis.conf /usr/local/redis/redis_cluster/redis_6379.confCopy the code

Redis_6379. conf: redis_6379.conf:

The daemonize property specifies whether Redis is started as a daemon thread, which defaults to no.

Daemonize yes: enables daemonize mode. In this mode, Redis runs in the background and writes the process PID to the path file set by the pidfile option in the configuration file. In this case, Redis will continue to run until the process is killed or shutdown is executed.

Daemonize no: indicates that the daemon thread mode is disabled. In this mode, the Redis cli is displayed. If you run exit or close the connection tool (such as XShell), the Redis process exits.

daemonize yes
Copy the code

The default port number of Redis

port 6379
Copy the code

Configure the PID storage path

pidfile "/var/run/redis_6379.pid"
Copy the code

Rename the RDB file

dbfilename "dump6379.rdb"
Copy the code

The path to the cluster directory where multiple Rredis instances will be stored

dir "/usr/local/soft/redis6/redis_cluster"
Copy the code

Output directory of log files

Logfile "/ Cluster configuration path /redis_cluster/redis_err_6379.log"Copy the code

Enabling Cluster

cluster-enabled yes
Copy the code

The file that stores cluster node information is stored in the cluster directory

cluster-config-file nodes-6379.conf
Copy the code

Cluster node failure timeout period

cluster-node-timeout 15000
Copy the code

(2) Copy multiple instances

Since a cluster must have at least three primary nodes, let’s use six as an example. Start by copying 6 Redis configuration files:

[root@localhost redis_cluster]# cp redis_6379.conf redis_6380.conf
[root@localhost redis_cluster]# cp redis_6379.conf redis_6381.conf
[root@localhost redis_cluster]# cp redis_6379.conf redis_6389.conf
[root@localhost redis_cluster]# cp redis_6379.conf redis_6390.conf
[root@localhost redis_cluster]# cp redis_6379.conf redis_6391.conf
Copy the code

Modify the configuration file using vim’s find and replace operation:

# open vim redis_6380.confCopy the code
# replace all "6379" in this file with "6380" :s%/6379/6380Copy the code

2. Start the Redis instance

(1) Perform the following steps to start multiple Instances of Redis

[root@localhost redis_cluster]# redis-server /usr/local/soft/redis6/redis_cluster/redis_6379.conf
[root@localhost redis_cluster]# redis-server /usr/local/soft/redis6/redis_cluster/redis_6380.conf
[root@localhost redis_cluster]# redis-server /usr/local/soft/redis6/redis_cluster/redis_6381.conf
[root@localhost redis_cluster]# redis-server /usr/local/soft/redis6/redis_cluster/redis_6389.conf
[root@localhost redis_cluster]# redis-server /usr/local/soft/redis6/redis_cluster/redis_6390.conf
[root@localhost redis_cluster]# redis-server /usr/local/soft/redis6/redis_cluster/redis_6391.conf
Copy the code

(2) After the instance is started, check whether the file nodes-xxxx.conf is generated (XXXX is the port number).

3. Configure the cluster

(1) Configure the cluster into three groups: one master group and one slave group

[root@localhost redis6]# redis-cli --cluster create --cluster-replicas 1 192.168.43.2:6379 192.168.43.2:6380 192.168.43.2:6381 192.168.43.2:6389 192.168.43.2:63790 192.168.43.2:6391 # -replicas 1 specifies that one master corresponds to one slaveCopy the code

After the command is executed, the following information is displayed:

Enter yes and press Enter:

(2) Log in to Redis in cluster mode

(3) View the cluster information

10.6.207.225:6379 > cluster nodesCopy the code

At this point, the cluster collocation is complete.