Fork me on GitHub

Redis Sentinel 高可用服务架构搭建

阅读目录:

  1. 关于 Redis 的概念
  2. 关于 Redis Sentinel 的概念
  3. 搭建 Redis Server(master)
  4. 搭建 Redis Server(slave)
  5. 搭建 Redis Sentinel
  6. Redis Sentinel 故障转移测试

前几天,看到了一篇文章:高可用 Redis 服务架构分析与搭建,思路讲的非常好,但是没有搭建的过程,这篇文章就记录下 Redis Sentinel 高可用服务架构搭建的过程。

搭建方案,就按照作者的第四种方案:


1. 关于 Redis 的概念

Redis 是完全开源免费的,遵守 BSD 协议,是一个高性能的 key-value 数据库。

Redis 与其他 key-value 缓存产品有以下三个特点:

  • Redis 支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
  • Redis 不仅仅支持简单的 key-value 类型的数据,同时还提供 list,set,zset,hash 等数据结构的存储。
  • Redis 支持数据的备份,即 master-slave 模式的数据备份。

Redis 的优势:

  • 性能极高 – Redis 能读的速度是 110000 次/s,写的速度是 81000 次/s。
  • 丰富的数据类型 – Redis 支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。
  • 原子 – Redis 的所有操作都是原子性的,意思就是要么成功执行要么失败完全不执行。单个操作是原子性的。多个操作也支持事务,即原子性,通过 MULTI 和 EXEC 指令包起来。
  • 丰富的特性 – Redis 还支持 publish/subscribe, 通知, key 过期等等特性。

以上内容摘自:http://www.runoob.com/redis/redis-intro.html

2. 关于 Redis Sentinel 的概念

Redis Sentinel(译为“哨兵”)是 Redis 官方推荐的高可用性(HA)解决方案,当用 Redis 做 Master-slave 的高可用方案时,假如 master 宕机了,Redis 本身(包括它的很多客户端)都没有实现自动进行主备切换,而 Redis-sentinel 本身也是一个独立运行的进程,它能监控多个 master-slave 集群,发现 master 宕机后能进行自动切换。

Redis Sentinel 系统用于管理多个 Redis 服务器(instance),该系统执行以下三个任务:

  • 监控(Monitoring):Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
  • 提醒(Notification):当被监控的某个 Redis 服务器出现问题时,Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
  • 自动故障迁移(Automatic failover):当一个主服务器不能正常工作时,Sentinel 会开始一次自动故障迁移操作,它会将失效主服务器的其中一个从服务器升级为新的主服务器,并让失效主服务器的其他从服务器改为复制新的主服务器;当客户端试图连接失效的主服务器时,集群也会向客户端返回新主服务器的地址,使得集群可以使用新主服务器代替失效服务器。

Redis Sentinel 是一个分布式系统,你可以在一个架构中运行多个 Sentinel 进程(progress),这些进程使用流言协议(gossip protocols)来接收关于主服务器是否下线的信息,并使用投票协议(agreement protocols)来决定是否执行自动故障迁移,以及选择哪个从服务器作为新的主服务器。

一个 Sentinel 进程可以与其他多个 Sentinel 进程进行连接,每个 Sentinel 进程之间可以互相检查对方的可用性,并进行信息交换。

以上内容摘自:http://redisdoc.com/topic/sentinel.html

3. 搭建 Redis Server(master)

我们就按照上面的架构图进行搭建,大概三台服务器(防火墙关闭):

10.9.10.154 master   redis-server redis-sentinel
10.9.10.152 slave    redis-server redis-sentinel
10.9.10.215 redis-sentinel

先从 redis.io/releases,下载最新版本的 Redis(需要进行编译)。

$ wget http://download.redis.io/releases/redis-4.0.8.tar.gz
$ tar xzf redis-4.0.8.tar.gz
$ cd redis-4.0.8
$ ls
00-RELEASENOTES  COPYING    Makefile   redis.conf       runtest-sentinel  tests
BUGS             INSTALL    README.md  runtest          sentinel.conf     utils
CONTRIBUTING     MANIFESTO  deps       runtest-cluster  src
$ cd src
$ make install

    CC Makefile.dep
    INSTALL redis-sentinel
    CC redis-cli.o
    LINK redis-cli
    CC redis-benchmark.o
    LINK redis-benchmark
    INSTALL redis-check-rdb

Hint: It's a good idea to run 'make test' ;)

    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install

编译成功后,会在redis-4.0.8/src目录下,生成redis-serverredis-sentinel可执行文件。

然后,我们在 master 节点(10.9.10.154)上,创建redis-4.0.8/redis-master.conf配置文件,示例配置:

# 使用守护进程模式
daemonize yes
# 非保护模式,可以外网访问
protected-mode no
# 端口号
port 6379
# 绑定ip,本机的ip
bind 10.9.10.154
# 学习开发,使用最大日志级别,能够看到最多的日志信息
loglevel debug
# 指定日志文件路径,没有文件的话,先创建
logfile /home/ubuntu/redis/redis-4.0.8/redis-server.log
# 客户端访问,需要密码连接
requirepass 123456

然后启动 master:

$ src/redis-server redis-master.config
$ cat redis-server.log
8517:C 28 Feb 06:10:38.345 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8517:C 28 Feb 06:10:38.345 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=8517, just started
8517:C 28 Feb 06:10:38.345 # Configuration loaded
8518:M 28 Feb 06:10:38.349 * Increased maximum number of open files to 10032 (it was originally set to 1024).
8518:M 28 Feb 06:10:38.352 * Running mode=standalone, port=6379.
8518:M 28 Feb 06:10:38.352 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
8518:M 28 Feb 06:10:38.352 # Server initialized
8518:M 28 Feb 06:10:38.352 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
8518:M 28 Feb 06:10:38.352 * Ready to accept connections
8518:M 28 Feb 06:10:38.352 - 0 clients connected (0 slaves), 765776 bytes in use

查看 Redis 的状态:

$ redis-cli -h 10.9.10.154 -p 6379 -a 123456
10.9.10.154:6379> INFO replication
# Replication
role:master
connected_slaves:0
master_replid:7f90fb4ba0c2c450b184a348f23f31d9c40b010d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
10.9.10.154:6379> ping
PONG

添加和获取键值测试:

10.9.10.154:6379> set test hello
OK
10.9.10.154:6379> get test
"hello"

Redis 常用命令:

  • 启动服务:service redis start
  • 停止服务:service redis stop
  • 重启服务:service ngredisnx restart

如果没有配置为 Service 服务,可以用下面命令关闭 Redis:

$ src/redis-cli shutdown
或者
$ ps aux | grep redis
$ kill 111

4. 搭建 Redis Server(slave)

slave 节点和 master 节点搭建差不多,只不过redis-slave.conf配置文件,有些不同:

# 使用守护进程模式
daemonize yes
# 非保护模式,可以外网访问
protected-mode no 
# 端口号
port 6379
# 绑定ip,本机的ip
bind 10.9.10.152
# 指定 master 的 ip 地址和端口
slaveof 10.9.10.154 6379
# 学习开发,使用最大日志级别,能够看到最多的日志信息
loglevel debug
# 指定日志文件路径,没有文件的话,先创建
logfile /home/ubuntu/redis/redis-4.0.8/redis-server.log
# 设置访问 master 的密码
masterauth 123456

然后启动 slave:

$ src/redis-server redis-slave.conf
$ cat redis-server.log
7307:C 28 Feb 06:11:00.987 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7307:C 28 Feb 06:11:00.987 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=7307, just started
7307:C 28 Feb 06:11:00.987 # Configuration loaded
7308:S 28 Feb 06:11:00.989 * Increased maximum number of open files to 10032 (it was originally set to 1024).
7308:S 28 Feb 06:11:00.989 * Running mode=standalone, port=6379.
7308:S 28 Feb 06:11:00.990 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7308:S 28 Feb 06:11:00.990 # Server initialized
7308:S 28 Feb 06:11:00.990 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
7308:S 28 Feb 06:11:00.990 * Ready to accept connections
7308:S 28 Feb 06:11:00.990 - 0 clients connected (0 slaves), 765736 bytes in use
7308:S 28 Feb 06:11:00.990 * Connecting to MASTER 10.9.10.154:6379
7308:S 28 Feb 06:11:00.990 * MASTER <-> SLAVE sync started
7308:S 28 Feb 06:11:00.990 * Non blocking connect for SYNC fired the event.
7308:S 28 Feb 06:11:00.991 * Master replied to PING, replication can continue...
7308:S 28 Feb 06:11:00.993 * Partial resynchronization not possible (no cached master)
7308:S 28 Feb 06:11:00.995 * Full resync from master: 55b29a9f06ffb89f96106287ce95ddfbdeccb986:0

查看 Redis 的状态:

$ redis-cli -h 10.9.10.152 -p 6379 -a 123456
10.9.10.154:6379> INFO replication
# Replication
role:slave
master_host:10.9.10.154
master_port:6379
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:280
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:55b29a9f06ffb89f96106287ce95ddfbdeccb986
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:280
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:280
10.9.10.152:6379> ping
PONG

然后我们再查看下 master 的状态(更新了connected_slaves的信息):

$ redis-cli -h 10.9.10.154 -p 6379 -a 123456
10.9.10.154:6379> INFO replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.9.10.152,port=6379,state=online,offset=28,lag=1
master_replid:55b29a9f06ffb89f96106287ce95ddfbdeccb986
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28

另外,你可以测试下,在 master 上添加一个 key 和 value,然后就可以在 salve 上获取这个对应的 key 的 value。

5. 搭建 Redis Sentinel

接着,我们分别在三台服务器上,配置 Redis Sentinel,创建redis-4.0.8/sentinel-my.conf配置文件,示例配置(记得更改不同的bind ip):

port 26379
bind 10.9.10.154
daemonize yes
logfile /home/ubuntu/redis/redis-4.0.8/redis-sentinel.log

sentinel monitor manager1 10.9.10.154 6379 2
sentinel auth-pass manager1 123456
sentinel down-after-milliseconds manager1 60000
sentinel failover-timeout manager1 180000
sentinel parallel-syncs manager1 1

这四行配置为一组,因为我们只有一个 master 节点,所以只配置了一个 master,可以配置多个 master,不用配置 slave 的信息,因为 slave 能够被自动检测到(master 节点会有关于 slave 的消息)

第一行配置指示 Sentinel 去监视一个名为manager1的主服务器,这个主服务器的 IP 地址为10.9.10.154,端口号为 6379,而将这个主服务器判断为失效至少需要 2 个 Sentinel 同意(只要同意 Sentinel 的数量不达标,自动故障迁移就不会执行)。

其他选项的基本格式如下:

sentinel <选项的名字> <主服务器的名字> <选项的值>

选项说明:

  • auth-pass:选项指定了 master 的连接密码。
  • down-after-milliseconds:选项指定了 Sentinel 认为服务器已经断线所需的毫秒数。
  • failover-timeout:如果在该时间(ms)内未能完成 failover 操作,则认为该 failover 失败。
  • parallel-syncs:选项指定了在执行故障转移时,最多可以有多少个从服务器同时对新的主服务器进行同步,这个数字越小,完成故障转移所需的时间就越长。

接着,我们启动 Redis Sentinel(默认端口 26379):

$ src/redis-server sentinel-my.conf --sentinel
或者
$ src/redis-sentinel sentinel-my.conf

$ cat redis-sentinel.log
5716:X 28 Feb 03:04:17.407 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5716:X 28 Feb 03:04:17.407 # Redis version=4.0.8, bits=64, commit=00000000, modified=0, pid=5716, just started
5716:X 28 Feb 03:04:17.407 # Configuration loaded
5716:X 28 Feb 03:04:17.408 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 4.0.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 5716
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

5716:X 28 Feb 03:04:17.410 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5716:X 28 Feb 03:04:17.412 # Sentinel ID is 493852dd16017dccdf7b169a256ce7eea2719aff
5716:X 28 Feb 03:04:17.412 # +monitor master manager1 10.9.10.154 6379 quorum 2

当有其他服务器启动 Redis Sentinel 的时候,会有这样的日志(Redis Sentinel 是会相互通信的):

5716:X 28 Feb 03:18:46.404 # -tilt #tilt mode exited
5716:X 28 Feb 03:19:43.959 * +sentinel sentinel 55ebce153ac69c908800bc14beff24725fc5a721 10.9.10.152 26379 @ manager1 10.9.10.154 6379

连接测试(测试三台服务器):

$ redis-cli -h 10.9.10.154 -p 26379
10.9.10.154:6379> ping
PONG

$ redis-cli -h 10.9.10.152 -p 26379
10.9.10.152:6379> ping
PONG

$ redis-cli -h 10.9.10.155 -p 26379
10.9.10.155:6379> ping
PONG

其他常用命令:

  • PING - 这个命令简单的返回 PONE。
  • SENTINEL masters - 展示监控的 master 清单和它们的状态。
  • SENTINEL master [master name] - 展示指定 master 的状态和信息。
  • SENTINEL slaves [master name] - 展示 master 的 slave 清单和它们的状态。
  • SENTINEL sentinels [master name] - 展示 master 的 sentinel 实例的清单和它们的状态。
  • SENTINEL get-master-addr-by-name [master name] - 返回 master 的 IP 和端口。如果故障转移在处理中或成功终止,返回晋升的 slave 的 IP 和端口。
  • SENTINEL reset [pattern] - 这个命令将重置所有匹配名字的 masters。参数是 blog 风格的。重置的过程清空 master 的所有状态,并移除已经发现和关联 master 的所有 slave 和 sentinel。
  • SENTINEL failover [master name] - 如果 master 不可到达,强制执行一个故障转移,而不征求其他 Sentinel 的同意。
  • SENTINEL ckquorum [master name] - 检查当前的 Sentinel 配置是否能够到达故障转移需要的法定人数,并且需要授权故障转移的多数。这个命令应该用于监控系统检查部署是否正确。
  • SENTINEL flushconfig - 强制 Sentinel 在磁盘上重写它的配置,包括当前的 Sentinel 状态。通常 Sentinel 每次重写配置改变它的状态。然而有时由于操作错误、硬盘故障、包升级脚本或配置管理器可能导致配置文件丢失。在这种情况下收到强制 Sentinel 重写配置文件。这个命令即使上面的配置文件完全不见了。

6. Redis Sentinel 故障转移测试

我们的计划是,将 master 的 Redis 服务停掉,看看 Redis Sentinel 是如何操作的。

首先,我们先查看下目前 master 节点的信息(记住 IP):

$ redis-cli -h 10.9.10.154 -p 6379
10.9.10.154:26379> SENTINEL get-master-addr-by-name manager1
1) "10.9.10.154"
2) "6379"

然后执行下面命令(强制 Redis Server 休眠 120 秒):

$ redis-cli -h 10.9.10.154 -p 6379 -a 123456 DEBUG sleep 120

然后我们再查看下 master 节点的信息:

$ redis-cli -h 10.9.10.154 -p 6379
10.9.10.154:26379> SENTINEL get-master-addr-by-name manager1
1) "10.9.10.152"
2) "6379"

发现 IP 已经变成了此前 salve 节点的,也就是说10.9.10.152变成了 master,然后我们查看下当前 master 的信息:

$ redis-cli -h 10.9.10.152 -p 6379 -a 123456
10.9.10.152:6379> INFO replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.9.10.154,port=6379,state=online,offset=1270535,lag=0
master_replid:8b033de60b4be410a1706ff6b27b52b97bcc2981
master_replid2:e56e893b96e7df8fda00ebcacf1d4b24c9499c4a
master_repl_offset:1270535
second_repl_offset:1208143
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:221960
repl_backlog_histlen:1048576

slave0:ip=10.9.10.154,port=6379表示此前的 master,已变为现在的 salve。

整个过程,我们可以查看 Redis Sentinel 的日志:

20153:X 28 Feb 09:13:51.023 # +sdown master manager1 10.9.10.154 6379
20153:X 28 Feb 09:13:51.243 # +new-epoch 1
20153:X 28 Feb 09:13:51.244 # +vote-for-leader c14ac16f2a43c311c663677b7e056e744e2e2852 1
20153:X 28 Feb 09:13:52.102 # +odown master manager1 10.9.10.154 6379 #quorum 2/2
20153:X 28 Feb 09:13:52.102 # Next failover delay: I will not start a failover before Wed Feb 28 09:19:51 2018
20153:X 28 Feb 09:13:52.365 # +config-update-from sentinel c14ac16f2a43c311c663677b7e056e744e2e2852 10.9.10.152 26379 @ manager1 10.9.10.154 6379
20153:X 28 Feb 09:13:52.365 # +switch-master manager1 10.9.10.154 6379 10.9.10.152 6379
20153:X 28 Feb 09:13:52.365 * +slave slave 10.9.10.154:6379 10.9.10.154 6379 @ manager1 10.9.10.152 6379
20153:X 28 Feb 09:15:00.797 * +convert-to-slave slave 10.9.10.154:6379 10.9.10.154 6379 @ manager1 10.9.10.152 6379

翻译一下就是:

  1. 每个 Sentinel 发现了主节点挂掉了并有一个 +sdown 事件
  2. 这个事件稍候升级到 +odown,意味着大多数 Sentinel 已经同意了主节点是不可达的。
  3. Sentinels 开始投票一个 Sentinel 开始并尝试故障转移
  4. 故障转移开始

另外,需要注意的是,Redis Sentinel 并不是提供对外服务的地址,它只是管理 Redis 主备切换的监测工具,所以,对外 Client 提供的地址,仍是 Redis Server 的地址(包含 salve),当然,也可以像提供负载均衡(SLB)或者虚拟IP(Virtual IP,VIP),进行统一地址的访问。

参考资料:

posted @ 2018-03-01 00:32  田园里的蟋蟀  阅读(6901)  评论(6编辑  收藏  举报