Redis主从集群搭建及容灾部署(哨兵sentinel)
redis容灾部署(哨兵Sentinel)
创新互联专业IDC数据服务器托管提供商,专业提供成都服务器托管,服务器租用,成都服务器托管,成都服务器托管,成都多线服务器托管等服务器托管服务。
1、sentinel介绍
Redis Sentinel
Sentinel(哨兵)是用于监控redis集群中Master状态的工具,其已经被集成在redis2.4+的版本中
2、Sentinel作用:
1):Master状态检测
2):如果Master异常,则会进行Master-Slave切换,将其中一个Slave作为Master,将之前的Master作为Slave
3):Master-Slave切换后,master_redis.conf、slave_redis.conf和sentinel.conf的内容都会发生改变,即master_redis.conf中会多一行slaveof的配置,sentinel.conf的监控目标会随之调换
3、Sentinel工作方式:
1):每个Sentinel以每秒钟一次的频率向它所知的Master,Slave以及其他 Sentinel 实例发送一个 PING 命令
2):如果一个实例(instance)距离最后一次有效回复 PING 命令的时间超过 down-after-milliseconds 选项所指定的值, 则这个实例会被 Sentinel 标记为主观下线。
3):如果一个Master被标记为主观下线,则正在监视这个Master的所有 Sentinel 要以每秒一次的频率确认Master的确进入了主观下线状态。
4):当有足够数量的 Sentinel(大于等于配置文件指定的值)在指定的时间范围内确认Master的确进入了主观下线状态, 则Master会被标记为客观下线
5):在一般情况下, 每个 Sentinel 会以每 10 秒一次的频率向它已知的所有Master,Slave发送 INFO 命令
6):当Master被 Sentinel 标记为客观下线时,Sentinel 向下线的 Master 的所有 Slave 发送 INFO 命令的频率会从 10 秒一次改为每秒一次
7):若没有足够数量的 Sentinel 同意 Master 已经下线, Master 的客观下线状态就会被移除。
若 Master 重新向 Sentinel 的 PING 命令返回有效回复, Master 的主观下线状态就会被移除。
4、主观下线和客观下线
主观下线:Subjectively Down,简称 SDOWN,指的是当前 Sentinel 实例对某个redis服务器做出的下线判断。
客观下线:Objectively Down, 简称 ODOWN,指的是多个 Sentinel 实例在对Master Server做出 SDOWN 判断,并且通过 SENTINEL is-master-down-by-addr 命令互相交流之后,得出的Master Server下线判断,然后开启failover.
通俗来讲就是:
redis的sentinel系统用来管理多个redis服务器,可以实现一个功能上实现HA的集群。该系统主要执行三个任务:
1、监控( Monitoring ): Redis Sentinel实时监控主服务器和从服务器运行状态。
2、提醒(notification): 当被监控的某个 Redis 服务器出现问题时, Redis Sentinel 可以向系统管理员发送通知, 也可以通过 API 向其他程序发送通知
3. 故障迁移(failover发):自动主从切换
4. 统一的配置管理:连接者询问sentinel取得主从的地址
5、Raft分布式算法
1. 主要用途:用于分布式系统,系统容错,以及选出领头羊
2. 作者:Diego Ongaro,毕业于哈佛
3. 目前用到这个算法的项目有:
a. CoreOS : 见下面
b. ectd : a distributed, consistent shared configuration
c. LogCabin : 分布式存储系统
d. redis sentinel : redis 的监控系统
Sentinel使用的Raft算法核心: 原则
1. 所有sentinel都有选举的领头羊的权利
2. 每个sentinel都会要求其他sentinel选举自己为领头羊(主要由发现redis客观下线的sentinel先发起选举)
3. 每个sentinel只有一次选举的机会
4. 采用先到先得的原则
5. 一旦加入到系统了,则不会自动清除(这一点很重要, why?)
6. 每个sentinel都有唯一的uid,不会因为重启而变更
7. 达到领头羊的条件是 N/2 + 1个sentinel选择了自己
8. 采用配置纪元,如果一次选举出现脑裂,则配置纪元会递增,进入下一次选举,所有sentinel都会处于统一配置纪元,以最新的为标准。
Raft算法核心: 可视图
Raft Visualization (算法演示)
Raft分布式算法的应用
coreos:云计算新星 Docker 正在以火箭般的速度发展,与它相关的生态圈也渐入佳境,CoreOS 就是其中之一。CoreOS 是一个全新的、面向数据中心设计的 Linux 操作系统,在2014年7月发布了首个稳定版本,目前已经完成了800万美元的A轮融资。
一个简单的主从结构加sentinel集群的架构图如下:
192.168.110.134(master redis,sentinel)----------->192.168.110.135(slave redis,sentinel)
| \ / |
| \ / |
| \ / |
| \ / |
| \ / |
| \ |
| \ |
| / \ |
| / \ |
| / \ |
| / \ |
192.168.110.132(sentinel)----------->192.168.110.133(slave redis,sentinel)
上图是一主一从节点,加上两个部署了sentinel的集群,sentinel集群之间会互相通信,沟通交流redis节点的状态,做出相应的判断并进行处理,这里的主观下线状态和客观下线状态是比较重要的状态,它们决定了是否进行故障转移。
可以 通过订阅指定的频道信息,当服务器出现故障得时候通知管理员 客户端可以将 Sentinel 看作是一个只提供了订阅功能的 Redis 服务
器,你不可以使用 PUBLISH 命令向这个服务器发送信息,但你可以用 SUBSCRIBE 命令或者 PSUBSCRIBE 命令, 通过订阅给定的频道来获取
相应的事件提醒。
6、配置哨兵
1)、配置一个哨兵Sentinel的模式:
把源码中的sentinel.conf配置文件拷贝到指定目录
[root@web1 ~]# mkdir /usr/local/sentinel
[root@web1 ~]# cd /usr/local/src/redis-3.2.6/
[root@web1 redis-3.2.6]# cp sentinel.conf /usr/local/sentinel/
[root@web1 redis-3.2.6]# cd /usr/local/sentinel/
[root@web1 sentinel]# vim sentinel.conf
[root@web1 sentinel]# grep -v "^#" sentinel.conf |grep -v "^$"
port 26379
dir /tmp
sentinel monitor mymaster 192.168.110.134 6379 1
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
例子:
#sentinel端口
port 26380
#工作路径
dir "/usr/local/redis-6380"
# 守护进程模式
daemonize yes
# 指明日志文件名
logfile "./sentinel.log"
#哨兵监控的master,主从配置一样,在进行主从切换时6379会变成当前的master端口,
sentinel monitor mymaster 192.168.137.40 6379 1
# master或slave多长时间(默认30秒)不能使用后标记为s_down状态。
sentinel down-after-milliseconds mymaster 5000
#若sentinel在该配置值内未能完成failover操作(即故障时master/slave自动切换),则认为本次failover失败。
sentinel failover-timeout mymaster 18000
#设置master和slaves验证密码
sentinel auth-pass mymaster 123456
#哨兵程序自动添加的部分
# Generated by CONFIG REWRITE
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 1
###指明了当前群集的从库的ip和端口,在主从切换时该值会改变
sentinel known-slave mymaster 192.168.137.40 6380
###除了当前的哨兵还有哪些监控的哨兵
sentinel known-sentinel mymaster 192.168.137.40 26379 7a88891a6147e202a53601ca16a3d438e9d55c9d
sentinel current-epoch 1
主要是这一项:
sentinel monitor mymaster 192.168.110.134 6379 2
my mymaster 后跟的是master的ip和端口
最后一个’2’代表我要启动只要有2个sentinel认为master下线,就认为该master客观下线,启动failover并选举产生新的master。
通常最后一个参数不能多于启动的sentinel实例数。
[root@web2 ~]# mkdir /usr/local/sentinel
[root@web2 ~]# cp /usr/local/src/redis-3.2.6/sentinel.conf /usr/local/sentinel/
[root@web2 ~]# cd /usr/local/sentinel/
[root@web2 sentinel]# vim sentinel.conf
[root@web2 sentinel]# grep -v "^#" sentinel.conf |grep -v "^$"
port 26379
dir /tmp
sentinel monitor mymaster 192.168.110.134 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
以后台启动模式启动两个sentinel(哨兵):
[root@web1 sentinel]# redis-sentinel /usr/local/sentinel/sentinel.conf &
[1] 18378
[root@web1 sentinel]# 18378:X 16 Dec 15:46:16.732 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 18378
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
18378:X 16 Dec 15:46:16.734 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
18378:X 16 Dec 15:46:16.734 # Sentinel ID is 28c5f96738e4fe3335088d037a85ef47dfe9ab8f
18378:X 16 Dec 15:46:16.734 # +monitor master mymaster 192.168.110.134 6379 quorum 1
由上图可以看到:
1、 哨兵已经启动,它的id为236f14b361fc5a0dc0621cf88823ed6e6252b2f3
2、 为master数据库添加了一个监控
3、 发现了2个slave(由此可以看出,哨兵无需配置slave,只需要指定master,哨兵会自动发现slave)
[root@web1 sentinel]# ps aux|grep redis
root 18357 0.1 1.0 135572 2440 ? Ssl 15:40 0:00 redis-server *:6379
root 18378 0.1 1.0 133528 2572 pts/1 Sl 15:46 0:00 redis-sentinel *:26379 [sentinel]
root 18400 0.0 0.3 103244 864 pts/1 S+ 15:51 0:00 grep redis
6、从Redis宕机测试
把其中一个从redis 的进程 kill掉:
[[root@web1 sentinel]# kill 18357
[root@web1 sentinel]# ps aux|grep redis
root 18378 0.1 1.0 133528 2572 pts/1 Sl 15:46 0:01 redis-sentinel *:26379 [sentinel]
root 18444 0.0 0.3 103244 864 pts/1 S+ 16:00 0:00 grep redis
30秒后哨兵的控制台输出:
[root@web1 sentinel]# 18378:X 16 Dec 16:01:16.452 # +sdown master mymaster 192.168.110.134 6379
18378:X 16 Dec 16:01:16.452 # +odown master mymaster 192.168.110.134 6379 #quorum 1/1
18378:X 16 Dec 16:01:16.452 # +new-epoch 9
18378:X 16 Dec 16:01:16.452 # +try-failover master mymaster 192.168.110.134 6379
18378:X 16 Dec 16:01:16.470 # +vote-for-leader 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 9
18378:X 16 Dec 16:01:16.470 # +elected-leader master mymaster 192.168.110.134 6379
18378:X 16 Dec 16:01:16.470 # +failover-state-select-slave master mymaster 192.168.110.134 6379
18378:X 16 Dec 16:01:16.546 # +selected-slave slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379
18378:X 16 Dec 16:01:16.546 * +failover-state-send-slaveof-noone slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379
18378:X 16 Dec 16:01:16.605 * +failover-state-wait-promotion slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379
18378:X 16 Dec 16:01:16.699 # +promoted-slave slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379
18378:X 16 Dec 16:01:16.699 # +failover-state-reconf-slaves master mymaster 192.168.110.134 6379
18378:X 16 Dec 16:01:16.759 # +failover-end master mymaster 192.168.110.134 6379
18378:X 16 Dec 16:01:16.759 # +switch-master mymaster 192.168.110.134 6379 192.168.110.135 6379
18378:X 16 Dec 16:01:16.760 * +slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
18378:X 16 Dec 16:01:46.858 # +sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
30秒后哨兵的控制台输出:
18378:X 16 Dec 16:01:16.759 # +switch-master mymaster 192.168.110.134 6379 192.168.110.135 6379
18378:X 16 Dec 16:01:16.760 * +slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
18378:X 16 Dec 16:01:46.858 # +sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
说明已经监控到刚才我人kill掉的master宕机了
可以看出,slave重新已经转换成立主库 -sdown:说明是恢复服务。
[root@web2 sentinel]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379>
把192.168.110.134恢复:
[root@web1 sentinel]# redis-server /usr/local/redis/etc/redis.conf
[root@web1 sentinel]# 18378:X 16 Dec 16:06:59.825 # -sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
18378:X 16 Dec 16:07:09.806 * +convert-to-slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
[root@web1 sentinel]#
[root@web1 sentinel]#
[root@web1 sentinel]# ps aux|grep redis
root 18378 0.2 1.0 133528 2636 pts/1 Sl 15:46 0:02 redis-sentinel *:26379 [sentinel]
root 18487 0.1 1.0 135572 2564 ? Ssl 16:06 0:00 redis-server *:6379
root 18491 0.0 0.3 103244 864 pts/1 S+ 16:08 0:00 grep redis
192.168.110.134是作为从库加入到复制中的(主库不会切换过来):
测试下:
[root@web2 sentinel]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.110.134,port=6379,state=online,offset=2835,lag=1
master_repl_offset:2835
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:2834
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.110.134,port=6379,state=online,offset=9687,lag=0
master_repl_offset:9832
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:9831
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.110.134,port=6379,state=online,offset=36450,lag=0
master_repl_offset:36595
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:36594
127.0.0.1:6379> set name2 lisi
OK
127.0.0.1:6379> set age2 22
OK
127.0.0.1:6379> get name2
"lisi"
127.0.0.1:6379> get age2
"22"
127.0.0.1:6379>
[root@web1 sentinel]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.110.135
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:73863
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379> get name2
"lisi"
127.0.0.1:6379> get set2
(nil)
127.0.0.1:6379> get age2
"22"
127.0.0.1:6379>
效果不错:
把主库切换回192.168.110.134
[root@web2 sentinel]# ps aux|grep redis
root 7063 0.1 1.2 135572 3116 ? Ssl 14:35 0:13 redis-server *:6379
root 7649 0.0 0.3 103244 864 pts/1 S+ 16:48 0:00 grep redis
[root@web2 sentinel]# kill 7063
[root@web1 sentinel]# 18522:X 16 Dec 16:49:05.725 # +sdown master mymaster 192.168.110.135 6379
18522:X 16 Dec 16:49:05.725 # +odown master mymaster 192.168.110.135 6379 #quorum 1/1
18522:X 16 Dec 16:49:05.725 # +new-epoch 10
18522:X 16 Dec 16:49:05.725 # +try-failover master mymaster 192.168.110.135 6379
18522:X 16 Dec 16:49:05.736 # +vote-for-leader 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 10
18522:X 16 Dec 16:49:05.736 # +elected-leader master mymaster 192.168.110.135 6379
18522:X 16 Dec 16:49:05.736 # +failover-state-select-slave master mymaster 192.168.110.135 6379
18522:X 16 Dec 16:49:05.791 # +selected-slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
18522:X 16 Dec 16:49:05.791 * +failover-state-send-slaveof-noone slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
18522:X 16 Dec 16:49:05.875 * +failover-state-wait-promotion slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
18522:X 16 Dec 16:49:06.233 # +promoted-slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
18522:X 16 Dec 16:49:06.233 # +failover-state-reconf-slaves master mymaster 192.168.110.135 6379
18522:X 16 Dec 16:49:06.331 # +failover-end master mymaster 192.168.110.135 6379
18522:X 16 Dec 16:49:06.331 # +switch-master mymaster 192.168.110.135 6379 192.168.110.134 6379
18522:X 16 Dec 16:49:06.331 * +slave slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379
18522:X 16 Dec 16:49:36.394 # +sdown slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379
切换成功
[root@web1 sentinel]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379>
恢复192.168.110.135的redis:
[root@web2 sentinel]# redis-server /etc/redis.conf
127.0.0.1:6379> 18522:X 16 Dec 16:52:32.351 # -sdown slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379
18522:X 16 Dec 16:52:42.305 * +convert-to-slave slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379
127.0.0.1:6379>
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.110.135,port=6379,state=online,offset=13653,lag=1
master_repl_offset:13667
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:13666
127.0.0.1:6379>
1
[root@web2 sentinel]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.110.134
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:16502
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379>
2)、配置两个哨兵Sentinel的模式:
[root@web1 sentinel]# grep -v "^$" /usr/local/sentinel/sentinel.conf |grep -v "^#"
port 26379
dir "/tmp"
sentinel myid 28c5f96738e4fe3335088d037a85ef47dfe9ab8f
sentinel monitor mymaster 192.168.110.134 6379 2
sentinel config-epoch mymaster 10
sentinel leader-epoch mymaster 10
sentinel known-slave mymaster 192.168.110.135 6379
sentinel known-sentinel mymaster 192.168.110.135 26379 16286f52de2675345d1b11bc8946adc5d3f0c360
sentinel current-epoch 10
[root@web2 sentinel]# grep -v "^$" /usr/local/sentinel/sentinel.conf |grep -v "^#"
port 26379
dir "/tmp"
sentinel myid 16286f52de2675345d1b11bc8946adc5d3f0c360
sentinel monitor mymaster 192.168.110.134 6379 2
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 6
sentinel known-slave mymaster 192.168.110.135 6379
sentinel known-sentinel mymaster 192.168.110.134 26379 28c5f96738e4fe3335088d037a85ef47dfe9ab8f
sentinel current-epoch 10
主要是这一项:
sentinel monitor mymaster 192.168.110.134 6379 2
最后一个’2’代表我要启动只要有2个sentinel认为master下线,就认为该master客观下线,启动failover并选举产生新的master。
通常最后一个参数不能多于启动的sentinel实例数。
[root@web1 sentinel]# redis-sentinel /usr/local/sentinel/sentinel.conf &
[1] 18747
[root@web1 sentinel]# 18747:X 16 Dec 17:04:30.597 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 18747
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
18747:X 16 Dec 17:04:30.599 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
18747:X 16 Dec 17:04:30.599 # Sentinel ID is 28c5f96738e4fe3335088d037a85ef47dfe9ab8f
18747:X 16 Dec 17:04:30.599 # +monitor master mymaster 192.168.110.134 6379 quorum 2
18747:X 16 Dec 17:05:00.659 # +sdown sentinel 16286f52de2675345d1b11bc8946adc5d3f0c360 192.168.110.135 26379 @ mymaster 192.168.110.134 6379
[root@web2 sentinel]# redis-sentinel /usr/local/sentinel/sentinel.conf &
[1] 7726
You have new mail in /var/spool/mail/root
[root@web2 sentinel]# 7726:X 16 Dec 17:04:33.835 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 7726
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
7726:X 16 Dec 17:04:33.836 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7726:X 16 Dec 17:04:33.836 # Sentinel ID is 16286f52de2675345d1b11bc8946adc5d3f0c360
7726:X 16 Dec 17:04:33.836 # +monitor master mymaster 192.168.110.134 6379 quorum 2
7726:X 16 Dec 17:04:34.668 # +new-epoch 10
7726:X 16 Dec 17:05:03.836 # +sdown sentinel 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 192.168.110.134 26379 @ mymaster 192.168.110.134 6379
上面是有问题的:原因是bind 默认是仅仅监控本机和 192.168.1.1这两个IP地址
# bind 127.0.0.1 192.168.1.1
添加: bind 0.0.0.0
就没问题了
配置文件:
[root@web1 ~]# grep -v "^$" /usr/local/sentinel/sentinel.conf |grep -v "^#"
bind 0.0.0.0
port 26379
dir "/tmp"
sentinel myid 28c5f96738e4fe3335088d037a85ef47dfe9ab8f
sentinel monitor mymaster 192.168.110.135 6379 2
sentinel config-epoch mymaster 14
sentinel leader-epoch mymaster 14
sentinel known-slave mymaster 192.168.110.134 6379
sentinel known-sentinel mymaster 192.168.110.135 26379 16286f52de2675345d1b11bc8946adc5d3f0c360
sentinel current-epoch 14
主要是下面两项:
bind 0.0.0.0
sentinel monitor mymaster 192.168.110.135 6379 2
如果设置了密码则添加下面一项:
# sentinel auth-pass
sentinel auth-pass def_master 012_345^678-90
[root@web1 ~]# redis-sentinel /usr/local/sentinel/sentinel.conf &
[1] 1963
[root@web1 ~]# 1963:X 19 Dec 11:55:29.670 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 1963
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1963:X 19 Dec 11:55:29.671 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1963:X 19 Dec 11:55:29.671 # Sentinel ID is 28c5f96738e4fe3335088d037a85ef47dfe9ab8f
1963:X 19 Dec 11:55:29.672 # +monitor master mymaster 192.168.110.134 6379 quorum 2
上面表示启动没问题的:
kill掉一个从redis,观察sentinel控制台输出信息:
[root@web1 ~]# kill 1912
[root@web1 ~]# ps aux|grep redis
root 1963 0.2 1.0 133528 2588 pts/0 Sl 11:55 0:00 redis-sentinel 0.0.0.0:26379 [sentinel]
root 1992 0.0 0.3 103244 864 pts/0 S+ 11:58 0:00 grep redis
[root@web1 ~]# 1963:X 19 Dec 11:59:06.906 # +sdown master mymaster 192.168.110.134 6379
1963:X 19 Dec 11:59:06.963 # +odown master mymaster 192.168.110.134 6379 #quorum 2/2
1963:X 19 Dec 11:59:06.963 # +new-epoch 14
1963:X 19 Dec 11:59:06.963 # +try-failover master mymaster 192.168.110.134 6379
1963:X 19 Dec 11:59:06.972 # +vote-for-leader 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 14
1963:X 19 Dec 11:59:06.992 # 16286f52de2675345d1b11bc8946adc5d3f0c360 voted for 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 14
1963:X 19 Dec 11:59:07.035 # +elected-leader master mymaster 192.168.110.134 6379
1963:X 19 Dec 11:59:07.035 # +failover-state-select-slave master mymaster 192.168.110.134 6379
1963:X 19 Dec 11:59:07.136 # +selected-slave slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379
1963:X 19 Dec 11:59:07.136 * +failover-state-send-slaveof-noone slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379
1963:X 19 Dec 11:59:07.199 * +failover-state-wait-promotion slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379
1963:X 19 Dec 11:59:08.171 # +promoted-slave slave 192.168.110.135:6379 192.168.110.135 6379 @ mymaster 192.168.110.134 6379
1963:X 19 Dec 11:59:08.171 # +failover-state-reconf-slaves master mymaster 192.168.110.134 6379
1963:X 19 Dec 11:59:08.260 # +failover-end master mymaster 192.168.110.134 6379
1963:X 19 Dec 11:59:08.260 # +switch-master mymaster 192.168.110.134 6379 192.168.110.135 6379
1963:X 19 Dec 11:59:08.260 * +slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
1963:X 19 Dec 11:59:38.290 # +sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
另一台:
[root@web2 ~]# redis-sentinel /usr/local/sentinel/sentinel.conf &
[1] 1809
[root@web2 ~]# 1809:X 19 Dec 11:55:35.698 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 1809
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1809:X 19 Dec 11:55:35.700 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1809:X 19 Dec 11:55:35.700 # Sentinel ID is 16286f52de2675345d1b11bc8946adc5d3f0c360
1809:X 19 Dec 11:55:35.700 # +monitor master mymaster 192.168.110.134 6379 quorum 2
1809:X 19 Dec 11:59:06.895 # +sdown master mymaster 192.168.110.134 6379
1809:X 19 Dec 11:59:06.999 # +new-epoch 14
1809:X 19 Dec 11:59:07.001 # +vote-for-leader 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 14
1809:X 19 Dec 11:59:08.020 # +odown master mymaster 192.168.110.134 6379 #quorum 2/2
1809:X 19 Dec 11:59:08.020 # Next failover delay: I will not start a failover before Mon Dec 19 12:05:07 2016
1809:X 19 Dec 11:59:08.284 # +config-update-from sentinel 28c5f96738e4fe3335088d037a85ef47dfe9ab8f 192.168.110.134 26379 @ mymaster 192.168.110.134 6379
1809:X 19 Dec 11:59:08.284 # +switch-master mymaster 192.168.110.134 6379 192.168.110.135 6379
1809:X 19 Dec 11:59:08.284 * +slave slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
1809:X 19 Dec 11:59:38.344 # +sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
[root@web2 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379>
恢复192.168.110.134:
[root@web1 ~]# redis-server /usr/local/redis/etc/redis.conf
[root@web1 ~]# 1963:X 19 Dec 12:14:24.393 # -sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
[root@web1 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.110.135
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:38769
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379>
135上的显示:
repl_backlog_histlen:0
127.0.0.1:6379> 1809:X 19 Dec 12:14:24.105 # -sdown slave 192.168.110.134:6379 192.168.110.134 6379 @ mymaster 192.168.110.135 6379
127.0.0.1:6379>
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.110.134,port=6379,state=online,offset=2404,lag=1
master_repl_offset:2404
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:2403
也是仅仅作为从。
2)、配置三个哨兵Sentinel的模式:
[root@web2 ~]# scp redis-3.2.6.tar.gz 192.168.110.133:/root
The authenticity of host '192.168.110.133 (192.168.110.133)' can't be established.
RSA key fingerprint is 60:6c:0d:f4:3b:f8:42:b1:c9:7e:ab:c4:bc:83:d1:09.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.110.133' (RSA) to the list of known hosts.
root@192.168.110.133's password:
redis-3.2.6.tar.gz 100% 1509KB 1.5MB/s 00:00
[root@node2 ~]# iptables -I INPUT 6 -m state --state NEW -p tcp --dport 6379 -j ACCEPT
[root@node2 ~]# iptables -I INPUT 7 -m state --state NEW -p tcp --dport 26379 -j ACCEPT
[root@node2 ~]# tar -zxvf redis-3.2.6.tar.gz -C /usr/local/src/
[root@node2 ~]# cd /usr/local/src/redis-3.2.6/
[root@node2 redis-3.2.6]# make
[root@node2 redis-3.2.6]# make install
文章标题:Redis主从集群搭建及容灾部署(哨兵sentinel)
URL地址:http://myzitong.com/article/jsoigo.html