一 前言 redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,Redis本身(包括它的很多客户端)都没有实现自动进行主备切换,而Redis-sentinel本身也是一个独立运行的进程,它能监控多个master-slave集群,发现master宕机后能进行自懂切换。它的主要功能有:
-
1 不时地监控redis是否按照预期良好地运行;
-
2 如果发现某个redis节点运行出现状况,能够通知另外一个进程(例如它的客户端);
-
3 能够进行自动切换。当一个master节点不可用时,能够选举出master的多个slave(如果有超过一个slave的话)中的一个来作为新的master,其它的slave节点会将它所追随的master的地址改为被提升为master的slave的新地址。
二 实践 本文介绍搭建主从复制以及三节点的sentinel 构建redis的高可用系统。其中 redis 一套主从 ,sentinel 三个节点 因为机器资源限制 ,两个sentinel 节点和 redis复用。
2.1 环境准备
-
Redis版本:Redis-3.2.8
-
OS 版本 CentOS 6
-
redis、sentinel
-
10.9.34.172 reids master
-
10.9.43.207 reids slave
-
sentinel
-
10.9.34.252 sentinel
2.2 软件安装
-
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
-
tar zxvf redis-3.2.8.tar.gz
-
mv redis-3.2.8 /usr/local/redis
-
cd /usr/local/redis
-
make PREFIX=/usr/local/redis install
-
cd src && make test
-
创建软连接
-
ln -s /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli ;
-
ln -s /usr/local/redis/bin/redis-sentinel /usr/local/bin/redis-sentinel ;
-
ln -s /usr/local/redis/bin/redis-server /usr/local/bin/redis-server ;
2.3 配置文件复制源码包里中的sentinel.conf和redis.conf文件到 /etc/redis 目录,我们需要重写编写配置文件,进行配置主从.
-
mkdir -p /etc/redis
-
cp /usr/local/redis/redis.conf /etc/redis/redis_7021.conf
-
cp /usr/local/redis/sentinel.conf /etc/redis/sentinel_17021.conf
-
cp /usr/local/redis/utils/redis_init_script /etc/init.d/redis
当然如果是想MySQL单机多实例那样,redis配置文件还可以放到redis 的实例里面 比如 /data/redis7021/redis.conf ,跟着redis实例走。
redis_7021.conf 的内容需要注意的是 本人为了图方便 去掉了权限以及 bind 并且设置了protected-mode 为no ,安装测试的过程中因为这两个参数导致额外的几个问题。生产上还是建议启用这两个参数的。还要记得本例子是主库,从库还要加上
slaveof master_ip port
sentinel_17021.conf 的内容master 的名称为youzan
-
port 17021
-
dir "/data/sentinel_17021"
-
logfile "/data/sentinel_17021/log/alert.log"
-
daemonize yes
-
protected-mode no
-
sentinel monitor youzan 10.9.34.172 7021 2
-
sentinel down-after-milliseconds youzan 5000
-
sentinel failover-timeout youzan 120000
-
#sentinel client-reconfig-script youzan /opt/bin/notify.py
-
rename-command SHUTDOWN REDIS_SHUTDOWN
-
#sentinel auth-pass youzan xxx
/etc/init.d/redis 的内容
-
#!/bin/sh
-
#
-
# Simple Redis init.d script conceived to work on Linux systems
-
# as it does use of the /proc filesystem.
-
REDISPORT=7021
-
EXEC=/usr/local/bin/redis-server
-
CLIEXEC=/usr/local/bin/redis-cli
-
PIDFILE=/data/redis_${REDISPORT}/run/redis.pid
-
CONF="/etc/redis/redis_${REDISPORT}.conf"
-
HOST=`hostname -i`
-
case "$1" in
-
start)
-
if [ -f $PIDFILE ]
-
then
-
echo "$PIDFILE exists, process is already running or crashed"
-
else
-
echo "Starting Redis server..."
-
su redis -c "$EXEC $CONF"
-
fi
-
;;
-
stop)
-
if [ ! -f $PIDFILE ]
-
then
-
echo "$PIDFILE does not exist, process is not running"
-
else
-
PID=$(cat $PIDFILE)
-
echo "Stopping ..."
-
$CLIEXEC -h $HOST -p $REDISPORT redis_shutdown
-
while [ -x /proc/${PID} ]
-
do
-
echo "Waiting for Redis to shutdown ..."
-
sleep 1
-
done
-
echo "Redis stopped"
-
fi
-
;;
-
*)
-
echo "Please use start or stop as first argument"
-
;;
-
esac
2.4 添加redis和 sentinel 账号。避免使用root账号直接启动 redis (存在相关安全隐患 利用Redis配置不当的漏洞对linux服务器root提权体验 )
-
useradd redis
-
mkdir -p /data/redis_7021/{data,log,run} ;
-
mkdir -p /data/sentinel_17021/{log,run} ;
-
chown -R redis.redis /data/redis_7021/ ;
-
chown -R redis.redis /etc/redis/ ;
-
useradd sentinel ;
-
chown -R sentinel.sentinel /data/sentinel_17021/ ;
-
chown -R sentinel.sentinel /etc/redis/sentinel_17021.conf ;
-
chmod 664 /etc/redis/sentinel_17021.conf
2.5 部署其他节点上面的过程是配置redis主库和其中的一个sentinel,复制上面的过程在10.9.43.207 重新执行一遍进行安装redis和sentinel,不过需要注意的是 我们设置10.9.43.207为redis的slave节点 ,所以要在配置文件redis_7021.conf中加上 slaveof 10.9.34.172 7021 。对于安装第三个sentinel节点10.9.34.252,只需选取上面步骤中的 sentinel相关的步骤就好。
2.6 启动redis和sentinel启动redis
/etc/init.d/redis start
关闭redis
/etc/init.d/redis stop
su sentinel -c "redis-sentinel /etc/redis/sentinel_17021.conf"
启动sentinel之后的日志如下
-
32126:X 31 Jul 23:28:41.570 # Sentinel ID is b28ca1bb3d722e09d0de905caa380397455e8e3c
-
32126:X 31 Jul 23:28:41.570 # +monitor master youzan 10.9.34.172 7021 quorum 2
-
32126:X 31 Jul 23:28:41.571 * +slave slave 10.9.43.207:7021 10.9.43.207 7021 @ youzan 10.9.34.172 7021
-
32126:X 31 Jul 23:28:54.484 * +sentinel sentinel 5c44560584c3245590e86fa78880eacccdfe8318 10.9.34.252 17021 @ youzan 10.9.34.172 7021
2.7 进行故障演练在主库上进行
-
10.9.34.172:7021> debug sleep 30
-
OK
-
(30.01s)
观察sentinel的log输出
32126:X 01 Aug 09:24:12.752 # +sdown master youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:12.828 # +odown master youzan 10.9.34.172 7021 #quorum 2/2
32126:X 01 Aug 09:24:12.828 # +new-epoch 1
32126:X 01 Aug 09:24:12.828 # +try-failover master youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:12.838 # +vote-for-leader b28ca1bb3d722e09d0de905caa380397455e8e3c 1
32126:X 01 Aug 09:24:12.842 # 5c44560584c3245590e86fa78880eacccdfe8318 voted for b28ca1bb3d722e09d0de905caa380397455e8e3c 1
32126:X 01 Aug 09:24:12.928 # +elected-leader master youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:12.928 # +failover-state-select-slave master youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:12.995 # +selected-slave slave 10.9.43.207:7021 10.9.43.207 7021 @ youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:12.995 * +failover-state-send-slaveof-noone slave 10.9.43.207:7021 10.9.43.207 7021 @ youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:13.047 * +failover-state-wait-promotion slave 10.9.43.207:7021 10.9.43.207 7021 @ youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:13.893 # +promoted-slave slave 10.9.43.207:7021 10.9.43.207 7021 @ youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:13.893 # +failover-state-reconf-slaves master youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:13.942 # +failover-end master youzan 10.9.34.172 7021
32126:X 01 Aug 09:24:13.942 # +switch-master youzan 10.9.34.172 7021 10.9.43.207 7021
32126:X 01 Aug 09:24:13.943 * +slave slave 10.9.34.172:7021 10.9.34.172 7021 @ youzan 10.9.43.207 7021
32126:X 01 Aug 09:24:18.986 # +sdown slave 10.9.34.172:7021 10.9.34.172 7021 @ youzan 10.9.43.207 7021
32126:X 01 Aug 09:24:37.041 # -sdown slave 10.9.34.172:7021 10.9.34.172 7021 @ youzan 10.9.43.207 7021
32126:X 01 Aug 09:24:47.041 * +convert-to-slave slave 10.9.34.172:7021 10.9.34.172 7021 @ youzan 10.9.43.207 7021
观察sentinel 的配置文件以及被修改重写.
三 总结 本文算是一篇入门级别的how to文档,介绍如何安装部署redis sentinel 高可用架构,内容比较浅显,需要注意的是运维一套数据库存储系统肯定不会这么简单,还要涉及到相关进程,日志,性能监控等等,本文的案例距离生产实践还有一大段路要走,继续学习。
四 参考文章[1] Redis之Sentinel高可用安装部署
[2] Redis Sentinel机制与用法(一)
[3] Redis Sentinel机制与用法(二)
网站栏目:【Redis】Sentinel高可用架构
当前链接:
http://cqcxhl.cn/article/jogsso.html