A single node instance

Single node instance or relatively simple, usually do a test, write a small program if needed

To cache, start one

Redis is still very easy, as a key/value database can also be competent

2. Master/Slaver mode

Redis master-slave mode:

In the master/slave mode of Redis, data is copied asynchronously. The slave node asynchronously copies data from the master node. The master node provides read/write services, while the slave node only provides read services (this is the default configuration and can be controlled by modifying the slave-read-only configuration file). A master node can have multiple slave nodes. To configure a slave node, specify slaveof master-ip master-port in the redis.conf file.

The primary/secondary replication mode is enabled on the node

The configuration file

Add: slaveof to the configuration file of the server

Start the command

Redis-server starts the command and adds: slaveof

Client command

After the Redis server is started, run the slaveof command on the client to make the Redis instance become the slave node.

configuration

Switch to the Redis installation path

cd /www/server/redis/
Copy the code

Under config, create three configuration files

Active Node Configuration
#Create the primary node configuration
vi master-6379.conf
#Enter the configurationRequirepass 123456 Bind 0.0.0.0 port 6379 logfile "6379.log" dbfilename "dump-6379.rdb" daemonize yes rdbcompression yesCopy the code

Secondary Node Configuration

#Create a secondary node configuration
vi slave-6380.conf
#Enter the configurationBind 0.0.0.0 port 6379 logfile "6379.log" dbfilename "dump-6379.rdb" daemonize yes rdbcompression yes slaveof 0.0.0.0 6379 masterauth 123456
#Create a secondary node configuration
vi slave-6381.conf
#Enter the configurationBind 0.0.0.0 port 6381 logfile "6381.log" dbfilename "dump-6381. RDB "daemonize yes rdbcompression yes slaveof 0.0.0.0 6379 masterauth 123456Copy the code

Note: If the master node has a password, add masterauth to the configuration file of the slave node.

Run the Redis instance

[root@localhost redis]# ./src/redis-server config/master-6379.conf 

[root@localhost redis]# ./src/redis-server config/slave-6380.conf 

[root@localhost redis]# ./src/redis-server config/slave-6381.conf
Copy the code

Check out the Redis service

[root @ localhost redis] # ps aux | grep redis redis 0.0 0.7 164992 7332 11575? Ssl 18:00 0:03 / WWW/server/redis/SRC/redis - server 0.0.0.0: root 6379 13262 164992 6612 0.0 0.6? Ssl 18:16 0:02./ SRC /redis-server 0.0.0.0:6380 root 13275 0.0 0.4 164992 4520? Ssl 18:16 0:02./ SRC /redis-server 0.0.0.0:6381Copy the code

Test the master/slave mode

Is there a key on the three Redis

[root@localhost redis]#./ SRC /redis-cli -p 6379 127.0.0.1:6379> get name (nil) [root@localhost redis]#./ SRC /redis-cli -p 6380 127.0.0.1:6380> get name (nil) [root@localhost redis]#./ SRC /redis-cli -p 6381 127.0.0.1:6381> get name (nil)#The values obtained are null
Copy the code

Set a key for the master node

[root@localhost redis]#./ SRC /redis-cli -p 6379 127.0.0.1:6379> set name cmy OK 127.0.0.1:6379> get name "cmy"Copy the code

The slave node directly reads the value whose key is name

[root@localhost redis]#./ SRC /redis-cli -p 6380 127.0.0.1:6380> get name "cmy" [root@localhost redis]#./ SRC /redis-cli -p 6381 127.0.0.1:6381> get name "cmy"Copy the code

The slave node provides only read services but cannot write data

127.0.0.1:6381> set age 23
(error) READONLY You can't write against a read only slave.
Copy the code

Note: If service persistence is not enabled on the master node, data will be lost after the master node restarts, and the slave node will synchronize data from the master node again.

Cause of data loss: After the master service is suspended and restarted, the slave node performs a complete resynchronization with the master node. Therefore, data on the slave node is also lost because the master node does not persist. Therefore, when configuring the master/slave mode of Redis, you should turn on the persistence function of the master server.

3. Redis Sentinel construction

Deployment notes for Redis Sentinel

  1. A robustRedis SentinelClusters should be used at leastthree SentinelInstances, and ensure that these instances are placed inDifferent machinesOn, even differentPhysical area.
  2. SentinelThere is no guarantee thatStrong consistency.
  3. commonClient application libraryAll supportSentinel.
  4. SentinelNeed to go through constanttestTo observe theTo ensure high availability.

Redis Sentinel configuration file

#Port on which the Sentinel instance runs. The default is 26379  
port 26379
#Sentinel's working directory
dir ./

#Sentinel monitors the redis master node 
## IP: indicates the HOST IP address
## port: sentry port number
## master-name: specifies the name of the master node that can be named by itself (consisting of only letters A-z, digits 0-9, and ".-_").
## quorum: When these quorum sentinels consider the master node to be disconnected, the master node is objectively considered to be disconnected  
# sentinel monitor <master-name> <ip> <redis-port> <quorum>Sentinel Monitor MyMaster 127.0.0.1 6379 2
#When requirepass 
      
        is enabled in a Redis instance, all clients connected to the Redis instance must provide a password.
      
# sentinel auth-pass <master-name> <password>  
sentinel auth-pass mymaster 123456  

#Specifies the maximum interval between the primary node responding to sentinel sentinel. After this interval, the sentinel subjectively considers the primary node offline. Default: 30 seconds  
# sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds mymaster 30000  

#Specifies the maximum number of slaves that can synchronize the new master at the same time during a failover. The smaller the number, the longer it takes to complete the failover; The opposite is true, but if this number is larger, it means that more slaves are unavailable because of Replication. This value can be set to 1 to ensure that only one slave at a time is unable to process command requests.
# sentinel parallel-syncs <master-name> <numslaves>
sentinel parallel-syncs mymaster 1  

#Failover -timeout. The default value is three minutes. It can be used for the following purposes:
## 1. The interval between two failover operations for the same sentinel and the same master.  
## 2. Start when a slave synchronizes data from an incorrect master and end when the slave is corrected to synchronize data from the correct master.  
## 3. The time it takes to cancel an ongoing failover.
## 4. Maximum time required to configure all Slaves to point to the new Master when performing failover. However, even after this timeout, slaves will still be configured correctly to point to the master but will not synchronize data according to the rules configured for parallel Syncs
# sentinel failover-timeout <master-name> <milliseconds>  
sentinel failover-timeout mymaster 180000

#This script is invoked when sentinel has any warning level events (such as subjective and objective failures of redis instances, etc.). The maximum execution time of a script is 60 seconds. If this time is exceeded, the script will be terminated by a SIGKILL signal and executed again.
#The following rules apply to the result of running a script:  
## 1. If the script returns 1 after execution, the script will be executed again later, with the current default being 10.
## 2. If the script returns 2 after execution, or a value higher than 2, the script will not be executed twice.  
## 3. If the script is aborted during execution due to a system interrupt signal, it behaves the same as if the value is 1.
# sentinel notification-script <master-name> <script-path>  
sentinel notification-script mymaster /var/redis/notify.sh

#The script should be generic and can be called multiple times, not specific.
# sentinel client-reconfig-script <master-name> <script-path>
sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
Copy the code

Node planning for Redis Sentinel

role The IP address The port number
Redis Master 192.168.1.230 6379
Redis Slave1 192.168.1.67 6379
Redis Slave2 192.168.1.118 6379
Redis Sentinel1 192.168.1.230 26379
Redis Sentinel2 192.168.1.67 26379
Redis Sentinel3 192.168.1.230 26379

Redis Sentinel configuration setup

Configuration management of Redis-server

Primary node 192.168.1.230: redis.conf

Daemonize yes pidfile /var/run/redis-6379.pid logfile /var/log/redis-6379. log port 6379 bind 0.0.0.0 timeout 300 databases 16 dbfilename dump-6379.db dir ./redis-workdir masterauth 123456 requirepass 123456Copy the code

Secondary node 192.168.1.67: redis.conf

Daemonize yes pidfile /var/run/redis-6379.pid logfile /var/log/redis-6379. log port 6379 bind 0.0.0.0 timeout 300 databases 16 dbfilename dump-6379.db dir ./redis-workdir masterauth 123456 requirepass 123456Copy the code

Secondary node 192.168.1.118: redis.conf

Daemonize yes pidfile /var/run/redis-6379.pid logfile /var/log/redis-6379. log port 6379 bind 0.0.0.0 timeout 300 databases 16 dbfilename dump-6379.db dir ./redis-workdir masterauth 123456 requirepass 123456Copy the code

For automatic failover, it is recommended that all redis.conf files be set to masterauth. Because automatic failure only overwrites the master-slave relationship, i.e. Slaveof, not masterauth. If Redis does not have a password, it can be ignored.

Redis-server enables authentication

Take the primary node Redis as an example

[root@localhost redis]# ps -ef | grep redis redis 4603 1 0 10:34 ? 00:00:09 / WWW/server/redis/SRC/redis - server 0.0.0.0: root, 9823, 2649 and 6379 PTS / 0 00:00:00 grep -- color = auto redisCopy the code

Sentinel Configuration Management

All three Sentinel nodes have the same configuration file because they listen on the same primary node

Protected -mode no bind 0.0.0.0 port 26379 daemonize yes Sentinel monitor Master 192.168.1.230 6379 2 Sentinel down-after-milliseconds master 5000 sentinel failover-timeout master 180000 sentinel parallel-syncs master 1 sentinel auth-pass master 123456 logfile /var/log/redis/sentinel-26379.logCopy the code

Sentinel start-up verification

[root@localhost redis]# ps -ef | grep redis-sentinel root 2818 1 0 10:17 ? 00:00:15 SRC /redis-sentinel 0.0.0.0:26379 [sentinel] root 10639 2649 0 11:35 PTS /0 00:00:00 grep --color=auto redis-sentinelCopy the code

Redis Sentinel failover and recovery

[root@localhost redis]# ps -ef | grep redis redis 4603 1 0 10:34 ? 00:00:09 / WWW/server/redis/SRC/redis - server 0.0.0.0: root, 9823, 2649 and 6379 PTS / 0 00:00:00 grep -- color = auto redisCopy the code

192.168.1.230 The REDis process ID of the active node is 4603. To simulate a Redis service outage, actively kill the process.

[root@localhost redis]# kill -9 4603
Copy the code

Run the redis-CLI client command to access the sentinel-26379 node and check the status of the Redis node.

[root@localhost redis]# redis-cli -p 26379 127.0.0.1:26379> SENTINEL Master Master 1) "name" 2) "master" 3) "IP" 4) "192.168.1.230" 5) "port" 6) "6379" 7) "runid" 8) "8 f91932f47c89c6a362c7401aabcb15ac5a416a8" 9) "flags" 10) "master" 11)  "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "40" 19) "last-ping-reply" 20) "40" 21) "down-after-milliseconds" 22) "5000" 23) "info-refresh" 24) "3857" 25) "role-reported" 26) "master" 27) "role-reported-time" 28) "4299779" 29) "config-epoch" 30) "3" 31) "num-slaves" 32) "2" 33) "num-other-sentinels" 34) "2" 35) "quorum" 36) "2" 37) "failover-timeout" 38) "180000" 39) "parallel-syncs" 40) "1"Copy the code

More verification of the operation is not specific, interested partners can refer to the following elder brother’s article, very detailed!

reference

In-depth analysis of Redis series ii – Redis Sentinel mode and high availability cluster