Redis+Sentinel哨兵模式安装与配置

机器环境有限,搭建一个伪集群。

1.环境介绍

集群机器                           redis服务                       哨兵(sentinel)                     节点描述

虚拟机:172.17.199.70    端口6400            安装sentinel服务,端口26400             主节点

虚拟机:172.17.199.70    端口6401            安装sentinel服务,端口26401             从节点

虚拟机:172.17.199.70    端口6402            安装sentinel服务,端口26402             从节点

2.配置主从关系

2.1.主redis.conf 配置  #日志和数据文件地址自行修改

daemonize yes

pidfile “/usr/redis/redis-2.8.19/redis_6400.pid”

port 6400

tcp-backlog 65535

bind 0.0.0.0

timeout 0

tcp-keepalive 0

loglevel notice

logfile “/usr/redis/redis-2.8.19/redis_6400.log”

maxmemory 8gb

maxmemory-policy allkeys-lru

databases 16

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename “dump.rdb”

dir “/usr/redis/redis-2.8.19/data”

slave-serve-stale-data yes

slave-read-only yes

repl-disable-tcp-nodelay no

slave-priority 100

appendonly no

appendfilename “appendonly.aof”

appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

lua-time-limit 5000

slowlog-log-slower-than 10000

slowlog-max-len 128

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

2.2.从redis.conf 配置    #相比于主多了一行slaveof

daemonize yes

pidfile “/usr/redis/redis-slave1/redis_6401.pid”

port 6401

tcp-backlog 65535

bind 0.0.0.0

timeout 0

tcp-keepalive 0

loglevel notice

logfile “/usr/redis/redis-slave1/redis_6401.log”

maxmemory 8gb

maxmemory-policy allkeys-lru

databases 16

save 900 1

save 300 10

save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename “dump.rdb”

dir “/usr/redis/redis-slave1/data”

slave-serve-stale-data yes

slave-read-only yes

repl-disable-tcp-nodelay no

slave-priority 100

appendonly no

appendfilename “appendonly.aof”

appendfsync everysec

no-appendfsync-on-rewrite no

auto-aof-rewrite-percentage 100

auto-aof-rewrite-min-size 64mb

lua-time-limit 5000

slowlog-log-slower-than 10000

slowlog-max-len 128

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

slaveof 172.17.199.70 6400

2.3启动

./src/redis-server redis.conf

2.4.查询主从关系

2.4.1主

[root@iZ2zeemywxjdx93lap023pZ redis-slave1]# ./src/redis-cli -h 172.17.199.70 -p 6400 info Replication

# Replication

role:master

connected_slaves:2

slave0:ip=172.17.199.70,port=6401,state=online,offset=15596870,lag=1

slave1:ip=172.17.199.70,port=6402,state=online,offset=15597011,lag=0

master_repl_offset:15597011

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:14548436

repl_backlog_histlen:1048576

2.4.2从

[root@iZ2zeemywxjdx93lap023pZ redis-slave1]# ./src/redis-cli -h 172.17.199.70 -p 6401 info Replication

# Replication

role:slave

master_host:172.17.199.70

master_port:6400

master_link_status:up

master_last_io_seconds_ago:0

master_sync_in_progress:0

slave_repl_offset:15606951

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

3.配置哨兵Sentinel

port 26400

daemonize yes

dir “/usr/redis/redis-2.8.19/sentinel”

sentinel monitor mymaster 172.17.199.70 6400 2

sentinel down-after-milliseconds mymaster 3000

sentinel failover-timeout mymaster 10000

sentinel config-epoch mymaster 0

sentinel leader-epoch mymaster 0

sentinel known-slave mymaster 172.17.199.70 6401

sentinel known-slave mymaster 172.17.199.70 6402

# Generated by CONFIG REWRITE

sentinel known-sentinel mymaster 172.17.199.70 26401 b0adf1633ee389f66794961751f069041b838471

sentinel known-sentinel mymaster 172.17.199.70 26402 c331f278bc5f54ba3e83007ef8af50eccf51e252

sentinel current-epoch 0

port 26401

daemonize yes

dir “/usr/redis/redis-slave1/sentinel”

sentinel monitor mymaster 172.17.199.70 6400 2

sentinel down-after-milliseconds mymaster 3000

sentinel failover-timeout mymaster 10000

sentinel config-epoch mymaster 0

sentinel leader-epoch mymaster 0

sentinel known-slave mymaster 172.17.199.70 6400

sentinel known-slave mymaster 172.17.199.70 6402

# Generated by CONFIG REWRITE

sentinel known-sentinel mymaster 172.17.199.70 26400 57ec8c95e8292ccbcc4da37202f2bdada5208936

sentinel known-sentinel mymaster 172.17.199.70 26402 c331f278bc5f54ba3e83007ef8af50eccf51e252

sentinel current-epoch 0

port 26402

daemonize yes

dir “/usr/redis/redis-slave2/sentinel”

sentinel monitor mymaster 172.17.199.70 6400 2

sentinel down-after-milliseconds mymaster 3000

sentinel failover-timeout mymaster 10000

sentinel config-epoch mymaster 0

sentinel leader-epoch mymaster 0

sentinel known-slave mymaster 172.17.199.70 6401

sentinel known-slave mymaster 172.17.199.70 6400

# Generated by CONFIG REWRITE

sentinel known-sentinel mymaster 172.17.199.70 26401 b0adf1633ee389f66794961751f069041b838471

sentinel known-sentinel mymaster 172.17.199.70 26400 57ec8c95e8292ccbcc4da37202f2bdada5208936

sentinel current-epoch 0

配置文件说明:

1. port :当前Sentinel服务运行的端口

2. dir : Sentinel服务运行时使用的临时文件夹

3.sentinel monitor master001 192.168.110.10163792:Sentinel去监视一个名为master001的主redis实例,这个主实例的IP地址为本机地址192.168.110.101,端口号为6379,而将这个主实例判断为失效至少需要2个 Sentinel进程的同意,只要同意Sentinel的数量不达标,自动failover就不会执行

4.sentinel down-after-milliseconds master001 30000:指定了Sentinel认为Redis实例已经失效所需的毫秒数。当实例超过该时间没有返回PING,或者直接返回错误,那么Sentinel将这个实例标记为主观下线。只有一个 Sentinel进程将实例标记为主观下线并不一定会引起实例的自动故障迁移:只有在足够数量的Sentinel都将一个实例标记为主观下线之后,实例才会被标记为客观下线,这时自动故障迁移才会执行

5.sentinel parallel-syncs master001 1:指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长

6.sentinel failover-timeout master001 180000:如果在该时间(ms)内未能完成failover操作,则认为该failover失败

7.sentinel notification-script :指定sentinel检测到该监控的redis实例指向的实例异常时,调用的报警脚本。该配置项可选,但是很常用

4.测试sentinel集群

4.1.停止6400主

[root@iZ2zeemywxjdx93lap023pZ redis-slave2]# ./src/redis-cli -h 127.0.0.1 -p 6400 info Replication

Could not connect to Redis at 127.0.0.1:6400: Connection refused

[root@iZ2zeemywxjdx93lap023pZ redis-slave2]# ./src/redis-cli -h 127.0.0.1 -p 6401 info Replication

# Replication

role:slave

master_host:172.17.199.70

master_port:6402

master_link_status:up

master_last_io_seconds_ago:0

master_sync_in_progress:0

slave_repl_offset:11952

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

#可以看到6400不能连接,6402成为了主节点

4.2启动6400

[root@iZ2zeemywxjdx93lap023pZ redis-2.8.19]# ./src/redis-server redis.conf

[root@iZ2zeemywxjdx93lap023pZ redis-2.8.19]# ./src/redis-cli -h 127.0.0.1 -p 6400 info Replication

# Replication

role:slave

master_host:172.17.199.70

master_port:6402

master_link_status:up

master_last_io_seconds_ago:0

master_sync_in_progress:0

slave_repl_offset:26737

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

#发现主节点还是6402,6400变成了从节点

4.3只保留一个哨兵sentinel,停止6402主节点

[root@iZ2zeemywxjdx93lap023pZ redis-2.8.19]# ./src/redis-cli -h 127.0.0.1 -p 6402 info Replication

Could not connect to Redis at 127.0.0.1:6402: Connection refused

[root@iZ2zeemywxjdx93lap023pZ redis-2.8.19]# ./src/redis-cli -h 127.0.0.1 -p 6400 info Replication

# Replication

role:slave

master_host:172.17.199.70

master_port:6402

master_link_status:down

master_last_io_seconds_ago:-1

master_sync_in_progress:0

slave_repl_offset:163317

master_link_down_since_seconds:11

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

[root@iZ2zeemywxjdx93lap023pZ redis-2.8.19]# ./src/redis-cli -h 127.0.0.1 -p 6401 info Replication

# Replication

role:slave

master_host:172.17.199.70

master_port:6402

master_link_status:down

master_last_io_seconds_ago:-1

master_sync_in_progress:0

slave_repl_offset:163317

master_link_down_since_seconds:14

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

#发现6402主已经连接不上,集群中也不存在主节点,原因是因为sentinel.conf中配置 sentinel monitor mymaster 172.17.199.70 6400 2 随后一个配置的是2

    原文作者:暴发户010
    原文地址: https://www.jianshu.com/p/2c3de68f9d23
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞