三、Redis高可用(主从+sintinel+自定义脚本) - 简书

一、 master 配置

      # 网络设置
port 6379
bind 0.0.0.0
protected-mode yes
tcp-backlog 511
unixsocket "/usr/local/redis-3.2.9/log/6379.sock"
unixsocketperm 700
timeout 100
tcp-keepalive 300

# 通用参数
daemonize yes
pidfile "/usr/local/redis-3.2.9/log/6379.pid"
loglevel notice
logfile "/usr/local/redis-3.2.9/log/6379.log"


# 持久化
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump6379.rdb"
dir "/usr/local/redis-3.2.9/data"

# 复制
#slaveof
masterauth "yantao"
slave-serve-stale-data yes
slave-read-only no
repl-diskless-sync no 
repl-diskless-sync-delay 5
repl-ping-slave-period 10
repl-timeout 60
repl-disable-tcp-nodelay no
repl-backlog-size 1mb
repl-backlog-ttl 3600
slave-priority 100
min-slaves-to-write 3
min-slaves-max-lag 10


# 当该slave提升为master时,连接需要密码
requirepass "yantao"


# limit
maxclients 10000
maxmemory 100000
maxmemory-policy noeviction 
maxmemory-samples 5

appendonly yes
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

二、slave 配置,两台相同

      # 网络设置
port 6379
bind 0.0.0.0
protected-mode yes
tcp-backlog 511
unixsocket "/usr/local/redis-3.2.9/log/6379.sock"
unixsocketperm 700
timeout 100
tcp-keepalive 300

# 通用参数
daemonize yes
pidfile "/usr/local/redis-3.2.9/log/6379.pid"
loglevel notice
logfile "/usr/local/redis-3.2.9/log/6379.log"


# 持久化
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump6379.rdb"
dir "/usr/local/redis-3.2.9/data"

# 复制
slaveof 172.16.7.190 6379
masterauth "yantao"
slave-serve-stale-data yes
slave-read-only no
repl-diskless-sync no 
repl-diskless-sync-delay 5
repl-ping-slave-period 10
repl-timeout 60
repl-disable-tcp-nodelay no
repl-backlog-size 1mb
repl-backlog-ttl 3600
slave-priority 100
min-slaves-to-write 3
min-slaves-max-lag 10


# 当该slave提升为master时,连接需要密码
requirepass "yantao"


# limit
maxclients 10000
maxmemory 100000
maxmemory-policy noeviction 
maxmemory-samples 5

appendonly yes
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

三、启动所有redis server,查看master复制信息

      [[email protected] conf]# redis-cli -a yantao info replication
# Replication
role:master
connected_slaves:2
min_slaves_good_slaves:2
slave0:ip=172.16.7.191,port=6379,state=online,offset=127,lag=0
slave1:ip=172.16.7.192,port=6379,state=online,offset=127,lag=1
master_repl_offset:127
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:126

四、sentinel 配置。三台配置相同

      protected-mode no

port 10000
dir "/usr/local/redis-3.2.9/conf"
daemonize yes
logfile "/usr/local/redis-3.2.9/log/sentinel.log"

sentinel monitor redis_master 172.16.7.190 6379 2
sentinel auth-pass redis_master yantao
sentinel down-after-milliseconds redis_master 30000
sentinel failover-timeout redis_master 180000
sentinel parallel-syncs redis_master 2

注意: sentinel 启动后,会重写 sentinel.conf。 主要是增加了slave信息

五、停掉redis master 进行测试

停掉master 后,查看日志信息。

      14584:X 28 Jun 16:11:14.628 # +sdown master redis_master 172.16.7.190 6379
14584:X 28 Jun 16:11:14.704 # +new-epoch 1
14584:X 28 Jun 16:11:14.707 # +vote-for-leader 9f0be631296015d8238f06e2a8cccb04e1a237c8 1
14584:X 28 Jun 16:11:15.345 # +config-update-from sentinel 9f0be631296015d8238f06e2a8cccb04e1a237c8 172.16.7.191 10000 @ redis_master 172.16.7.190 6379
14584:X 28 Jun 16:11:15.345 # +switch-master redis_master 172.16.7.190 6379 172.16.7.192 6379
14584:X 28 Jun 16:11:15.346 * +slave slave 172.16.7.191:6379 172.16.7.191 6379 @ redis_master 172.16.7.192 6379
14584:X 28 Jun 16:11:15.346 * +slave slave 172.16.7.190:6379 172.16.7.190 6379 @ redis_master 172.16.7.192 6379
14584:X 28 Jun 16:11:45.399 # +sdown slave 172.16.7.190:6379 172.16.7.190 6379 @ redis_master 172.16.7.192 6379

从日志中可以看到, redis master 由之前的 172.16.7.190 切换到了 172.16.7.192

  • 在slave2 上查看 复制信息
      [[email protected] conf]# redis-cli -a yantao info replication
# Replication
role:master
connected_slaves:1
min_slaves_good_slaves:1
slave0:ip=172.16.7.191,port=6379,state=online,offset=28806,lag=1
master_repl_offset:28949
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:28948
  • 在slave2 上查看sentinel信息
      [[email protected] conf]# redis-cli -p 10000 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=redis_master,status=ok,address=172.16.7.192:6379,slaves=2,sentinels=3
  • 启动190上的redis,然后在 slave2上查看复制信息, 发现 190 也成为了192的slave
      [[email protected] conf]# redis-cli -a yantao info replication
# Replication
role:master
connected_slaves:2
min_slaves_good_slaves:2
slave0:ip=172.16.7.191,port=6379,state=online,offset=49561,lag=0
slave1:ip=172.16.7.190,port=6379,state=online,offset=49561,lag=1
master_repl_offset:49704
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:49703

查看每天服务的redis配置文件

      [[email protected] conf]# cat 6379.conf | grep slaveof
#slaveof ""
slaveof 172.16.7.192 6379

[[email protected] conf]#  cat 6379.conf | grep slaveof
slaveof 172.16.7.192 6379

[[email protected] conf]# cat 6379.conf | grep slaveof
[[email protected] conf]#

可以看出。redis的配置文件中的slaveof 指令也发生了变化。同样的,sentinel 也有类似的变化。

六、 配置监控脚本

在sentinel.conf中加入脚本定义

      sentinel notification-script redis_master /usr/local/redis-3.2.9/bin/notify.sh
sentinel client-reconfig-script redis_master /usr/local/redis-3.2.9/bin/reconfig.sh

那么完整的sentinel配置就是:

      protected-mode no

port 10000
dir "/usr/local/redis-3.2.9/conf"
daemonize yes
logfile "/usr/local/redis-3.2.9/log/sentinel.log"

sentinel monitor redis_master 172.16.7.190 6379 2
sentinel auth-pass redis_master yantao
sentinel down-after-milliseconds redis_master 30000
sentinel failover-timeout redis_master 180000
sentinel parallel-syncs redis_master 2
sentinel notification-script redis_master /usr/local/redis-3.2.9/bin/notify.sh
sentinel client-reconfig-script redis_master /usr/local/redis-3.2.9/bin/reconfig.sh
  • notify.sh
      [[email protected] conf]# cat ../bin/notify.sh 
#!/bin/bash

DIR="/usr/local/redis-3.2.9"
echo "master failovered at `date`" >> $DIR/log/redis_issues.log
  • reconfig.sh
      [[email protected] conf]# cat ../bin/reconfig.sh 
#!/bin/bash

#redis parament
#       


DIR="/usr/local/redis-3.2.9"
mastername=$1
role=$2
state=$3
from_ip=$4
from_port=$5
to_ip=$6
to_port=$7

echo -e "`date`    mastername: $mastername    role: $role    state: $state    from_ip: $from_ip    from_port: $from_port    to_ip: $to_ip    to_port: $to_port" >> $DIR/log/redis_issues.log

建议先停掉sentinel,再进行配置,再重启。

现在停掉slave2上的redis master。然后会进行故障迁移。我们看看 我们定义的脚本打印出来的东西

      [[email protected] log]# cat redis_issues.log 
master failovered at Wed Jun 28 17:03:09 CST 2017
master failovered at Wed Jun 28 17:04:40 CST 2017
master failovered at Wed Jun 28 17:04:40 CST 2017
master failovered at Wed Jun 28 17:04:40 CST 2017
master failovered at Wed Jun 28 17:04:40 CST 2017
master failovered at Wed Jun 28 17:04:40 CST 2017
Wed Jun 28 17:04:40 CST 2017    mastername: redis_master    role: observer    state: start    from_ip: 172.16.7.192    from_port: 6379    to_ip: 172.16.7.190    to_port: 6379
master failovered at Wed Jun 28 17:05:10 CST 2017

重点看下这句:

      Wed Jun 28 17:04:40 CST 2017    mastername: redis_master    role: observer    state: start    from_ip: 172.16.7.192    from_port: 6379    to_ip: 172.16.7.190    to_port: 6379

可以看到,在进行故障迁移时, sentinel 会给自定义的脚本传入相关参数的。
表示 master名字
表示的是 每个redis实力的角色,如leader、observer
表示状态
原来的redis master

故障迁移后的redis master

你可能感兴趣的:(三、Redis高可用(主从+sintinel+自定义脚本) - 简书)