Redis Sentinel实现哨兵模式搭建 做个小小的总结

2018-12-2720:44:31服务器及运维Comments2,663 views字数 4768阅读模式

Redis哨兵模式,用现在流行的话可以说就是一个“哨兵机器人”,给“哨兵机器人”进行相应的配置之后,这个"机器人"可以7*24小时工作,它能能够自动帮助你做一些事情,如监控,提醒,自动处理故障等。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

Redis-sentinel简介文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

Redis-sentinel是Redis的作者antirez,因为Redis集群的被各大公司使用,每个公司要写自己的集群管理工具,于是antirez花了几个星期写出了Redis-sentinel。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance),Redis 的 Sentinel 为Redis提供了高可用性。使用哨兵模式创建一个可以不用人为干预而应对各种故障的Redis部署。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

该系统执行以下三个任务:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

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

sentinel的分布式特性
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

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

单个sentinel进程来监控redis集群是不可靠的,当sentinel进程宕掉后(sentinel本身也有单点问题,single-point-of-failure)整个集群系统将无法按照预期的方式运行。所以有必要将sentinel集群,这样有几个好处:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

  • 有一些sentinel进程宕掉了,依然可以进行redis集群的主备切换;
  • 如果只有一个sentinel进程,如果这个进程运行出错,或者是网络堵塞,那么将无法实现redis集群的主备切换(单点问题);
  • 如果有多个sentinel,redis的客户端可以随意地连接任意一个sentinel来获得关于redis集群中的信息

一个健壮的部署至少需要三个哨兵实例。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

三个哨兵实例应该放置在客户使用独立方式确认故障的计算机或虚拟机中。例如不同的物理机或不同可用区域的虚拟机。【本次讲解是一个机器上进行搭建,和多级是一个道理文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

背景文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

最近项目需求,接触到了Redis的搭建,简单记录下搭建过程中遇到的坑文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

总体配置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

192.168.1.100:6379 -> master
192.168.1.101:6379 -> slave
192.168.1.102:6379 -> slave
192.168.1.100:26379 -> sentinel
192.168.1.101:26379 -> sentinel
192.168.1.102:26379 -> sentinel

搭建步骤文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

1.安装redis文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

# 解压
tar -xvf /usr/local/redis-3.2.11.tar.gz

mkdir -p /usr/local/redis/bin

cp /usr/local/redis/src/{redis-benchmark,redis-check-aof,redis-check-rdb,redis-cli,redis-sentinel,redis-server,redis-trib.rb} /usr/local/redis/bin

mkdir -p /u01/redis/{6379/{log,data,pid,conf},26379/{log,data,pid,conf}

# 添加环境变量
echo "export PATH=/usr/local/redis/bin:$PATH" >> /etc/profile
source /etc/profile

2.redis-6379配置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

redis节点配置基本如下,把如下配置分别cp到三台虚拟机的/u01/redis/6379/conf/redis_6379.conf文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

bind 0.0.0.0
protected-mode no
daemonize yes
pidfile "/u01/redis/6379/pid/redis_6379.pid"
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/u01/redis/6379/log/redis_6379.log"
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/u01/redis/6379/data"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
min-slaves-to-write 1
min-slaves-max-lag 10
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512

启动服务文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

# 在三台虚拟机上分别执行
redis-server /u01/redis/6379/conf/redis_6379.conf

建立主从关系文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

# 在192.168.1.101
redis-cli -p 6379 SLAVEOF 192.168.1.100 6379

# 在192.168.1.102
redis-cli -p 6379 SLAVEOF 192.168.1.100 6379

查看Replication文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

192.168.1.101:6379> info replication
# Replication
role:master
connected_slaves:2
min_slaves_good_slaves:2
slave0:ip=192.168.1.102,port=6379,state=online,offset=9577826,lag=1
slave1:ip=192.168.1.103,port=6379,state=online,offset=9577965,lag=0
master_repl_offset:9577965
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:8529390
repl_backlog_histlen:1048576

192.168.1.102:6379> info replication
# Replication
role:slave
master_host:192.168.1.101
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:9600220
slave_priority:100
slave_read_only:1
connected_slaves:0
min_slaves_good_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

192.168.1.103:6379> info replication
# Replication
role:slave
master_host:192.168.1.101
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:9612675
slave_priority:100
slave_read_only:1
connected_slaves:0
min_slaves_good_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

3.sentinel-6379配置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

sentinel节点配置基本如下,把如下配置分别cp到三台虚拟机的/u01/redis/26379/conf/sentinel_26379.conf文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

sentinel monitor mymaster 后监控的是redis中的master节点,也就是192.168.1.100,所以这个文件在三台机器上是相同的文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

port 26379
bind 0.0.0.0
daemonize yes
protected-mode no
dir "/u01/redis/26379/tmp"
logfile "/u01/redis/26379/log/sentinel_26379.log"
sentinel monitor mymaster 192.168.1.100 6379 1

等待启动完毕后观察/u01/redis/26379/conf/sentinel_26379.conf文件变化文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

查看sentinel状态用info sentinel文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

redis-cli -h 192.168.1.100 -p 26379 info sentinel

# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=zhuanche01,status=ok,address=192.168.1.100:6379,slaves=2,sentinels=3

总结文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

我搭建的时候遇到了192.168.1.101、192.168.1.102上的sentinel启动后一段时间出错的问题,后来发现是没有监控master。再就是出问题了多看log。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9108.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/9108.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定