标签:redis sentinel redis主从 redis高可用
sentinel作用系统环境:centos7
redis版本:4.0.8
主服务器IP:192.168.20.101 6379
从服务器IP:192.168.20.99 6379
主服务器sentinel端口:26379
从服务器sentinel端口:26379
[root] # wget http://download.redis.io/releases/redis-4.0.8.tar.gz
[root] # tar zxvf redis-4.0.8.tar.gz
[root] # cd redis-4.0.8
[root] # make
[root] # mkdir -p /usr/local/redis
[root] # cp /usr/local/src/redis-4.0.8/src/redis-server /usr/local/redis/
[root] # cp /usr/local/src/redis-4.0.8/src/redis-cli /usr/local/redis/
[root] # cp /usr/local/src/redis-4.0.8/redis.conf /usr/local/redis/
[root] # cd /usr/local/redis
[root] # vim /usr/local/redis/redis.conf
修改以下四项:
bind 0.0.0.0 #接受所有来自于可用网络接口的连接
daemonize yes #启用后台守护进程
protected-mode no #禁用保护模式
requirepass 123456 #设置密码
[root] # vim /etc/init.d/redis
#!/bin/sh
# description: Start and Stop redis
#PATH=/usr/local/bin:/sbin:/usr/bin:/bin
REDISPORT=6379
EXEC=/usr/local/redis/redis-server
REDIS_CLI=/usr/local/redis/redis-cli
PIDFILE=/var/run/redis_6379.pid
CONF="/usr/local/redis/redis.conf"
AUTH="123456"
case "$1" in
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
[root] # chmod 755 /etc/init.d/redis #可执行
[root] # chkconfig --add redis #添加系统服务
[root] # vim /etc/rc.local
在文件末尾添加以下内容
service redis start #开机启动
开启redis服务
[root] # service redis start
查看进程
[root] # ps aux | grep redis
主(master):192.168.20.101
从(slave):192.168.20.99
slaveof 192.168.20.101 6379
masterauth 123456
查看主从信息
[root] # cd /usr/local/redis
[root] # ./redis-cli -a 123456
[root] > info replication
主-停止redis
从-变主
从-保存数据,迁移数据到主redis
主-重启redis服务
从-重新切换成从redis
从-又成为只读redis服务
[root] # cp /usr/local/src/redis-4.0.8/sentinel.conf /usr/local/redis/ #复制sentinel配置文件
[root] # vim /usr/local/redis/sentinel.conf
修改如下配置
daemonize yes
protected-mode no
sentinel monitor mymaster 192.168.20.101 6379 1
sentinel down-after-milliseconds mymaster 1000
sentinel failover-timeout mymaster 5000
sentinel auth-pass mymaster 123456
详细配置及解释见redis中文网
[root] # netstat -lap |grep 26379
[root] # ./redis-cli -h 192.168.20.101 -p 26379
[root] # >info
kill主redis进程
自动切换成功
标签:redis sentinel redis主从 redis高可用
原文地址:http://blog.51cto.com/13660331/2096167