1 Redis installation, keepalived installation
2 keepalived Configuration
Master
/etc/keepalived/keepalived.conf
############
! Configuration File for Keepalived
Vrrp_script Chk_redis {
Script "/etc/keepalived/scripts/redis_check.sh" # # #监控脚本
Interval 2 # # #监控时间
}
Vrrp_instance Vi_1 {
State MASTER # # #设置为MASTER
Interface Eth0 # # #监控网卡
VIRTUAL_ROUTER_ID 51
Priority # # # #权重值
Authentication {
Auth_type PASS # # #加密
Auth_pass 7IB8LAW3RB # # #密码
}
Track_script {
Chk_redis # # #执行上面定义的chk_redis
}
virtual_ipaddress {
192.168.2.200 # # #VIP
}
notify_master/etc/keepalived/scripts/redis_master.sh
notify_backup/etc/keepalived/scripts/redis_backup.sh
notify_fault/etc/keepalived/scripts/redis_fault.sh
notify_stop/etc/keepalived/scripts/redis_stop.sh
}
#########
/etc/keepalived/scripts/Create a script ( Note that the script has permission to execute ):
redis_master.sh
###############
#!/bin/bash
Rediscli= "/usr/local/redis/bin/redis-cli"
Logfile= "/var/log/keepalived-redis-state.log"
echo "[Master]" >> $LOGFILE
Date >> $LOGFILE
echo "Being master ..." >> $LOGFILE 2>&1
echo "Run slaveof cmd ..." >> $LOGFILE
$REDISCLI-P 6379-a admin slaveof 192.168.2.82 6379 >> $LOGFILE 2>&1
Sleep #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run slaveof NO one cmd ..." >> $LOGFILE
$REDISCLI-P 6379-a admin slaveof NO one >> $LOGFILE 2>&1
########
redis_backup.sh
########
#!/bin/bash
Rediscli= "/usr/local/redis/bin/redis-cli"
Logfile= "/var/log/keepalived-redis-state.log"
echo "[Backup]" >> $LOGFILE
Date >> $LOGFILE
echo "Being slave ..." >> $LOGFILE 2>&1
Sleep #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run slaveof cmd ..." >> $LOGFILE
$REDISCLI-P 6379-a admin slaveof 192.168.2.82 6379 >> $LOGFILE 2>&1
########
192.168.2.82 slave IP
redis_fault.sh
######
#!/bin/bash
Logfile=/var/log/keepalived-redis-state.log
echo "[Fault]" >> $LOGFILE
Date >> $LOGFILE
#####
redis_stop.sh
####
#!/bin/bash
Logfile=/var/log/keepalived-redis-state.log
echo "[Fault]" >> $LOGFILE
Date >> $LOGFILE
####
Backup
/etc/keepalived/keepalived.conf
##########
! Configuration File for Keepalived
Vrrp_script Chk_redis {
Script "/etc/keepalived/scripts/redis_check.sh" # # #监控脚本
Interval 2 # # #监控时间
}
Vrrp_instance Vi_1 {
State BACKUP # # #设置为BACKUP
Interface Eth0 # # #监控网卡
VIRTUAL_ROUTER_ID 51
Priority # # # #比MASTRE权重值低
Authentication {
Auth_type PASS
Auth_pass 7IB8LAW3RB # # #密码与MASTRE相同
}
Track_script {
Chk_redis # # #执行上面定义的chk_redis
}
virtual_ipaddress {
192.168.2.200 # # #VIP
}
notify_master/etc/keepalived/scripts/redis_master.sh
notify_backup/etc/keepalived/scripts/redis_backup.sh
notify_fault/etc/keepalived/scripts/redis_fault.sh
notify_stop/etc/keepalived/scripts/redis_stop.sh
}
Create a script under/etc/keepalived/scripts/:
redis_master.sh
####
#!/bin/bash
Rediscli= "/usr/local/redis/bin/redis-cli"
Logfile= "/var/log/keepalived-redis-state.log"
echo "[Master]" >> $LOGFILE
Date >> $LOGFILE
echo "Being master ..." >> $LOGFILE 2>&1
echo "Run slaveof cmd ..." >> $LOGFILE
# $REDISCLI-H 192.168.2.82-p 6379-a admin slaveof NO One
$REDISCLI-P 6379-a admin slaveof 192.168.2.81 6379
Sleep #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run slaveof NO one cmd ..." >> $LOGFILE
$REDISCLI-P 6379-a admin slaveof NO one >> $LOGFILE 2>&1
####
192.168.2.81 to Masterip
redis_backup.sh
#####
#!/bin/bash
Rediscli= "/usr/local/redis/bin/redis-cli"
Logfile= "/var/log/keepalived-redis-state.log"
echo "[Backup]" >> $LOGFILE
Date >> $LOGFILE
echo "Being slave ..." >> $LOGFILE 2>&1
Sleep #延迟15秒待数据被对方同步完成之后再切换主从角色
echo "Run slaveof cmd ..." >> $LOGFILE
# $REDISCLI slaveof-h 192.168.2.81-p 6379-a admin >> $LOGFILE 2>&1
$REDISCLI-H 192.168.2.82-p 6379-a admin slaveof 192.168.2.81 6379
####
redis_fault.sh
####
#!/bin/bash
Logfile=/var/log/keepalived-redis-state.log
echo "[Fault]" >> $LOGFILE
Date >> $LOGFILE
####
redis_stop.sh
####
#!/bin/bash
Logfile=/var/log/keepalived-redis-state.log
echo "[Stop]" >> $LOGFILE
Date >> $LOGFILE
####
Two Redis + keepalived master Preparation