​ 基于Sentinel实现redis主从自动切换

Sentinel(哨兵)是用于监控redis集群中Master状态的工具,它可以实现对redis的监控、通知、自动故障转移。

Sentinel作用:

  1. Master状态检测

  2. 当被监控的某个 Redis Master异常无法连接时 Sentinel 可以向系统管理员发送通知, 也可以通过 API 向其他程序发送通知,并且进行Master-Slave切换,将其中一个Slave作为Master,将之前的Master作为Slave

  3. Master-Slave切换后,masterredis.conf、slaveredis.conf和sentinel.conf的内容都会发生改变,即master_redis.conf中会多一行slaveof的配置,sentinel.conf的监控目标会随之调换。当应用程序连接Redis 服务器时, Redis Sentinel会告之新的主服务器地址和端口。

Redis 主从部署:

192.168.11.12:6379  master              
192.168.11.12:6381  slave          
192.168.11.13:6379  slave           
192.168.11.13:6381  slave            
192.168.11.14:6379  slave           
192.168.11.14:6381  slave

Sentinel:

192.168.11.12                 
192.168.11.13           
192.168.11.14

具体部署(简写) mkdir -p /opt/redis6379/{bin,etc,var,log} 
mkdir -p /opt/redis6381/{bin,etc,var,log}

tar zxmf redis-2.8.19.tar.gz               
cd redis-2.8.19               
make             
make PREFIX=/opt/redis6379 install     
make PREFIX=/opt/redis6381 install

master config:

cat redis6379.conf
daemonize yes
pidfile /opt/redis6379/redis6379.pid
port 6379
tcp-backlog 10240
maxclients 10000
bind 0.0.0.0
timeout 0
tcp-keepalive 0
loglevel notice
logfile /opt/redis6379/log/redis6379.log
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump6379.rdb
dir /export/redis6379/
slave-priority 100
maxmemory 2500mb
maxmemory-policy allkeys-lru
appendonly no
appendfilename "appendonly01.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
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

slave config:

cat redis6379.conf
daemonize yes
pidfile /opt/redis6379/redis6379.pid
port 6379
tcp-backlog 10240
maxclients 10000
bind 0.0.0.0
timeout 0
tcp-keepalive 0
loglevel notice
logfile /opt/redis6379/log/redis6379.log
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump6379.rdb
dir /export/redis6379/
slave-priority 100
slave-serve-stale-data yes
slave-read-only yes
slave-priority 100
slaveof 192.168.11.12 6379
maxmemory 2500mb
maxmemory-policy allkeys-lru
appendonly no
appendfilename "appendonly01.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 ""
save 900 1
save 300 100
save 60 10000
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

启动:

/opt/redis6379/bin/redis-server /opt/redis6379/etc/redis6379.conf 

/opt/redis6381/bin/redis-server /opt/redis6381/etc/redis6381.conf  

# ps aux | grep redis
root       6379  0.2  0.7 137368  7520 ?        Ssl  08:19   0:00 /opt/redis6379/bin/redis-server 0.0.0.0:6379                     
root       6457  0.1  0.7 137368  7504 ?        Ssl  08:19   0:00 /opt/redis6381/bin/redis-server 0.0.0.0:6381

查看主从状态:

# /opt/redis6379/bin/redis-cli -h 127.0.0.1 -p 6379 INFO Replication
# Replication
role:master
connected_slaves:5
slave0:ip=192.168.11.12,port=6381,state=online,offset=449,lag=1
slave2:ip=192.168.11.13,port=6379,state=online,offset=449,lag=1
slave4:ip=192.168.11.13,port=6381,state=online,offset=449,lag=1
slave2:ip=192.168.11.14,port=6379,state=online,offset=449,lag=1
slave4:ip=192.168.11.14,port=6381,state=online,offset=449,lag=1
master_repl_offset:449
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:448

查看同步:

192.168.11.12:6379
127.0.0.1:6379> set aa 123
OK

192.168.11.13:6379
127.0.0.1:6379> get aa
"123"

192.168.11.14:6381
127.0.0.1:6381> get aa
"123"

数据以同步

配置sentinel:

mkdir -p /opt/redis6379/sentinel

sentinel.conf:

port 26379
dir "/opt/redis6379/sentinel"
sentinel monitor master 192.168.11.12 6379 2
sentinel down-after-milliseconds master 60000
sentinel config-epoch master 1
sentinel leader-epoch master 1
sentinel known-slave master 192.168.11.12 6381
sentinel known-slave master 192.168.11.14 6381
# Generated by CONFIG REWRITE
sentinel known-slave master 192.168.11.112 6379
sentinel known-slave master 192.168.11.112 6381
sentinel known-slave master 192.168.11.14 6379
sentinel known-sentinel master 192.168.11.112 26379 459b949dd141a301f93d764d92bb04af9450870b
sentinel known-sentinel master 192.168.11.14 26379 9638d9c07c83c8cf6b3cac0b419867d9a4eeb17c
sentinel current-epoch 1

说明: port 监听端口 
dir Sentinel服务运行时使用的临时文件夹 sentinel monitor master IP 端口 x 监控的 redis master ip 端口 判断为失效至少需要2个 Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行

一下配置启动之后无法看到 sentinel failover-timeout master 180000 : 如果在该时间(ms)内未能完成failover操作,则认为该failover失败 sentinel parallel-syncs master 1:指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长

启动 redis-sentinel :

/opt/redis6379/bin/redis-sentinel /opt/redis6379/sentinel/sentinel.conf  2>&1 /opt/redis6379/sentinel.log &

模拟故障:

192.168.11.12:6379
/opt/redis6379/bin/redis-cli -h 127.0.0.1 -p 6379 shutdown

查看:

192.168.11.12:6381
/opt/redis6379/bin/redis-cli -h 127.0.0.1 -p 6381 info Replication
# Replication
role:slave
master_host:192.168.11.14
master_port:6381
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:4265
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

master 已经主观转移

本文进行简单的sentinel测试 
后面进行redis参数优化讲解以及测试线上master方案 
twemproxy 
codis 
redis cluster

原创文章,作者:可乐,如若转载,请注明出处:http://www.178linux.com/10499

(0)
可乐可乐
上一篇 2016-02-14 10:22
下一篇 2016-02-14 14:04

相关推荐

  • 脚本实现httpd创建虚拟主机

    概述 本文使用脚本实现基于主机名的虚拟主机按需创建: 脚本可接受参数,提供独立站点目录; 生成独立站点首页; 脚本可接受参数,参数虚拟主机名称; 每虚拟使用单独的配置文件; 脚本可接受参数,参数虚拟主机名称; 环境 系统基于CentOS7.2,并通过yum安装httpd 2.4.6 建议关闭防火墙和selinux。 演示   客户机将域名解析写入/…

    Linux干货 2016-10-08
  • 帮助和文件管理信息

    history命令: -c 清空命令历史 -d offset 删除历史中指定的第offset个命令 n 显示最近的n条历史 -a 追加本次会话新执行的命令历史列表至历史文件 -n 读历史文件中未读过的行到历史列表 -r 读历史文件附加到历史列表 -w 保存历史列表到指定的历史文件 -p 展开历史参数成多行,但不存在历史列表中 -s 展开历史参数马一行。附加在…

    Linux干货 2017-04-11
  • 测试

    3、查询file.txt以abc结尾的行grep "abc$" file.txt4、删除file.txt文件中的空行grep -v '^$' file.txt > file.txtsed '/^$/d' file.txt > file.txt(覆盖重定向文件变为空该文件为空,但是追加时正常成功。…

    Linux干货 2016-06-12
  • rpm程序包管理器使用详解

    rpm程序包管理器的使用详解   说到程序包管理器,就会想到基于CentOS系统上的rpm命令与其对应的前端处理器yum。所谓程序包,是将源代码转换为二进制格式,再组织成一个或者多个包文件供用户安装使用。rpm程序包的命名格式如下:   name-VERSION-release.arch.rpm   软件名-版本号(分为主版本m…

    Linux干货 2016-08-21
  • 给linux系统添加新的磁盘

    虚拟化环境中,我们经常会遇到需要增加磁盘容量的情况,通常有两种方式,第一种是添加一块新的硬盘,另一种是扩容原有磁盘,以下是添加新磁盘至linux系统的操作规范。注:具体磁盘信息可能有所不同。 ##查看新增加磁盘/dev/sdb的信息 [root@CentOS7 ~]# fdisk -l /dev/sdb   Disk /dev/sdb: 5368 …

    Linux干货 2016-11-11
  • Linux入门之常见文本处理工具

    Linux入门之常见文本处理工具 文本内容查看命令 cat   tac    rev  more  less   head   tail 普通文本查看 cat  tac  rev cat 命令 cat  [option]…

    Linux干货 2016-08-08

评论列表(1条)

  • stanley
    stanley 2016-02-14 10:23

    样式略乱,内容详尽