标签:desc proc 解压 off 密码 运行 byte 日志 connect
准备
1.1 准备
keepalived-1.2.22.tar.gz
redis-3.2.1.tar.gz
1.2 说明
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
当 Master 挂掉,Slave 正常时, Slave接管服务升级Master,有写权限,同时关闭主从复制功能;
当 Master 恢复正常, Master降级为Slave同步数据,开启主从复制。
1.3 测试服务器信息
Master redis: 192.168.0.4
Slave redis: 192.168.0.5
配置redis服务
PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/soft/server/redis/src/redis-server
REDIS_CLI=/soft/server/redis/src/redis-cli
PIDFILE=/var/run/redis.pid
CONF="/soft/server/redis/redis.conf"
if [ ! -d /var/lib/redis ] ;then
mkdir -p /var/lib/redis
mkdir -p /var/log/redis
fi
case "$1" in
status)
ps -A|grep redis
;;
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac
复制代码
5. 配置开机自启
chkconfig --add redis-server
chkconfig redis-server on
配置keepalive服务
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
复制代码
3. keepalive配置文件
Master节点
复制代码
! Configuration File for keepalived
vrrp_script chk_redis {
script "/root/scripts/redis_check.sh" ###监控脚本
interval 2 ###监控时间
}
vrrp_instance VI_1 {
state BACKUP ###设置为BACKUP
nopreempt # 不抢占MASTER
interface eth0 ###监控网卡
virtual_router_id 51
priority 100 ###权重值
authentication {
auth_type PASS ###加密
auth_pass 1q2w3e ###密码
}
#设置额外的监控,里面的任意一个网卡出现问题,都会进入FAULT状态
track_interface {
eth0
}
track_script {
chk_redis ###执行上面定义的chk_redis
}
virtual_ipaddress {
192.168.0.99 ######VIP
}
notify_master /root/scripts/redis_master.sh
notify_backup /root/scripts/redis_backup.sh
notify_fault /root/scripts/redis_fault.sh
notify_stop /root/scripts/redis_stop.sh
}
复制代码
Slave节点
复制代码
! Configuration File for keepalived
vrrp_script chk_redis {
script "/root/scripts/redis_check.sh" ###监控脚本
interval 2 ###监控时间
}
vrrp_instance VI_1 {
state BACKUP ###设置为BACKUP
interface eth0 ###监控网卡
virtual_router_id 51
priority 10 ###比MASTRE权重值低
authentication {
auth_type PASS
auth_pass 1q2w3e ###密码与MASTRE相同
}
#设置额外的监控,里面的任意一个网卡出现问题,都会进入FAULT状态
track_interface {
eth0
}
track_script {
chk_redis ###执行上面定义的chk_redis
}
virtual_ipaddress {
192.168.0.99 ####vip
}
notify_master /root/scripts/redis_master.sh
notify_backup /root/scripts/redis_backup.sh
notify_fault /root/scripts/redis_fault.sh
notify_stop /root/scripts/redis_stop.sh
}
复制代码
4. keepalive脚本
当进入Master状态时会呼叫notify_master
当进入Backup状态时会呼叫notify_backup
当发现异常情况时进入Fault状态呼叫notify_fault
当Keepalived程序终止时则呼叫notify_stop
redis_check.sh 检测redis是否正常脚本,正常则返回PONG,返回代码0,否则返回代码1
复制代码
ALIVE=/usr/local/bin/redis-cli PING
if [ "$ALIVE" == "PONG" ]; then
echo $ALIVE
exit 0
else
echo $ALIVE
exit 1
fi
复制代码
redis_master.sh 我被选入Master时,执行的脚本
复制代码
REDISCLI="/usr/local/bin/redis-cli"
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_fault.sh 当检测服务不正常是,执行fault脚本
LOGFILE=/var/log/keepalived-redis-state.log
echo "[fault]" >> $LOGFILE
date >> $LOGFILE
redis_stop.sh keepalive终止时写入日志
LOGFILE=/var/log/keepalived-redis-state.log
echo "[stop]" >> $LOGFILE
date >> $LOGFILE
redis_backup.sh 进入slave时执行的脚本
复制代码
REDISCLI="/usr/local/bin/redis-cli"
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 192.168.0.5 6379 >> $LOGFILE 2>&1 # IP为之前从redis的IP
复制代码
这里backup 脚本主要说明一下,我的keepalive配置了不去抢占Master,也就是当我Master 宕机后,我master再次起来就是slave,这时就是执行redis_backup的时候了,我去指定我的Master去同步
role:master
connected_slaves:1
slave0:ip=192.168.75.131,port=6379,state=online,offset=1569,lag=1
master_repl_offset:1569
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:1568
systemctl stop redis
info
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:513
systemctl start redis
role:slave
master_host:192.168.75.131
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:43
slave_priority:100
slave_read_only:1
如果服务器资源不够的话,用keepalived就行了。两个节点就行了。sentinel是redis官网推荐的方案,需要的服务器比较多,最少三台,因为它用了节点仲裁原理来选master。
标签:desc proc 解压 off 密码 运行 byte 日志 connect
原文地址:https://www.cnblogs.com/wolf12/p/13371216.html