This is the 12th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Download and install Redis

1. Download address

Distfiles.macports.org/redis/redis…

2. Decompress the Redis installation package

[root@open-falcon MNT]# CD redis-5.0.7 # redis [root@open-falcon MNT]# tar -zxvf redis-5.0.tar. gz # redis [root@open-falcon MNT]# CD redis-5.0.7 # [root@open-falcon redis-5.0.7]# ls 00-RELEASENOTES CONTRIBUTING Deps Makefile readme. md Runtest Runtest-moduleAPI sentinel.conf tests BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster runtest-sentinel src utils [root @ the open - falcon redis - 5.0.7] #Copy the code

3. Compile and install Redis

[root@open-falcon redis-5.0.7]# CD redis-5.0.7 # compile & install [root@open-falcon redis-5.0.7]# make && make installCopy the code

3. Cluster setup and configuration

1. Overall structure

In a Redis high availability cluster, three servers are selected: this way, if one of the primary nodes fails, the other two can elect one of them to be the master of the primary node.

① Configure the slave node to synchronize data from the master node

(2) Sentinel failover mechanism: Each Redis node sets up a monitoring sentinel. When the master node of Redis fails, three sentinel nodes elect a slave node to become the master node

1 Redis service 192.168.154.145 6379 Sentinel sentinel 192.168.154.145 26379 Slave 2 Redis service 192.168.154.145 26379 192.168.154.145 27001 Sentinel Sentinel 192.168.154.145 27002Copy the code

2. Pseudo cluster

Since we don’t have three servers on site, we’ll build a “pseudo cluster” for him using three ports on one server:

What is a pseudo cluster?

The “pseudo cluster” architecture is exactly the same as the real cluster, with failover and high availability.

It is built on one server and uses three ports to provide external services.

In the production environment, the cluster environment still requires three different servers to ensure high availability. Here is just to save resources, easy to build.

3. Configure the Redis service 6379 on the primary node

Complete configuration and steps are as follows:

Create the working directory of the primary node redis-6379 mkdir -p /usr/local/redis-6379 2. Copy the configuration file to the working directory cp redis-5.0.7/redis.conf /usr/local/redis-6379Copy the code

Conf vim /usr/local/redis-6379/redis.conf /usr/local/redis-6379/redis.conf 6379 136 daemonize yes 158 pidfile /var/run/redis_6379.pid 171 logfile "/usr/local/redis-6379/redis-6379.log 218 save 900 1 219 Save 300 10 220 save 60 10000 # Full backup filename 253 dbfilename redis-6379.rdb # /usr/local/redis-6379 # Set redis password 509 requirePass 123456 # Enable incremental backup 701 appendonly Yes 705 appendfilename "appendonly-6379.aof"Copy the code

4. Configure service 7001/7002 on the redis secondary node

Note that the slave node configuration differs from the master node configuration in two main places

① Configure the primary node and replicaof 192.168.154.145 6379

② Configure the password for logging in to the master node as masterauth 123456

Complete configuration and steps are as follows:

1. Create working directories redis-7001 and redis-7002 mkdir -p /usr/local/redis-7001 mkdir -p /usr/local/redis-7002 on the secondary nodeCopy the code

2. Copy the configuration file to the working directory cp redis-5.0.7/redis.conf /usr/local/redis-7001 cp redis-5.0.7/redis.conf /usr/local/redis-7002Copy the code

Conf vim /usr/local/redis-7002/redis.conf vim /usr/local/redis-7002/redis.confCopy the code

4. 7001 Secondary node configuration 69 bind 0.0.0.0 92 port 7001 136 daemonize yes 158 pidfile /var/run/redis_7001. Pid 171 logfile "/usr/local/redis-7001/redis-7001.log" # Default configuration 218 Save 900 1 219 Save 300 10 220 Save 60 10000 # Full backup file name 253 Dbfilename redis-7001. RDB # specifies path name 263 dir /usr/local/redis-7001 # Specifies primary node address port + primary node connection password 286 replicaof 192.168.154.145 6379 293 masterauth 123456 # Set redis password 509 requirePass 123456 # Enable incremental backup 701 appendonly Yes 705 appendfilename "appendonly-7001.aof"Copy the code

Configure the secondary 7002 node. 69 bind 0.0.0.0 92 port 7002 136 daemonize yes 158 pidfile /var/run/redis_7002.pid 171 logfile "/usr/local/redis-7002/redis-7002.log" # Default configuration 218 Save 900 1 219 Save 300 10 220 save 60 10000 # Full backup file name 253 Dbfilename redis-7002. RDB # specify path name 263 dir /usr/local/redis-7002 # Specify primary node address port + primary node connection password 286 replicaof 192.168.154.145 6379 dbfilename redis-7002 293 masterauth 123456 # Set redis password 509 requirePass 123456 # Enable incremental backup 701 appendonly Yes 705 appendfilename "appendonly-7002.aof"Copy the code

4. Failover/High Availability configuration (Sentinel mode)

1. Overall architecture

Secondary node Sentinel: Redis service on port 7001, Redis service on port 27001, Redis service on port 7002, Redis service on port 27002Copy the code

2. Configure sentinel on the master node

For the convenience of management, we built the sentinel working directory under the corresponding Redis working directory

Note:

① Configure the address and name of the master (myMaster is the cluster name) : Sentinel Monitor myMaster 192.168.154.145 6379 2

② Configure the password sentinel auth-pass mymaster 123456 for connecting to the redis primary node

③ The log directory must be created. If a problem occurs, you can easily find the cause. logfile “/usr/local/redis-6379/sentinel/redis-sentinel.log”

1. Create the sentinel working directory mkdir -p /usr/local/redis-6379/sentinel/ for the primary nodeCopy the code

2. Copy the configuration file to the sentinel working directory cp redis-5.0.7/sentinel.conf /usr/local/redis-6379/sentinel/Copy the code

3, modify the configuration file The left to modify the number of rows, facilitate everybody find vim/usr/local/redis - 6379 / sentinel/sentinel. Conf # configuration sentry port 26379 # 21 port configuration process id 32 pidfile memory address /usr/local/redis-6379/redis-sentinel-26379.pid" /usr/local/redis-6379/redis-sentinel-26379.pid" 64 dir "/usr/local/redis-6379/sentinel 120 Sentinel auth-pass mymaster 123456Copy the code

3. Configure sentinel on the slave node

① The configurations of the secondary node are the same as those of the primary node, except for the different working directories and ports

1. Create the working directory mkdir -p /usr/local/redis-7001/sentinel-mkdir -p /usr/local/redis-7002/sentinel-/ of the secondary nodeCopy the code

2. Copy the configuration file to cp redis-5.0.7/sentinel.conf /usr/local/redis-7001/sentinel/ cp redis-5.0.7/sentinel.conf /usr/local/redis-7002/sentinel/Copy the code

3, respectively modified slave node sentry vim configuration file/usr/local/redis - 7001 / sentinel/sentinel. Conf vim/usr/local/redis - 7002 / sentinel/sentinel. ConfCopy the code

Port 27001 # Configure process ID storage address 32 pidfile "/usr/local/redis-7001/redis-sentinel-27001.pid" 64 dir "/usr/local/redis-7001/sentinel 120 Sentinel auth-pass mymaster 123456Copy the code

# configure sentry port 21 port 27002 # Configure process ID storage address 32 pidfile "/usr/local/redis-7002/redis-sentinel-27002.pid" "/usr/local/redis-7002/ redis-sentinel.log" 64 dir "/usr/local/redis-7002/sentinel 120 Sentinel auth-pass mymaster 123456Copy the code

The configuration is complete.

5. Start the cluster

1. Start all redis service nodes

[root@mail ~]# redis-server /usr/local/redis-6379/redis.conf [root@mail ~]# redis-server /usr/local/redis-7001/redis.conf [root@mail ~]# redis-server /usr/local/redis-7002/redis.confCopy the code

2. Start all sentinel services

# When starting, According to the configuration file to start/root @ mail ~ # redis - sentinel/usr/local/redis - 6379 / sentinel/sentinel. Conf/root @ mail ~ # redis - sentinel /usr/local/redis-7001/sentinel/sentinel.conf [root@mail ~]# redis-sentinel /usr/local/redis-7002/sentinel/sentinel.confCopy the code

3. View the service process

# below: three redis service + the sentinel surveillance All startup success/root @ mail ~ # ps aux | grep redis root 50845 158068 3904 0.0 0.1? Ssl 07:50 0:42 Redis-server 0.0.0.0:6379 root 50166 0.0 0.1 161652 3768? Ssl 07:39 0:43 Redis-server 0.0.0.0:7001 root 50173 0.1 0.2 161652 4884? Ssl 07:40 0:53 Redis-server 0.0.0.0:7002 root 50370 0.1 0.1 152436 3692? Ssl 07:43 1:14 Redis-sentinel *:26379 [Sentinel] root 50375 0.1 0.0 152436 1748? Ssl 07:43 1:15 Redis-sentinel *:27001 [sentinel] root 50431 0.1 0.0 152436 1712? Ssl 07:43 1:14 redis-sentinel *:27002 [sentinel]Copy the code

Six, test: data synchronization + read/write separation + failover

1. Data synchronization test

Test target: Master The master node writes, modifies, or deletes data. Slave the slave node automatically synchronizes data operation records of the master node.

① Redis -cli links to the master node and writes the age and name data

[root@mail ~]# redis-cli -a 123456 -p 6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> Set age 25 OK 127.0.0.1:6379> set name lex OKCopy the code

② Log in to the slave node

[root@mail ~]# redis-cli -a 123456 -p 7001 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe 127.0.0.1:7001> set age 30 (error) READONLY You can't Write Against a read only replica. # Data on the slave node is read onlyCopy the code

2. View the information on the Master and slave nodes

① Link sentinel: Check the distribution of master and slave nodes

2. Link command: redis-cli -a 123456 -p 26379

Check master IP address: 6379 Port service

Redis -cli [root@mail ~]# redis-cli -a 123456 -p 26379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe 127.0.0.1:26379> Sentinel master mymaster 1) "name" 2) "mymaster" 3) "IP" 4) "192.168.154.145" 5) "port" 6) "6379" 7) "runid" 8) "105cc1c986fcda65ad4ddba69012e8e6e0073e1f" 9) "flags" 10) "master" ...Copy the code

③ Check the IP address of the slave node. The two slave nodes are 7001 and 7002 respectively. You can see that the master node 6379 is pointed to by the two slave nodes

# Command: 127.0.0.1:26379> Sentinel Slaves mymaster 1) 1) "name" 2) "192.168.154.145:7001" 3) "IP" 4) "192.168.154.145" 5) "port" 6) "7001" 9) "slave" 31) "master-link-status" 32) "ok" 33) "master-host" 34) "192.168.154.145" 35) "master-port" 36) "6379" ... 2) 1) "192.168.154.145:7002" 3) "IP" 4) "192.168.154.145" 5) "port" 6) "7002" 9) "flags" 10) "slave" 31) "Master-link-status" 32) "ok" 33) "master-host" 34) "192.168.154.145" 35) "master-port" 36) "6379"...Copy the code

3. Failover (high availability) testing

Test purpose: After the master node fails, our Sentinel cluster can re-elect one of the slave nodes as a new master node through the election mechanism.

① Shut down the active node 6379

# redis-cli -a 123456 -p 6379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> shutdown not connected>Copy the code

② Check the current master node

Link to the sentry to see the primary node: at this point, after the election of the three sentries, the primary node becomes 7002

[root@mail ~]# redis-cli -a 123456 -p 26379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:26379> sentinel Master mymaster 1) "name" 2) "mymaster" 3) "IP" 4) "192.168.154.145" 5) "port" 6) "7002" 7) "runid" 8) "105cc1c986fcda65ad4ddba69012e8e6e0073e1f" 9) "flags" 10) "master" ... 127.0.0.1:26379 >Copy the code

③ Check the status of the slave node

We found that the slave node changed to: 6379 and 7001, and that 6379’s state was “s_down,slave,disconnected”, because we closed 6379

[root@mail ~]# redis-cli -a 123456 -p 26379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:26379> sentinel Slaves Mymaster 1) 1) "name" 2) "192.168.154.145:6379" 3) "IP" 4) "192.168.154.145" 5) "port" 6) "6379" 7) "Runid" 8) "e740b7285067b2dbb8c2640745196099460f4bb3" 9) "flags" 10) "s_down,slave,disconnected" ... 2) 1) "name" 2) "192.168.154.145:7001" 3) "IP" 4) "192.168.154.145" 5) "port" 6) "7001" 7) "runid" 8) "19a7647509ba020adfd06b1eb40f1f35d06c549d" 9) "flags" 10) "slave" ... 127.0.0.1:26379 >Copy the code

④ The failover is successful.

4. Separate read and write tests

① After the Redis cluster is enabled, the slave node is read-only by default. We can also enable write permissions on slave nodes.

So, write data: link to the master node; Read data: Links slave slave nodes.

The following figure shows the slave node that has no write permission

[root@mail ~]# redis-cli -a 123456 -p 7001 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe 127.0.0.1:7001> set age 30 (error) READONLY You can't Write Against a read only replica. # Data on the slave node is read onlyCopy the code