Premise: the master-slave structure is in place, and the master database is not configured with persistence. Configure AOF from the slave database. (The master database is used for backup and write services, and the slave database is used to provide read services)
Scenario: What should I do if the master database suddenly goes down?
Method: connect to the slave database and perform the save operation. The latest dump. rdb file of the slave database will be saved in the data directory of the slave database. Copy the dump. rdb file to the data directory of the master database. Restart the master database.
This is because I thought of the cluster solution, but redis does not officially have a master-
Master mode ~ In this case, we can only find a solution for ourselves ~
Implementation principle ~
When both the Master and Slave work normally, the Master is responsible for reading and the Slave is responsible for synchronization;
When the Master node fails and the Slave is normal, the Slave takes over the service and disables the Master/Slave replication function;
And then cyclically.
In this way, if either of the two redis servers fails, the other server will continue to provide services without any noticeable impact on the website or data loss.
You can also enable the Master node to resume data synchronization from the Slave, disable Master-Slave replication after data synchronization, and restore the Master identity. After the Master node completes data synchronization, restore the Slave identity.
High Availability
If you want to separate the read/write operations, you can point to a vip ~ The two masters use keepalived and scripts for judgment.
If read, try to use haproxy for shunting. In this way, even if one is down, haproxy will automatically remove it ~
Install redis ~
- Wget http://redis.googlecode.com/files/redis-2.2.13.tar.gz
- Tar-zxf redis-2.2.13.tar.gz
- Cd redis-2.2.13
- Make
- Make install
Install keepalived ~
- Tar-xzvf keepalived-1.1.20.tar.gz
- Cd keepalived-1.1.20
- ./Configure -- prefix =/usr/local/webserver/keepalived
- Make
- Make install
- Cp/usr/local/webserver/keepalived/sbin/keepalived/usr/sbin
- Cp/usr/local/webserver/keepalived/etc/sysconfig
- Cp/usr/local/webserver/keepalived/etc/rc. d/init. d/keepalived/etc/init. d
- Mkdir/etc/keepalived
- Cp/usr/local/webserver/keepalived/etc/keepalived. conf/etc/keepalived
After the installation, let's talk about the high availability section. below is the configuration file of the master's keepalived. conf ~
Killall-0 redis-server is used to determine the survival of the process ~
You can also use the checkredis. sh script ~
[Simple for everyone ~] This method is a program that comes with redis to judge service survival.
- #! /Bin/bash
- REDIS_HOME = "/home/redis"
- REDIS_COMMANDS = "/home/redis/src" # The location of the redis binary
- REDIS_MASTER_IP = "172.16.0.180" # Redis MASTER ip
- REDIS_MASTER_PORT = "6379" # Redis MASTER port
- ERROR_MSG = '$ {REDIS_COMMANDS}/redis-cli ping'
- #
- # Check the output for PONG.
- #
- If ["$ ERROR_MSG "! = "PONG"]
- Then
- # Redis is down and return http 503
- /Bin/echo-e "HTTP/1.1 503 Service Unavailable \ r \ n"
- /Bin/echo-e "Content-Type: text/plain \ r \ n"
- /Bin/echo-e "\ r \ n"
- /Bin/echo-e "Redis is * down *. \ r \ n"
- /Bin/echo-e "\ r \ n"
- Exit 1
- Else
- # Redis is fine, return http 200
- /Bin/echo-e "HTTP/1.1 200 OK \ r \ n"
- /Bin/echo-e "Content-Type: text/plain \ r \ n"
- /Bin/echo-e "\ r \ n"
- /Bin/echo-e "Redis is running. \ r \ n"
- /Bin/echo-e "\ r \ n"
- Exit 0
- Fi