主从切换技术的方法是:当主服务器宕机后,须要手动把一台从服务器切换为主服务器,这就须要人工干预,费事费力,还会形成一段时间内服务不可用。这不是一种推荐的方式,更多时候,咱们优先考虑哨兵模式。javascript
哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,做为进程,它会独立运行。其原理是哨兵经过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。java
这里的哨兵有两个做用linux
经过发送命令,让Redis服务器返回监控其运行状态,包括主服务器和从服务器。redis
当哨兵监测到master宕机,会自动将slave切换成master,而后经过发布订阅模式通知其余的从服务器,修改配置文件,让它们切换主机。服务器
然而一个哨兵进程对Redis服务器进行监控,可能会出现问题,为此,咱们可使用多个哨兵进行监控。各个哨兵之间还会进行监控,这样就造成了多哨兵模式。网络
用文字描述一下故障切换(failover)的过程。假设主服务器宕机,哨兵1先检测到这个结果,系统并不会立刻进行failover过程,仅仅是哨兵1主观的认为主服务器不可用,这个现象成为主观下线。当后面的哨兵也检测到主服务器不可用,而且数量达到必定值时,那么哨兵之间就会进行一次投票,投票的结果由一个哨兵发起,进行failover操做。切换成功后,就会经过发布订阅模式,让各个哨兵把本身监控的从服务器实现切换主机,这个过程称为客观下线。这样对于客户端而言,一切都是透明的。app
配置3个哨兵和1主2从的Redis服务器来演示这个过程。socket
特别注意:使用Redis哨兵模式,最少须要3个节点(一主多从结构)async
2、Redis主从复制搭建① 配置mastertcp
# 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 6379
② 修改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_6379.pid
pidfile 是咱们启动redis 的时候,linux 为咱们分配的一个pid 进程号,若是这里不做修改,会影响后面redis服务的启动
③ 启动 redis
[root@yunwei ~] # redis-server redis.conf 进入redis: redis-cli -p 6379 127.0.0.1:6379> info ... # Replication role:master 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 ...
咱们能够看到,redis 如今的角色是一个master 启动的服务。
和上面配置 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 6379
咱们能够在配置文件中直接修改 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>
② 启动redis 服务:
[root@yunwei ~] # redis-server redis.conf 进入redis: redis-cli -p 6379
使用==info命令==,查看一下slave主机的状态:
# Replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:71 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
咱们能够看到,如今的redis 是一个从服务的角色,链接着6379的服务。接下来咱们再来看一下目前master 的状态:
# Replication role:master connected_slaves:2 slave0:ip=192.168.11.129,port=6379,state=online,offset=785,lag=0 slave1:ip=192.168.11.130,port=6379,state=online,offset=785,lag=0 master_repl_offset:785 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:784
咱们若是须要设置读写分离,只须要在主服务器中设置:
# 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
Redis01/Redis02/Redis03 => redis.conf bind 0.0.0.0 ... protected-mode no => 哨兵必须配置protected-mode,外部网络链接redis服务3、Sentinel哨兵
在sentinel.conf 配置文件中, 咱们能够找到port 属性,这里是用来设置sentinel 的端口,通常状况下,至少会须要三个哨兵对 redis 进行监控。
# port <sentinel-port> # The port that this sentinel instance will run on port 26379
在slave从服务器上,咱们把须要设置主服务器的IP和端口,而且加上权值为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 192.168.11.128 6379 2
[root@yunwei ~] # redis-sentinel sentinel.conf
sentinel 启动以后,就会监视到如今有一个主服务器,两个从服务器。
redis-sentinel启动警告问题:WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
翻译:对一个高负载的环境来讲tcp设置128这个值,过小了。
解决方案:
echo 511 > /proc/sys/net/core/somaxconn
命令就把这个问题解决了。可是这个只是暂时的。若是想要永久解决,打开/etc/sysctl.conf,在这里面添net.core.somaxconn=1024 而后执行sysctl -p 就能够永久消除这个warning
咱们手动关闭Master 以后,sentinel 在监听master 确实是断线了以后,将会开始计算权值,而后从新分配主服务器。
128799:X 29 May 12:08:35.657# +failover-end master mymaster 192.168.11.128 6379 128799:X 29 May 12:08:35.657# +switch-master mymaster 192.168.11.128 6379 192.168.11.130 6379
你们可能会好奇,若是master 重连以后,会不会抢回属于他的位置,答案是否认的,就好比你被一个小弟抢了你老大的位置,他肯给回你这个位置吗。所以当master回来以后,他也只能当个小弟。
① Master 状态监测
② 若是Master 异常,则会进行Master-slave 转换,将其中一个Slave做为Master,将以前的Master做为Slave
③ Master-Slave切换后,master_redis.conf、slave_redis.conf和sentinel.conf的内容都会发生改变,即master_redis.conf中会多一行slaveof的配置,sentinel.conf的监控目标会随之调换