一 主从高可用要求
1 主节点挂掉之后,从节点能在秒级内完成晋升为主
2 挂掉的节点恢复后,能追加为从节点
3 升主、恢复、挂掉的时候都会有日志和钉钉机器人消息通知,确保及时感知到redis的状态,尽早人工干预恢复。ke’y# 二 机器情况
Centos7.2的2核8G的机器
机器 | redis的docker挂载目录 | keepalived脚本目录 | 日志位置 |
---|---|---|---|
master 10.0.32.72 | /data/redis/data,/data/redis/redis.conf, /data/redis/logs | /etc/keepalived/scripts | /var/log/keepalived-redis* |
slave 10.0.32.73 | /data/redis/data,/data/redis/redis.conf, /data/redis/logs | /etc/keepalived/scripts | /var/log/keepalived-redis* |
三 redis.conf配置
两台机器都创建目录1
2
3
4mkdir -p /data/redis/data
mkdir -p /data/redis/logs
chmod 777 /data/redis/data
chmod 777 /data/redis/logs
master 10.0.32.72 /data/redis/redis.conf
1 | cat > /data/redis/redis.conf << EOF |
slave 10.0.32.73 /data/redis/redis.conf
1 | cat > /data/redis/redis.conf << EOF |
四 Docker启动redis
主从启动命令一致如下:1
docker run -dit --name redis --restart=always -v /data/redis/redis.conf:/etc/redis.conf -v /data/redis/data/:/data/ -v /data/redis/logs:/var/log -p 6379:6379 redis:4.0.1 redis-server /etc/redis.conf
启动redis之后copy redis-cli 到宿主机的 /usr/local/bin/目录下
1 | docker cp redis:/usr/local/bin/redis-cli /usr/local/bin/ |
五 Keepalived配置
master 10.0.32.72 /etc/keepalived/keepalived.conf
1 | ! Configuration File for keepalived |
slave 10.0.32.73 /etc/keepalived/keepalived.conf
1 | ! Configuration File for keepalived |
keepalived的脚本主备都是一致的
/etc/keepalived/scripts/redis_check.sh1
2
3
4
5
6
7
8
9
10
11
12
ALIVE=`/usr/local/bin/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
/etc/keepalived/scripts/redis_master.sh
1 |
|
/etc/keepalived/scripts/redis_backup.sh
1 |
|
/etc/keepalived/scripts/redis_fault.sh
1 |
|
/etc/keepalived/scripts/redis_stop.sh
1 |
|
六 启动顺序
- 先启动master 10.0.32.72 的 redis
- 再启动slave 10.0.32.73 的redis
- 启动master 10.0.32.72 的 keepalived
- 启动slave 10.0.32.73 的 keepalived
七 检查
1 两台机器 redis-cli 进入后执行
info Replication
查看主从信息
2 人为操作主节点挂掉,检查从节点是否会晋升为主节点,反之亦然。