Redis master-slave introduction
When the master Redis writes data, the slave Redis synchronizes the data through the Redis Sync mechanism to ensure data consistency. And Redis has a Sentinel mechanism. If the Redis master hangs up, it will automatically help us improve the slave master.
Master-Slave Synchronization Type and Sentinel Introduction
cascading replication
In order to avoid excessive reading and writing pressure on the main Redis at the same time, the three Redis including the above nodes can be configured for cascade replication. As shown in the following figure, node 4 can synchronize the data of node 3, and can also synchronize the data of node 2.
One master, many slaves
Two or more nodes are required. Because Redis is asynchronous synchronous data, the data written to the master node and returned to the client does not mean that the slave node has also written data, and the result of the slave node synchronization will not be returned to the master node, but will be discarded directly. Therefore (above) cascading replication from node 3 to synchronize data from node 2 increases the risk of losing data. The specific method depends on your own situation.
Sentinel mechanism
With master and slave, then we need to monitor it, Sentinel will constantly check whether your master server and slave server are working properly. After a node fails, Sentinel will start an automatic failover operation, it will upgrade one of the slave servers of the failed master server to the new master server, and change the other slave servers of the failed master server to copy the new master server; When the client tries to connect to the failed master server, the cluster also returns the address of the new master server to the client, so that the cluster can use the new master server instead of the failed server. Redis Sentinel is a distributed system, you can run multiple Sentinel processes (Progress) in an architecture, these processes use Gossip Protocols (Gossip Protocols) to receive information about whether the main server is offline, and use voting protocols (Agreement Protocols) ) to decide whether to perform an automatic failover and which slave to choose as the new master. Although Redis Sentinel is released as a separate executable, redis-sentinel, it is actually just a Redis server running in a special mode.
Redis master-slave configuration
System environment Debian, one master and one slave configuration.
Master node IP: 192.168.198.131
Slave node IP: 192.168.198.132
The port uses the default 6379
Node 1 and node 2 all execute the following commands, and different places are marked.
cd /data/ #I am used to putting Redis in the data directory
wget http://download.redis.io/releases/redis-3.2.9.tar.gz
tar -xf redis-3.2.9.tar.gz
mv redis-3.2.9 redis
cd redis/
make && make install
cp src/redis-server /usr/bin/redis-server
cp src/redis-cli /usr/bin/redis-cli
mv redis.conf redis.conf ~
cat redis.conf~ | grep -Ev "^$|^#" > redis.conf
vim redis.conf #Modify the following content, not necessary
bind 192.168.198.131 #The two hosts are changed to their own IPs respectively
logfile "/data/redis/logs/redis.log"
daemonize yes #Enable daemonize mode
slave-read-only yes #slave is read-only by default, so don't care here.
protected-mode no #protected-mode is a new feature added after 3.2, in order to prohibit the public network from accessing the redis cache and strengthen the security of redis. Configure it according to your own needs. There are two conditions for it to be enabled. There is no bind IP and no access password is set.
requirepass "admin.123" #Set redis login password
masterauth "admin.123" #Master-slave authentication password, otherwise the master-slave cannot be synchronized
To enable the master-slave mode, only Redis Slave adds a line, and the service master-slave is configured.
slaveof 192.168.198.131 6379
Check master-slave status
~]# redis-cli -c -h 192.168.198.131 -p 6379
192.168.198.131:6379> info
.......
# Replication # The content is omitted in the middle, you can look at it yourself, mainly to find this paragraph and see the master-slave state.
role:master
connected_slaves:1
slave0:ip=192.168.198.132,port=6379,state=online,offset=5700675,lag=0
master_repl_offset:5700675
repl_backlog_active:1
repl_backlog_size:10000000
repl_backlog_first_byte_offset:1
repl_backlog_histlen:570767
.......
Redis Sentinel configuration
vim sentinel.conf #In the directory of redis
port 26379
daemonize yes
protected-mode no #If the protection mode is enabled, the ipv4 and ipv6 address links that only accept loopback addresses are turned on, and external links are rejected, and normally multiple sentinels should be configured to avoid a dictatorship of one sentinel. If multiple sentinels are configured, if Turning it on will also reject connections from other sentinels. As a result, the sentinel configuration cannot take effect.
logfile "/data/redis/logs/sentinel.log" #Specify the log file
dir "/data/redis/sentinel"
sentinel monitor mymaster 192.168.198.131 6379 1 #The master monitored by the sentinel.
sentinel down-after-milliseconds mymaster 5000 #master or slave how much time (default 30 seconds) cannot be marked as down state.
sentinel failover-timeout mymaster 9000 #If the sentinel fails to complete the failover operation within the configuration value, the failover of the task fails this time.sentinel auth-pass mymaster tomredis.123 #If redis is configured with a password, then authentication must be configured here, otherwise it cannot be automatically switched
/data/redis/src/redis-sentinel /data/redis/sentinel.conf #Start service
If there is the following error, add --sentinel after the startup command
*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 6
>>> 'sentinel monitor mymaster 192.168.198.131 6379 1'
sentinel directive while not in sentinel mode
The configuration files of multiple sentinels are the same. Under normal circumstances, odd-numbered sentinels should be configured to avoid the same number of votes during switching, which may cause competition and affect online services.