- 实践意义 :http://blog.csdn.net/luyee2010/article/details/9385155
- 特点
- 可以实现监控,通知
- tcp端口26379
- 不需要在配置文件中标master,因为slave都是自动发现的,不需要注明那个是slave。
- 如果sentinel主程序失效,那么将无法failover
- 安装须知
- 最好三台以上
- 快速安装
- redis_sentinel 集成在redis 2.8 3.0中了。
- 启动命令(二者都可以)
- redis-sentinel /path/to/sentinel.conf
- redis-server /path/to/sentinel.conf --sentinel
- 最小配置文件
-
sentinel monitor mymaster 127.0.0.1 6379 2
#mymaster 就是主的名字;127是ip地址;6379端口;2是法定人数(quorum) sentinel down-after-milliseconds mymaster 60000 sentinel failover-timeout mymaster 180000 sentinel parallel-syncs mymaster 1 sentinel monitor resque 192.168.1.3 6380 4 sentinel down-after-milliseconds resque 10000 sentinel failover-timeout resque 180000 sentinel parallel-syncs resque 5
#优化设置
min-slaves-to-write 1 min-slaves-max-lag 10
优化解释
-
如果网络容易有问题,主可能被新的slave代替,而老的master 仍然是master,client写入到老master时候,这个写会丢失。但是如果两个slave 掉电则master不会被写入。这个就是利弊(纤细的看官方文档)
-
-
Setup Redis Failover with Redis Sentinel
Recently I’ve been playing with redis, to study as an alternative for memcached for one project. One of my main requirements was to implement a good failover solution for a key-value memory database, actually you’ve many ways to do it from code and memcached (doing get/set checking the availability of the servers) or better solution use the repcached patch for memcached, the first it’s not a clean solution at all and I was not very convinced with repcached. After get more involved in all the features that redis can offer, one of the interesting features is the persistence on disk. Redis stores all in memory key/value pairs on disk, so in case of failure after recovery you’ll get the last data stored from the last snapshot in memory. Note that the store to disk is not an operation effected on the fly, so you can lose some data, although redis offers different kind of setup to store on disk it’s important that you understand how it works. Anyway remember you’re working with a memory key-value cache solution, so it’s not the solution to make persistent critical data. A good read that I recommend to understand how persistence works:
http://oldblog.antirez.com/post/redis-persistence-demystified.htmlOne 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:
12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# port <sentinel-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 <master-name> <numslaves>
# Minimum of two sentinels to declare an ODOWN
sentinel monitor mymaster 172.16.23.33 6379 2
# sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel down-after-milliseconds mymaster 5000
# sentinel failover-timeout <master-name> <milliseconds>
sentinel failover-timeout mymaster 900000
# sentinel parallel-syncs <master-name> <numslaves>
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:
12
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 ||
exit
0
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
chown
redis:redis $RUNDIR $PIDFILE
chmod
755 $RUNDIR
if
[ -n
"$ULIMIT"
]
then
ulimit
-n $ULIMIT
fi
if
start-stop-daemon --start --quiet --
umask
007 --pidfile $PIDFILE --chuid redis:redis --
exec
$DAEMON -- $DAEMON_ARGS
then
echo
"$NAME."
else
echo
"failed"
fi
;;
stop)
echo
-n
"Stopping $DESC: "
if
start-stop-daemon --stop --retry forever
/TERM/1
--quiet --oknodo --pidfile $PIDFILE --
exec
$DAEMON
then
echo
"$NAME."
else
echo
"failed"
fi
rm
-f $PIDFILE
sleep
1
;;
restart|force-reload)
${0} stop
${0} start
;;
status)
echo
-n
"$DESC is "
if
start-stop-daemon --stop --quiet --signal 0 --name ${NAME} --pidfile ${PIDFILE}
then
echo
"running"
else
echo
"not running"
exit
1
fi
;;
*)
echo
"Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|status}"
>&2
exit
1
;;
esac
exit
0
– 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:
12
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:
– Sources:
http://redis.io/documentation
- 启动命令(二者都可以)
- redis_sentinel 集成在redis 2.8 3.0中了。
redis 高可用最新方案(实践)
时间: 2024-09-30 07:03:34
redis 高可用最新方案(实践)的相关文章
利用redis-sentinel+consul实现redis高可用
在前文<利用redis-sentinel+keepalived实现redis高可用>详细描述了利用redis-sentinel+keepalived实现redis高可用的方案.本文中redis-sentinel的应用场景也是一样的,也是提供Redis单实例服务,当某Redis(master)服务意外停掉或该服务所在的主机发生宕机故障或网络故障时,另一台Redis服务会由slave自动成为master,提供Redis读写服务.redis-sentinel的配置可以参考前文,本文略去,只讨论con
Redis高可用详解:持久化技术及方案选择
Redis高可用详解:持久化技术及方案选择 Java架构师那些事 关注 0.3 2018.08.23 22:55 字数 9774 阅读 542评论 0喜欢 9 前言 本文将先说明上述几种技术分别解决了Redis高可用的什么问题,然后详细介绍Redis的持久化技术,主要是RDB和AOF两种持久化方案.在介绍RDB和AOF方案时,不仅介绍其作用及操作方法,同时还会介绍持久化实现的一些原理细节及需要注意的问题.最后,介绍在实际使用中持久化方案的选择以及经常遇到的问题等内容. 一.Redis高可用概述
Redis 高可用实践
Sentinel 简介 如上图所示,Redis 高可用是通过Sentinel来实现的,是Redis官方推荐的高可用性(HA)解决方案,Sentinel英文含义是哨兵,放哨的,可以理解为它是Redis集群的监控者,监控着所有的master和slave机器的健康状况.首先我们来看下它提供了哪些功能. 1.监控(Monitoring):监控所有主从机的健康状态,当主从机连接出现问题时,会自动变更有问题机器的状态.2.通知(Notification):一旦集群的节点有问题,Sentinel可以通过API
搭建一个redis高可用系统
一.单个实例 当系统中只有一台redis运行时,一旦该redis挂了,会导致整个系统无法运行. 单个实例 二.备份 由于单台redis出现单点故障,就会导致整个系统不可用,所以想到的办法自然就是备份(一般工业界认为比较安全的备份数应该是3份).当一台redis出现问题了,另一台redis可以继续提供服务. 备份 三.自动故障转移 虽然上面redis做了备份,看上去很完美.但由于redis目前只支持主从复制备份(不支持主主复制),当主redis挂了,从redis只能提供读服务,无法提供写服务.所以
利用lvs keepalived配置redis高可用及负载均衡
需求 我们用ES做日志服务,架构是 上游数据来源=>redis=>logstash=>ES redis目前还是单点, 没有做高可用, 现在数据量越来越多, 如果下游消费不出问题还好, redis里面数据来了就走,但是下游一旦出问题, 分给redis的内存半小时就撑满了. 看到redis3.0 beta版本已经提供了集群功能, 但是需要client以集群模式接入, 我们这么多上游用户, 不太可能统一要求他们改造. 公司也有硬件的LB, 同事在E公司的时候就是用的硬件LB. 但接入还要申请,
【独家】终生受用的Redis高可用技术解决方案大全
最近很多朋友向我咨询关于高可用的方案的优缺点以及如何选择合适的方案线上使用,刚好最近在给宜人贷,光大银行做企业内训的时候也详细讲过,这里我再整理发出来,供大家参考,如有不妥之处,欢迎批评指正,也欢迎推荐更好的技术方案.不废话了,来看看方案吧- 总纲 Redis常见的几种主要使用方式: Redis 单副本 Redis 多副本(主从) Redis Sentinel(哨兵) Redis Cluster Redis 自研 Redis各种使用方式的优缺点: 1.Redis单副本 Redis 单副本,采用单
Keepalived+Redis高可用部署(第二版)
更新 20150625 脚本由5个减少为4个,sh脚本指令做了精简. 修改了另外3个脚本,在日志里增加了日期显示. 新增redis数据类型,持久化,主从同步简介. 新增hiredis简介. 新增c语言客户端测试. Redis简介及安装 Redis是一个开源,先进的key-value存储,用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使用磁盘仅用于持久性. 相比许多键值数据存储,Redis拥有一套较为丰富的数据
利用redis-sentinel+keepalived实现redis高可用
目标.需求: 为上层应用提供高可靠.低延迟.低(无限接近0)数据损失的Redis缓存服务 方案概述: 采用同一网络内的三台主机(可以是物理主机.虚拟机或docker容器),要求三台主机之间都能相互访问,每一台主机上都安装redis-server.redis-sentinel和keepalived. redis-server负责提供Redis缓存服务,三台主机间的关系是master-slave-slave redis-sentinel负责提供Redis高可用,三台主机间的关系与redis-serv
Redis高可用部署及监控
Redis高可用部署及监控 目录 一.Redis Sentinel简介 二.硬件需求 三.拓扑结构 1.单M-S结构 2.双M-S结构 3.优劣对比 四.配置部署 1.Redis配置 2.Redis Sentinel配置 3.启动服务 4.故障模拟检测 五.备份恢复 1.备份策略 2.灾难恢复 六.运维监控 1.安全监控 2.性能监控 一. Redis Sentinel简介 Redis Sentinel是redis自带的集