Environment Introduction:
Realization of the principle
In each installation Sentinel and keepalived, master of the master and backup server priority 100, priority backup is 99, configure vrrp_script check script rules on the salve server, check the slave current role of the state, redis role of the state once the slave to master , put the slave priority plus 2 becomes 101, so as to obtain permission of vip;
When the master of redis service hang, Sentinel will be upgraded to the new slave master, slave checking role status is master priority plus 2 get vip, when the original master of redis up Sentinel service will be added to it as a slave master copy from
When becomes the master of the slave node redis service hang, redis sentinel will set the master node to the original master, vrrp_script redis not checked when their master, Save priority 2 becomes 99, the original master priority higher priority than the slave, the original master obtain permission vip
From the same master server following step, once installed here
First, install redis service and Sentinel
1, redis download the service pack to a server, unpacked and installed the compiler (the main step from the same installation only once here)
[Root @ centos01 /] # tar xzvf redis-3.2.9.tar.gz [root@centos01 /]# cd redis-3.2.9/ [root@centos01 redis-3.2.9]# make && make install [root@centos01 redis-3.2.9]# ./utils/install_server.sh Welcome to the redis service installer ## all the way round Installation successful!
2, modify redis configuration file, modified to run in the background, monitoring all addresses (other parameters modified as needed), to restart the service redis
[root@centos01 redis-3.2.9]# vim /etc/redis/6379.conf bind 0.0.0.0 daemonize yes [root@centos01 redis-3.2.9]# /etc/init.d/redis_6379 restart
3. Copy Sentry configuration files and startup files to the installation directory redis
[root@centos01 redis-3.2.9]# cp sentinel.conf /etc/redis/ [root@centos01 redis-3.2.9]# cp src/redis-sentinel /etc/redis/
Second, set redis from the master copy, and configured to monitor sentinel
1. Log in slave-redis server, the master is set to 192.168.2.225
[root@slave /]# redis-cli 127.0.0.1:6379> SLAVEOF 192.168.2.225 6379 ## set master to 2.225 127.0.0.1:6379> INFO Replication
3, modify the master and slave servers sentinel.conf configuration file (master and slave configuration consistent)
## Sentinel modify master files [root@master /]# cd /etc/redis/ [root@master /]# vim /etc/redis/sentinel.conf port 26379 dir "/tmp" sentinel monitor mymaster 192.168.2.225 6379 1 # (designated monitoring master address and port number, a plurality of sentinel expressed consent before switch over the primary) sentinel down-after-milliseconds mymaster 4000 # (less than the number of milliseconds master connection identified as master die) sentinel failover-timeout mymaster 18000 # (from the master when the master switch that failed to switch from a long) daemonize yes # (background) logfile "/var/log/redis_sentinel.log" # (log file path) [Root @ master redis] # ./redis-sentinel sentinel.conf # start Sentinel Services
Third, the configuration keepalived high availability
1, using the yum package installed keepalived (master and slave are installed)
[root@master redis]# yum install -y keepalived [Root @ master redis] # ./redis-sentinel sentinel.conf # start Sentinel Services
2, modify keepalived profile
Master configuration file contents ! Configuration File for keepalived global_defs { router_id redis-225 ## Device Name } vrrp_instance VI_redis { state MASTER ## device as the master interface eth0 ## vip card bundled virtual_router_id 79 ## backup group id, the same backup group to be consistent priority 100 ## priority, high priority becomes the master advert_int 3 ## conduct a health check every number of seconds authentication { auth_type PASS auth_pass redis } virtual_ipaddress { 192.168.2.253 dev eth0 label eth0: 1 ## vip address and set up a network card aliases } }
BACKUP profile contents
! Configuration File for keepalived global_defs { router_id redis-224 } vrrp_script chkmaster { script "/etc/keepalived/scripts/chkmaster.sh" # check whether the current master redis interval 3 # inspected once every 3 seconds When the unit weight 2 # is the master returns a detection result, priority plus 2 } vrrp_instance VI_redis { Status information of the machine state BACKUP # interface card eth0 #vip bound virtual_router_id 79 ## backup group id, the same backup group to be consistent priority 99 ## priority, high priority becomes the master advert_int 3 ## conduct a health check every number of seconds authentication { auth_type PASS auth_pass redis } virtual_ipaddress { 192.168.2.253 dev eth0 label eth0:1 } track_script {## call status check chkmaster } }
Check the status of the contents of the script:
#!/bin/bash STATUS=`/usr/local/bin/redis-cli info | grep role:master | wc -l` echo $STATUS if [ "$STATUS" -eq 0 ];then exit 1 else exit 0 be
Validation results:
1. Close master of redis service program, view vip and redis primary server switching to the slave
2. open the master of redis service program, redis service to slave status was added to the slave server
3. shut down slave server redis service, view vip and redis master switch to the master
4. open slave of redis service program, redis service to slave status was added to the master server
I never encountered a main switch of the pit
Stopped master main prompt handover from the error log
Solution:
1) If the instance is not configured redis
protected-mode yes
bind 192.168.98.136
Then the sentinel profile plus
protected-mode no
To
2) If there is redis configuration instance
protected-mode yes
bind 192.168.98.136
Then the sentinel profile plus
protected-mode yes
bind 192.168.98.136