设为首页 收藏本站
查看: 860|回复: 0

[经验分享] redis 高可用最新方案(实践)

[复制链接]

尚未签到

发表于 2018-11-5 11:41:36 | 显示全部楼层 |阅读模式
Setup Redis Failover with Redis Sentinel

  Recently I’ve been playing with redis, to study as an>  http://oldblog.antirez.com/post/redis-persistence-demystified.html

  One of the other interesting features that I really appreciate of this solution, is the possibility to work with different data structures like lists, hashes, sets and sorted sets. So you’ve more flexibility to work to store different values on cache on the same key and have a support for native data types provided from the client library of your programming language used. You can take a look there to check the different data structures used in redis:
  http://redis.io/topics/data-types
  After this small introduction of some tips that why I choose redis, now I’ll talk about the failover solution. Redis supports master-slave asynchronous replication and sentinel will provides the failover, which comes from redis version 2.6, but from the project documentation they recommend the version shipped with redis 2.8, seems like they did very important enhances with the last version. Sentinel is a distributed system, which the different processes are communicated between them over messages and using the different protocols to elect a new master and inform the address of the current master of the cluster to the client.
  We’ll run sentinel in our systems as a separate daemon listening in a different port, which will communicate with the other sentinels setup on the cluster to alert in event of a node failure and choose a new master. Sentinel will change our configuration files of our servers just to attach a recovered node on the cluster (setup as slave) or promote a slave as a master. The basic process to choose a new master basically is this:
  1.- One sentinel node detects a server failure on the cluster in the number of milliseconds setup in the directive “down-after-milliseconds“. At this moment this sentinel node mark this instance as subjectively down (SDOWN).
  2.- When the enough number of sentinels agreed is reached about this master failure , then this node is marked as objectively down (ODOWN), then the failover trigger is processed. The number of sentinels it’s setup for master.
  3.- After the trigger failover, it’s not still enough to perform a failover, since it’s subject to a quorum process and at least a majority of Sentinels must authorized the Sentinel to failover.
  Basically we’ll need a minimum of three nodes in our cluster to setup our redis failover solution. In my case I choose to use two redis servers (master & slave) both running sentinel, and one third node running just sentinel for the quorum process. For more information about the failover process and sentinel you can check the official documentation:
  http://redis.io/topics/sentinel
  After this basic tips about how it works redis & sentinel, we can begin with the setup. For this environment I used a total of three servers running Ubuntu 14.04. All that I need to do is install redis-server from repositories. Note if you’re using other GNU/Linux distribution or an older Ubuntu version you’ll need to compile and install by hand.
  – Setup for redis sentinels (nodes 1,2,3) /etc/redis/sentinel.conf:
  1
  2
  3
  4
  5
  6
  7
  8
  9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  # port
  # The port that this sentinel instance will run on
  port 26379
  daemonize yes
  pidfile /var/run/redis/redis-sentinel.pid
  loglevel notice
  logfile /var/log/redis/redis-sentinel.log
  # Master setup
  # sentinel parallel-syncs  
  # Minimum of two sentinels to declare an ODOWN
  sentinel monitor mymaster 172.16.23.33 6379 2
  # sentinel down-after-milliseconds  
  sentinel down-after-milliseconds mymaster 5000
  # sentinel failover-timeout  
  sentinel failover-timeout mymaster 900000
  # sentinel parallel-syncs  
  sentinel parallel-syncs mymaster 1
  # Slave setup
  sentinel monitor resque 172.16.23.34 6379 2
  sentinel down-after-milliseconds resque 5000
  sentinel failover-timeout resque 900000
  sentinel parallel-syncs resque 4
  – Create init scripts for sentinels (nodes 1,2,3) /etc/init.d/redis-sentinel:
  1
  2
  3
  4
  5
  6
  7
  8
  9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  #! /bin/sh
  ### BEGIN INIT INFO
  # Provides: redis-sentinel
  # Required-Start: $syslog $remote_fs
  # Required-Stop: $syslog $remote_fs
  # Should-Start: $local_fs
  # Should-Stop: $local_fs
  # Default-Start: 2 3 4 5
  # Default-Stop: 0 1 6
  # Short-Description: redis-sentinel - Persistent key-value db
  # Description: redis-sentinel - Persistent key-value db
  ### END INIT INFO
  PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  DAEMON=/usr/bin/redis-sentinel
  DAEMON_ARGS=/etc/redis/sentinel.conf
  NAME=redis-sentinel
  DESC=redis-sentinel
  RUNDIR=/var/run/redis
  PIDFILE=$RUNDIR/redis-sentinel.pid
  test-x $DAEMON || exit0
  if[ -r /etc/default/$NAME ]
  then
  . /etc/default/$NAME
  fi
  . /lib/lsb/init-functions
  set-e
  case"$1"in
  start)
  echo-n "Starting $DESC: "
  mkdir-p $RUNDIR
  touch$PIDFILE
  chownredis:redis $RUNDIR $PIDFILE
  chmod755 $RUNDIR
  if[ -n "$ULIMIT"]
  then
  ulimit-n $ULIMIT
  fi
  ifstart-stop-daemon --start --quiet --umask007 --pidfile $PIDFILE --chuid redis:redis --exec$DAEMON -- $DAEMON_ARGS
  then
  echo"$NAME."
  else
  echo"failed"
  fi
  ;;
  stop)
  echo-n "Stopping $DESC: "
  ifstart-stop-daemon --stop --retry forever/TERM/1--quiet --oknodo --pidfile $PIDFILE --exec$DAEMON
  then
  echo"$NAME."
  else
  echo"failed"
  fi
  rm-f $PIDFILE
  sleep1
  ;;
  restart|force-reload)
  ${0} stop
  ${0} start
  ;;
  status)
  echo-n "$DESC is "
  ifstart-stop-daemon --stop --quiet --signal 0 --name ${NAME} --pidfile ${PIDFILE}
  then
  echo"running"
  else
  echo"not running"
  exit1
  fi
  ;;
  *)
  echo"Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|status}">&2
  exit1
  ;;
  esac
  exit0
  – Give execution permission on the script:
  1
  # chmod +x /etc/init.d/redis-sentinel
  – Start the script automatically at boot time:
  1
  # update-rc.d redis-sentinel defaults
  – Change owner & group for /etc/redis/ to allow sentinel change the configuration files:
  1
  # chown -R redis.redis /etc/redis/
  – On node 3 I’ll not use redis-server, so I can remove the init script:
  1
  # update-rc.d redis-server remove
  – Edit the configuration of redis server on nodes 1,2 (/etc/redis/redis.conf), with the proper setup for your project. The unique requirement to work with seninel it’s just to setup the proper ip address on bind directive. All the directives are commented on the file and are very clear, so take your time to adapt redis to your project.
  – Connecting to our redis cluster:
  Now we’ve our redis cluster ready to store our data. In my case I work with Perl and currently I’m using this library: http://search.cpan.org/dist/Redis/lib/Redis.pm which you can install using the cpan tool. Note the version coming from ubuntu repositories (libredis-perl) it’s quite old and doesn’t implement the sentinel interface, so it’s better to install the module from cpan.
  So to connect to our cluster as documented on the client library I used the next chain:
  1
  2
  my$cache= Redis->new(sentinels=> [ "redis1:26379", "redis2:26379", "node3:26379"],
  service=> 'mymaster');
  So basically the library will tries to connect with the different sentinel servers and get the address of the current master redis servers which will get and store the data in our system.
  Another solution instead to connect from our scripts to the different sentinel servers, is use haproxy as backend and as a single server connection for our clients. HaProxy should check on the different redis servers the string “role:master” and redirect all the requests to this server.
  Take a look on the library documentation for your programming language used in your project. The different clients currently supported by redis are listed here:
  http://redis.io/clients
  – Sources:
  http://redis.io/documentation


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-631070-1-1.html 上篇帖子: redis 基准测试 下篇帖子: Redis配置文件各项参数说明
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表