主:172.16.0.104
备:172.16.0.105
VIP:172.16.0.107
客户端直接连VIP,当master 104的redis挂掉后,105作为master。当104重启后,104作为105的slave。当105又挂掉之后,104作为master......
1、安装redis并配置主从同步
安装过程省略
1.1 配置104的redis实例:/etc/redis/redis.conf
requirepass 123456 #生产环境千万不要配置这么简单的密码
masterauth 123456 #备库密码,这里没有配置slaveof却配置masterauth是因为主备切换时需要用到密码
1.2 配置105的redis实例
requirepass 123456 #生产环境千万不要配置这么简单的密码
masterauth 123456 #主库密码
slaveof 172.16.0.104 6379
2、安装keepalived
安装过程省略
3、配置keepalived
Master(104)实例:
keepalived.conf
global_defs { router_id test } vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379 123456" interval 2 timeout 2 fall 3 } vrrp_instance redis { state BACKUP # 主也配置为SLAVE interface eth0 virtual_router_id 51 priority 150 nopreempt # 不抢占,注意加上 advert_int 1 authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 172.16.0.107 } track_script { chk_redis } notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 6379 123456" notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 6379 123456 172.16.0.105 6379" notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh
}
redis_master.sh
#!/bin/bash REDISCLI="redis-cli -h $1 -p $2 -a $3" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
redis_backup.sh
#!/bin/bash REDISCLI="redis-cli -h $1 -p $2 -a $3" LOGFILE="/var/log/keepalived-redis-state.log" echo "[backup]" >> $LOGFILE date >> $LOGFILE echo "Being slave...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF $4 $5 >> $LOGFILE 2>&1
Slave(105)实例
keepalived.conf
global_defs { router_id redis } vrrp_script chk_redis { script "/etc/keepalived/scripts/redis_check.sh 127.0.0.1 6379 123456" ###监控脚本 interval 2 ###监控时间 timeout 2 ###超时时间 fall 3 ###重试次数 } vrrp_instance redis { state BACKUP ###设置为MASTER interface eth0 ###监控网卡 virtual_router_id 51 priority 100 ###权重值 nopreempt # 不抢占,注意加上 advert_int 1 authentication { auth_type PASS ###加密 auth_pass 123456 ###密码 } track_script { chk_redis ###执行上面定义的chk_redis } virtual_ipaddress { 172.16.0.107 ###VIP } notify_master "/etc/keepalived/scripts/redis_master.sh 127.0.0.1 6379 123456" notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 6379 123456 172.16.0.104 6379" notify_fault /etc/keepalived/scripts/redis_fault.sh notify_stop /etc/keepalived/scripts/redis_stop.sh }
redis_master.sh
#!/bin/bash REDISCLI="redis-cli -h $1 -p $2 -a $3" LOGFILE="/var/log/keepalived-redis-state.log" echo "[master]" >> $LOGFILE date >> $LOGFILE echo "Being master...." >> $LOGFILE 2>&1 echo "Run SLAVEOF NO ONE cmd ..." >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
redis_backup.sh
#!/bin/bash REDISCLI="redis-cli -h $1 -p $2 -a $3" LOGFILE="/var/log/keepalived-redis-state.log" echo "[backup]" >> $LOGFILE date >> $LOGFILE echo "Being slave...." >> $LOGFILE 2>&1 echo "Run SLAVEOF cmd ..." >> $LOGFILE $REDISCLI SLAVEOF $4 $5 >> $LOGFILE 2>&1
公共配置
redis_check.sh
#!/bin/bash ALIVE=`redis-cli -h $1 -p $2 -a $3 PING` LOGFILE="/var/log/keepalived-redis-check.log" echo "[CHECK]" >> $LOGFILE date >> $LOGFILE if [ $ALIVE == "PONG" ]; then : echo "Success: redis-cli -h $1 -p $2 PING $ALIVE" >> $LOGFILE 2>&1 exit 0 else echo "Failed:redis-cli -h $1 -p $2 PING $ALIVE " >> $LOGFILE 2>&1 exit 1 fi
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
启动keepalived:keepalived -D
查看ip漂移情况:ip a
配置要点:
双BACKUP + nopreempt,优先级大的先启动作为master,当master挂掉后,slave被选中成为新的master。
参考文章:http://www.cnblogs.com/lulu/archive/2013/04/14/3021261.html
时间: 2024-10-17 13:16:39