Original website: http://www.jianshu.com/p/c2ab606b00b7
One, single instance
When there is only one Redis runtime in the system, once the Redis is hung up, the entire system will not run.
Single instance two, backup
Because single Redis has a single point of failure, it will cause the whole system is not available, so the idea is to backup (the general industry believes that the more secure backup number should be 3 copies). When a redis problem occurs, another redis can continue to serve.
Backup Three, automatic failover
Although the above Redis has been backed up, it looks perfect. However, since Redis currently only supports master-slave copy backups (which do not support primary master replication), when the master Redis is hung, only the read service from Redis is available and the write service is not available. So, think of a way to upgrade from Redis to master Redis when the master Redis hangs.
This requires automatic failover, which is provided by Redis Sentinel, which enables Redis Sentinel to upgrade from Redis to primary Redis and configure the other from Redis when a primary redis is not serviced. Have them copy the backup using the new master Redis.
Automatic failover Four, hands -on practice
1. Environment
Here you use three servers, one redis-server and Redis-sentinel service on each server, Redis-server port is 8000,redis-sentinel port is 6800, and modifying the default port is the first step to secure ^_^.
Redis-server Description 192.168.56.101:8000 Master 192.168.56.102:8000 from 192.168.56.103:8000
Redis-sentinel Description 192.168.56.101:6800 192.168.56.102:6800 192.168.56.103:6800
2. Building a Redis system
First Download 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
#复制redis相关命令到/usr/sbin directory so that you can execute these commands directly without writing the full path of the
sudo cp redis-cli redis-server Redis-sentinel /usr/sbin/
There are examples of redis.conf and sentinel.conf profiles under the Redis directory, using the sudo cp redis.conf sentinel.conf/etc/command to copy two configuration files to/etc directory (also available in/etc/ Directory to create a new profile), and then modify the configuration file.
Modify the contents of the main redis-server configuration file as follows:
Port 8000 #修改端口是安全的第一步
daemonize Yes
bind 0.0.0.0
pidfile /var/run/ Redis-8000.pid
logfile /var/log/redis/redis-8000.log
Modify the contents from the Redis-server configuration file as follows:
Port 8000 #修改端口是安全的第一步
daemonize Yes
bind 0.0.0.0
pidfile /var/run/ Redis-8000.pid
logfile /var/log/redis/redis-8000.log
slaveof 192.168.56.101 8000 # This line from Redis is more than master Redis
Start Redis-server.
sudo redis-server/etc/redis.conf
After three Redis services are started, go to the command line and execute info replication to see the current master-slave configuration.
View master and slave information on the main node
Found and did not have information from the node.
3. No communication between Master and slave
The reason for this is that the firewall has blocked 8000 ports and needs to modify the firewall settings to open 8000 ports (the same Redis-sentinel 6800 ports).
# Open Firewall config file, add 8000 port
sudo vim/etc/sysconfig/iptables
#修改完后, need to restart firewall
sudo service iptables restart
Add 8000 ports and 6800 ports
Then re-enter the master node, view the master-slave information, you can find two from the node information, indicating that Redis-server master and slave has been configured.
View master and slave information on the main node
4. Build Redis-sentinel System
Redis-sentinel program has been installed above, here only need to modify the configuration file. Modify the/etc/sentinel.conf if it is not created.
Modify the contents of 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 2
#5秒内master6800没有响应 that Sdown
Sentinel Down-after-milliseconds master8000
15000 Sentinel failover-timeout master8000
Start Redis-sentinel.
Redis-sentinel /etc/sentinel.conf
Three Redis-sentinel when the service is started, connect any Sentinel service to know the current master Redis service information.
Sentinel Monitoring status five, testing
1. Shut Down Redis
Redis-cli-h 192.168.56.101-p 8000 shutdown
2. View the monitoring status of the Redis-sentinel
Sentinel Monitoring Status
Found 102 This redis-server upgrade to the main library.
At this point, the high-availability Redis solution has been built. VI. Client Programs
IP and port are required for client programs (such as PHP programs) to connect to Redis, but when Redis-server fails over, the master Redis is changed, so the IP address is also changed. How the client program perceives the IP address and port of the current master Redis. Redis-sentinel provides an interface to request any Sentinel, send Sentinel Get-master-addr-by-name <master name> to get the current master Redis IP and port.
Gets the IP and port of the current master Redis
Each time the client connects to Redis, it sends a request to Sentinel, obtains the IP and port of the master Redis, and then connects Redis with the returned IP and port.
The drawback of this approach is that it is obvious that Redis needs to be sent at least two connection requests per operation, the first time Sentinel is requested, and Redis is requested the second time.
PHP Request Sentinel Program code can be found in: Https://github.com/huyanping/redis-sentinel
A better approach is to use the VIP, of course, the configuration of the environment has certain requirements, such as Redis built on Alibaba Cloud server, may not support VIP.
The VIP scheme is that the Redis system is always the same IP address, and when Redis fails over, it is necessary to drift the VIP from the previous Redis server to the new master Redis server.
For example: The current REDIS system in the main Redis IP address is 192.168.56.101, then VIP (192.168.56.250) point to 192.168.56.101, the client program with VIP (192.168.56.250) address to connect Redis, actually connected is Current Master Redis, which avoids sending requests to Sentinel.
When Master Redis goes down and fails over, 192.168.56.102 the Redis promotion on this server is the dominant one. At this point 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 connection is 192.168.56.102 this master Redis.
VIP Point 192.168.56.101
After failover, VIP drift points to 192.168.56.102 seven, drift VIP
The question now is how to drift the VIP to the new master Redis server when Redis failover occurs.
Here you can use a Redis sentinel parameter Client-reconfig-script, which configures the execution script, Sentinel executes this script when doing failover, and passes 6 parameters <master-name >, <role>, <state>, <from-ip>, <from-port>, <to-ip>, <to-port> <to-ip > is the IP address of the new master Redis, which can be used to perform VIP drift operations in this script.
Sentinel Client-reconfig-script master8000 /opt/notify_master6800.sh
Modify the Redis-sentinel profile of the three servers/etc/sentinel.conf, adding the line above. Then create the notify_master6800.sh script file under the/opt/directory, this script does the VIP drift operation, the contents are as follows: