I. Single instance

If only one REDIS runs in the system, the system cannot run once the REDIS is down.





A single instance

Second, the backup

Because a single point of failure of a single Redis will lead to the unavailability of the entire system, so the natural way to think of is backup (generally, the industry believes that the safe backup number should be 3 copies). When one Redis has a problem, the other redis can continue to provide service.





The backup

Automatic failover

Although redis has made a backup above, it looks perfect. However, redis supports only master/slave copy backup (master/master copy is not supported). When the master Redis is suspended, the slave Redis can only provide read services, but cannot provide write services. Therefore, we have to find a way to upgrade from redis to primary Redis when the primary Redis fails.

This requires automatic failover. Redis Sentinel has this feature. When a primary Redis is not available, Redis Sentinel can upgrade a secondary Redis to a primary redis and configure the other secondary Redis to use the new primary redis for replication and backup.





Automatic failover

Fourth, hands-on practice

1. Environmental

There are three servers, each of which has a redis-server and a Redis-sentinel service enabled. The redis-server port is 8000, and the redis-sentinel port is 6800. Changing the default port is the first step to security.

Redis – server

  • Lord 192.168.56.101:8000
  • 192.168.56.102: from 8000
  • 192.168.56.103: from 8000

Redis – sentinel

  • 192.168.56.101:6800
  • 192.168.56.102:6800
  • 192.168.56.103:6800

2. Set up redis system

First download and install Redis

wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar zxvf redis-3.2.8.tar.gz
cd redis-3.2.8
make

cd src
Copy the redis commands to /usr/sbin so that you can execute them directly without writing to the full path
sudo cp redis-cli  redis-server  redis-sentinel   /usr/sbin/Copy the code

There are examples of redis. Conf and sentinel.conf configuration files in the redis directory, Run the sudo cp redis.conf sentinel.conf /etc/command to copy the two configuration files to the /etc directory (of course, you can also create a new configuration file in the /etc/ directory) and modify the configuration files.

Modify the main redis-server configuration file as follows:

port  8000Daemonize yes bind0.0. 0. 0
pidfile   /var/run/redis- 8000.pid
logfile   /var/log/redis/redis- 8000.logCopy the code

Modify the contents of the redis-server configuration file as follows:

port  8000Daemonize yes bind0.0. 0. 0
pidfile   /var/run/redis- 8000.pid
logfile   /var/log/redis/redis- 8000.log
slaveof  192.168. 56101.  8000# Secondary Redis has more lines than primary redisCopy the code

Start the redis server. –

sudo redis-server /etc/redis.confCopy the code

After the three Redis services are started, go to the CLI and run info replication to check the current primary/secondary configurations.





View primary/secondary information on the primary node

Found no slave node information.

3. The master and slave cannot communicate with each other

The reason is that the firewall shields port 8000. You need to modify firewall Settings to enable port 8000 (port 6800 of Redis-Sentinel is the same).

Open the firewall profile and add port 8000
sudo vim /etc/sysconfig/iptables

After modifying the firewall, restart the firewall
sudo service iptables restartCopy the code




Add ports 8000 and 6800

Log in to the primary node again and check the primary and secondary node information. The information about the two secondary nodes indicates that the primary and secondary nodes of the Redis-server have been configured.





View primary/secondary information on the primary node

4. Build the Redis-Sentinel system

The redis-Sentinel program is already installed, so you just need to modify the configuration file. Run the following command to modify /etc/sentinel.conf:

Modify the sentinel.conf configuration file as follows:

daemonize yes
port  6800
logfile  /var/log/redis/sentinel.log
pidfile  /var/run/sentinel.pid
sentinel monitor Master8000 192.168.56.101 8000 2If master6800 does not respond within 5 seconds, it is considered SDOWN
sentinel down-after-milliseconds master8000 5000  
sentinel failover-timeout  master8000 15000Copy the code

Start the redis – sentinel.

redis-sentinel  /etc/sentinel.confCopy the code

After the three RedIS-Sentinel services are started, the current master Redis service information can be obtained by connecting any Sentinel service.





Sentinel monitoring status

Five, the test

1. Stop redis

redis-cli -h 192.168. 56101. -p 8000 shutdownCopy the code

2. View the monitoring status of Redis-Sentinel





Sentinel monitoring status

We found 102 redis-server promoted to master library.





At this point, the high availability solution of Redis has been set up.

Vi. Client program

Client programs (such as PHP programs) need IP and port to connect to Redis, but when redis-server fails over, the primary Redis changes, so does the IP address. How does the client program sense the IP address and port of the current primary Redis? Redis-sentinel provides an interface to obtain the IP address and port of the current primary redis by sending sentinel get-master-addr-by-name

to request any sentinel.





Gets the IP and port of the current primary Redis

Each time a client connects to Redis, it sends a request to Sentinel to obtain the IP and port of the primary Redis, and then connects to Redis with the returned IP and port.

The disadvantages of this approach are obvious. Each redis operation requires at least two connection requests, the first for Sentinel and the second for REDis.

The PHP request for sentinel program code can be found at github.com/huyanping/r…

A better way is to use VIP, of course, this has certain requirements on the configuration environment, for example, Redis is built on Ali cloud server, may not support VIP.

In the VIP solution, the REDis system always has the same IP address externally. When THE REDis failover occurs, the VIP needs to be migrated from the previous REDis server to the new primary REDis server.

Such as: In the current REDis system, the IP address of the primary REDis is 192.168.56.101, so the VIP (192.168.56.250) points to 192.168.56.101, and the client program connects to redis with the VIP (192.168.56.250) address. The connection is actually the current primary Redis, which avoids sending a request to Sentinel.

When the primary REDis is down for failover, the redis on the server 192.168.56.102 is promoted to the primary server, and the VIP (192.168.56.250) points to 192.168.56.102, so that the client program does not need to modify any code. The primary redis 192.168.56.102 is connected.





VIP points to 192.168.56.101





After the failover, the VIP drifts to 192.168.56.102

Seven, drift VIP

The question then is how to migrate the VIP to the new primary Redis server in the event of a Redis failover.

The client-reconfig-script parameter of Redis Sentinel can be used. This parameter configures the execution script, which is executed by Sentinel during failover. And pass 6 parameters <master-name>, <role>, <state>, <from-ip>, <from-port>, <to-ip>, <to-port>, where <to-ip> is the IP address of the new primary Redis, You can do VIP drift in this script.

sentinel client-reconfig-script master8000   /opt/notify_master6800.shCopy the code

Add the preceding line to the /etc/sentinel.conf redis-sentinel configuration file of the three servers. In the /opt/ directory, create a notify_master6800.sh script that performs VIP drift as follows:

#notify_master6800.sh Script content
#! /bin/bash
MASTER_IP=$6  The sixth parameter is the IP address of the new primary Redis
LOCAL_IP='192.168.56.101'  192.168.56.102, 192.168.56.103
VIP='192.168.56.250'
NETMASK='24'
INTERFACE='eth1'
if [ ${MASTER_IP} = ${LOCAL_IP} ];then   
    /sbin/ip  addr  add ${VIP}/${NETMASK}  dev ${INTERFACE}  Bind the VIP to this server
    /sbin/arping -q -c 3 -A ${VIP} -I ${INTERFACE}
    exit 0
else 
   /sbin/ip  addr del  ${VIP}/${NETMASK}  dev ${INTERFACE}   Delete the VIP from this server
   exit 0
fi
exit 1  If 1 is returned, sentinel will always execute this scriptCopy the code

The current primary redis is 192.168.56.102. You need to manually bind VIP to the server.

/sbin/ip  addr add 192.168. 56250./24 dev eth1
/sbin/arping -q   -c 3 -A 192.168. 56250. -I eth1Copy the code

Then, go to another server and connect to Redis-Server and Redis-Sentinel via VIP address.





Connect to Redis via VIP

The current primary redis is 192.168.56.102.

Let’s shut down the Redis service and see if the VIP drifts to another server.

redis-cli -h 192.168. 56102. -p 8000 shutdownCopy the code




192.168.56.103 was upgraded to primary by sentinel query.





By accessing the VIP connection redis, it is found that the VIP does point to 192.168.56.103.

Eight, summary

Through the above operations, a REDis high availability system is built by using the scheme of REDis master-slave + Sentinel + drift VIP, but this system guarantees the high availability of a single REDis instance, so it is suitable for small business applications. If the business is large and the concurrency is high, it is recommended to build redis cluster, such as the official Redis Cluster and the open source Codings cluster.

In addition, drift VIP can be implemented using Keepalived software, which is not covered here.