zhoujun.g 发表于 2015-9-4 11:33:31

Redis主从配置及通过Keepalived实现Redis自动切换高可用

Redis主从配置及通过Keepalived实现Redis自动切换高可用
[日期:2014-07-23]来源:Linux社区作者:fuquanjun[字体:大 中 小]
  一:环境介绍:
  Master: 192.168.1.4
  Slave: 192.168.1.5
  Virtural IP Address (VIP): 192.168.1.253
  二:设计思路:
  当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责Standby;
  当 Master 挂掉,Slave 正时, Slave接管服务,同时关闭主从复制功能;
  当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。
  然后依次循环。
  需要注意的是,这样做需要在Master与Slave上都开启本地化策略,否则在互相自动切换的过程中,未开启本地化的一方会将另一方的数据清空,造成数据完全丢失。
  三:安装配置前准备工作
  
1.在主服务器192.168.1.4上面做下面操作
  echo "192.168.1.4 test01" >> /etc/hosts
  echo "192.168.1.5 test" >> /etc/hosts
  2.在从服务器192.168.1.5上面做下面操作
  echo "192.168.1.4 test01" >> /etc/hosts
  echo "192.168.1.5 test" >> /etc/hosts
  
  
四:主服务器配置redis
  1.下载安装redis软件包
  wget http://download.redis.io/releases/redis-2.8.12.tar.gztar xf redis-2.8.12.tar.gz
  cd redis-2.8.12
  make && make install
  cd src/
  cp redis-server redis-cli redis-benchmark redis-check-aof redis-check-dump /usr/local/bin
  cd /usr/local/bin
  ls -ll
  然后将源码中的 redis.conf 复制到 /etc/redis.conf
  cp /root/redis-2.8.12/redis.conf /etc/redis.conf
  修改 /etc/redis.conf里面可以把daemonize no 修改为daemonize yes
  就可以默认在后台执行redis-server了。
  
  再制作一个 init.d 的启动脚本:
  vim /etc/init.d/redis-server
  #!/usr/bin/env bash
  #
  # redis start up the redis server daemon
  #
  # chkconfig: 345 99 99
  # description: redis service in /etc/init.d/redis \
  #            chkconfig --add redis or chkconfig --list redis \
  #            service redis startorservice redis stop
  # processname: redis-server
  # config: /etc/redis.conf
  
  PATH=/usr/local/bin:/sbin:/usr/bin:/bin
  
  REDISPORT=6379
  EXEC=/usr/local/bin/redis-server
  REDIS_CLI=/usr/local/bin/redis-cli
  
  PIDFILE=/var/run/redis.pid
  CONF="/etc/redis.conf"
  #make sure some dir exist
  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
  
chmod o+x /etc/init.d/redis-server
  chkconfig --add redis-server
  service redis-server start
  --------------------------------------分割线 --------------------------------------
  Ubuntu 14.04下Redis安装及简单测试 http://www.iyunv.com.com/Linux/2014-05/101544.htm
  Redis集群明细文档 http://www.iyunv.com.com/Linux/2013-09/90118.htm
  Ubuntu 12.10下安装Redis(图文详解)+ Jedis连接Redis http://www.iyunv.com.com/Linux/2013-06/85816.htm
  Redis系列-安装部署维护篇 http://www.iyunv.com.com/Linux/2012-12/75627.htm
  CentOS 6.3安装Redis http://www.iyunv.com.com/Linux/2012-12/75314.htm
  Redis配置文件redis.conf 详解 http://www.iyunv.com.com/Linux/2013-11/92524.htm
  CentOS 6.3下Haproxy+Keepalived+Apache配置笔记 http://www.iyunv.com.com/Linux/2013-06/85598.htm
  Haproxy + KeepAlived 实现WEB群集 on CentOS 6 http://www.iyunv.com.com/Linux/2012-03/55672.htm
  Keepalived+Haproxy配置高可用负载均衡 http://www.iyunv.com.com/Linux/2012-03/56748.htm
  Haproxy+Keepalived构建高可用负载均衡 http://www.iyunv.com.com/Linux/2012-03/55880.htm
  --------------------------------------分割线 --------------------------------------
  五:从发服务器配置redis
  从服务器,配置一样,只不过 修改/etc/redis.conf 中
  slaveof <masterip> <masterport>修改为
  slaveof 192.168.1.4 6379
  然后开启从服务器的redis服务。
  start redis-server start
  
  六:进行redis主从测试
  #主服务器
  redis-cli -p 6379 set hello world
  #从服务器
  redis-cli -p 6379 get hello
  "world"
  
  
#主服务器
  redis-cli -p 6379 set hello world2
  #从服务器
  redis-cli -p 6379 get hello
  "world2"
  redis-cli -p 6379 set hello world
  (error) READONLY You can't write against a read only slave.
  成功配置主从redis服务器,由于配置中有一条从服务器是只读的,所以从服务器没法设置数据,只可以读取数据。
页: [1]
查看完整版本: Redis主从配置及通过Keepalived实现Redis自动切换高可用