Redis Sentinel 哨兵

来源:互联网 发布:淘宝会员管理有什么用 编辑:程序博客网 时间:2024/05/19 13:28

Sentinel 是Redis的高可用性解决方案:由一个或多个Sentinel实例组成的Sentinel系统可以监视多个主服务器,并在被监视的主服务进入下线状态时,自动将下线主服务器属下的所有从服务器,并在被监视的主服务器进入下线状态时,自动将下线主服务器属下的某个从服务器升级为新的主服务器。

Redis主从分离(读写分离)

1.配置Master

(1)修改端口
# Accept connections on the specified port, default is 6379 (IANA #815344).# If port 0 is specified Redis will not listen on a TCP socket.port 6380

redis的默认端口是6379,这里我们把主服务器的端口设置为6380

(2)修改pidfile
# If a pid file is specified, Redis writes it where specified at startup# and removes it at exit.## When the server runs non daemonized, no pid file is created if none is# specified in the configuration. When the server is daemonized, the pid file# is used even if not specified, defaulting to "/var/run/redis.pid".## Creating a pid file is best effort: if Redis is not able to create it# nothing bad happens, the server will start and run normally.pidfile /var/run/redis_6380.pid

pidfile是我们启动redis的时候,linux为我们分配的一个pid进程号,如果这里不作修改,会影响后面redis服务的启动。

(3)启动redis
启动redis,我们可以看到,redis已经占领了6380端口。
redis-cli -p 6380127.0.0.1:6380> info...# Replicationrole:masterconnected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0...

我们可以看到,redis现在的角色是一个master启动的服务。

2.配置Slave

(1)和上面配置master一样,我们需要修改端口号和pid文件,在修改完之后,我们有两种方法配置从服务。
################################# REPLICATION ################################## Master-Slave replication. Use slaveof to make a Redis instance a copy of# another Redis server. A few things to understand ASAP about Redis replication.## 1) Redis replication is asynchronous, but you can configure a master to#    stop accepting writes if it appears to be not connected with at least#    a given number of slaves.# 2) Redis slaves are able to perform a partial resynchronization with the#    master if the replication link is lost for a relatively small amount of#    time. You may want to configure the replication backlog size (see the next#    sections of this file) with a sensible value depending on your needs.# 3) Replication is automatic and does not need user intervention. After a#    network partition slaves automatically try to reconnect to masters#    and resynchronize with them.## slaveof <masterip> <masterport>slaveof 127.0.0.1 6380

我们可以在配置文件中直接修改slaveof属性,我们直接配置主服务器的ip地址,和端口号,如果这里主服务器有配置密码,可以通过配置masterauth来设置链接密码
# If the master is password protected (using the "requirepass" configuration# directive below) it is possible to tell the slave to authenticate before# starting the replication synchronization process, otherwise the master will# refuse the slave request.## masterauth <master-password>

# Replicationrole:slavemaster_host:127.0.0.1master_port:6380master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:71slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0

我们可以看到,现在的redis是一个从服务的角色,连接着6380的服务。

另外,还可以在服务启动后,修改它的状态
127.0.0.1:6382> slaveof 127.0.0.1 6380//修改后状态# Replicationrole:slavemaster_host:127.0.0.1master_port:6380master_link_status:upmaster_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:617slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0

上面两种配置的区别在于,当slave断线重连之后,如要我们是修改配置文件,重连之后会自己连接上去master,并且同步master上面的数据,如果 我们是手动连接上主服务器,重连之后,从服务器会读取自己的本地的rdb恢复数据,而不会去自动连接主服务。

如果需要设置读写分离,只需要在主服务器中设置:

# Note: read only slaves are not designed to be exposed to untrusted clients# on the internet. It's just a protection layer against misuse of the instance.# Still a read only slave exports by default all the administrative commands# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve# security of read only slaves using 'rename-command' to shadow all the# administrative / dangerous commands.slave-read-only yes

Sentinel

1.配置端口

在sentinel.conf配置文件中,我们可以找到port属性,这里是用来设置sentinel的端口,一般情况下,至少会需要三个哨兵对redis进行监控,我们可以通过修改端口启动多个sentinel服务。
# port <sentinel-port># The port that this sentinel instance will run onport 26379

2.配置主服务器的ip和端口

我们把监听的端口修改成6380,并且加上权值为2,这里的权值,是用来计算我们需要将哪一台服务器升级为主服务器。
# sentinel monitor <master-name> <ip> <redis-port> <quorum>## Tells Sentinel to monitor this master, and to consider it in O_DOWN# (Objectively Down) state only if at least <quorum> sentinels agree.## Note that whatever is the ODOWN quorum, a Sentinel will require to# be elected by the majority of the known Sentinels in order to# start a failover, so no failover can be performed in minority.## Slaves are auto-discovered, so you don't need to specify slaves in# any way. Sentinel itself will rewrite this configuration file adding# the slaves using additional configuration options.# Also note that the configuration file is rewritten when a# slave is promoted to master.## Note: master name should not include special characters or spaces.# The valid charset is A-z 0-9 and the three characters ".-_".sentinel monitor mymaster 127.0.0.1 6380 2

3.启动Sentinel

/sentinel$ redis-sentinel sentinel.conf


Sentinel的工作方式:
(1)每个Sentinel以每秒一次的频率向它所知的Master、Slave以及其他Sentinel实例发送一个PING命令。
(2)如果一个实例距离最后一次有效回复PING命令的时间超过down-after-milliseconds选项所指定的值,则这个实例会被Sentinel标记为主观下线。
(3)如果一个Master被标记为主观下线,则正在监视这个Master的所有Sentinel要以每秒一次的频率确认Master进入主观下线状态。
(4)当有足够数量的Sentinel在指定的时间范围内确认Master的确进入了主观下线状态,则Master会被标记为客观下线。
(5)在一般情况下,每个Sentinel会以每10秒一次的频率向它已知的所有Master、Slave发送INFO命令。
(6)当Master被Sentinel标记为客观下线时,Sentinel向下线的Master的所有Slave发送INFO命令的频率会以10秒一次改为每秒一次。
(7)若没有足够数量的Sentinel同意Master已经下线,Master的客观下线状态就会被移除。若Master重新向Sentinel的PING命令返回有效回复,master的主观下线状态就会被移除。
原创粉丝点击