Setting redis master-slave is mainly used to edit configuration files on different hosts
We have three main engines ready
server1, server2 and server3 respectively
The master-slave automatic switching of redis is based on sentinel
1. The Sentinel system of Redis is used to manage multiple Redis servers (instance s). The system performs the following three tasks:
1) Monitoring: Sentinel will constantly check whether your master server and slave server are working normally.
2) Notification: when a monitored Redis server has a problem, Sentinel can send a notification to the administrator or other applications through the API.
3) Automatic failover: when a master server fails to work normally, Sentinel will start an automatic failover operation, which will upgrade one of the failed master servers from the server to the new master server, and change other slave servers of the failed master server to copy the new master server; When the client tries to connect to the failed primary server, the cluster will also return the address of the new primary server to the client, so that the cluster can use the new primary server instead of the failed server.
Redis Sentinel is a distributed system. You can run multiple Sentinel processes (progress) in one architecture. These processes use gossip protocols to receive information about whether the master server is offline, and use agreement protocols To decide whether to perform automatic failover and which slave server to choose as the new master server.
Although Redis Sentinel is released as a separate executable file Redis Sentinel, it is actually just a Redis server running in a special mode. You can start Redis Sentinel by giving the – sentinel option when starting an ordinary Redis server.
2. Get Sentinel
At present, Sentinel system is part of the unstable branch of Redis. You must clone an unstable score on the Github page of Redis project, and then compile to obtain Sentinel system.
Sentinel program can be found in the compiled src document. It is a program named redis sentinel.
You can also run the redis server program in Sentinel mode through the methods described in the next section.
In addition, a new version of Sentinel has been included in the release file of redis version 2.8.0.
The above can be viewed on the Chinese official website of redis
www.redis.cn
3. Configure master-slave
Where server1 is the master
##In server2 vim /etc/redis/6379.conf 76 bind 127.0.0.1 -::1 172.25.50.2 95 protected-mode no
server2 and server3 are slave terminals
Repeat the installation and deployment in server1. You can view the installation and deployment of redis on the blog
The blog link is as follows
CSDNhttps://mp.csdn.net/mp_blog/creation/editor/120429818
Restart redis after configuration
vim /etc/redis/6379.conf 76 bind 127.0.0.1 -::1 172.25.50.2 95 protected-mode no 478 # replicaof <masterip> <masterport> 479 replicaof 172.25.50.1 6379 /etc/init.d/redis_6379 restart
vim /etc/redis/6379.conf 76 bind 127.0.0.1 -::1 172.25.50.3 95 protected-mode no 478 # replicaof <masterip> <masterport> 479 replicaof 172.25.50.1 6379 /etc/init.d/redis_6379 restart
You can view it in server1
redis-cli 127.0.0.1:6379> info # Replication role:master connected_slaves:2 slave0:ip=172.25.50.2,port=6379,state=online,offset=56028,lag=0 slave1:ip=172.25.50.3,port=6379,state=online,offset=56028,lag=1 If the above content is displayed, the configuration is successful
You can use the same method to view in server2 and server3
##In server2 redis-cli 127.0.0.1:6379> info # Replication role:slave master_host:172.25.50.1
stay server3 in redis-cli 127.0.0.1:6379> info # Replication role:slave master_host:172.25.50.1
4. Configure sentinel mode (high availability)
In server1
cp redis-6.2.4/sentinel.conf /etc/redis/ cd /etc/redis/ vim sentinel.conf 83 # The valid charset is A-z 0-9 and the three characters ".-_". 84 sentinel monitor mymaster 127.0.0.1 6379 2 ##server1 is the master. When the server hangs up, the slave side needs to vote on whether the master side hangs up. If two votes think the master is hung up, it means that server1 really hangs up 125 sentinel down-after-milliseconds mymaster 10000 ##The connection timeout is 10 seconds, and the default is 30 seconds scp sentinel.conf server2:/etc/redis/ scp sentinel.conf server3:/etc/redis/ redis-sentinel /etc/redis/sentinel.conf ##Enable redis Sentinel
At this time, you can also view in server2 and server3
redis-sentinel /etc/redis/sentinel.conf
The display is as shown in the figure
At this time, server1, server2 and server3 have started Sentinel
Now kill server1
Note: you can't directly kill in server1 that has already opened Sentinel. You should re open a server1 and kill again. Otherwise, after closing Sentinel, the automatic switching cannot be completed
Reopen a server2 and view it in it
redis-cli info
You can see that server2 has become a new master
At this point, restart redis in server1. You can see that server1 has become a slave
Description high availability configuration succeeded