redis 的高可用

Redis高可用

为了保证redis最大程度上能够使用,redis提供了主从同步+Sentinel哨兵机制。

Sentinel 哨兵

下面是官网地址:https://redis.io/topics/sentinel

redis提供的哨兵是用来看护redis实例进程的,可以自动进行故障转移,其功能如下:

  • Monitoring. Sentinel constantly checks if your master and replica instances are working as expected.
  • Notification. Sentinel can notify the system administrator, or other computer programs, via an API, that something is wrong with one of the monitored Redis instances.
  • Automatic failover. If a master is not working as expected, Sentinel can start a failover process where a replica is promoted to master, the other additional replicas are reconfigured to use the new master, and the applications using the Redis server are informed about the new address to use when connecting.
  • Configuration provider. Sentinel acts as a source of authority for clients service discovery: clients connect to Sentinels in order to ask for the address of the current Redis master responsible for a given service. If a failover occurs, Sentinels will report the new address.

  即:

  • 监测。Sentinel不断检查主实例和从实例是否按预期工作。
  • 通知。Sentinel可以通过API通知系统管理员和其他计算机程序,其中一个被监视的Redis实例有问题。
  • 自动故障切换。如果主设备不能按预期工作,Sentinel可以启动一个故障转移过程,其中一个从设备升级为主设备,其他附加的从设备被重新配置为使用新的主设备,并且使用Redis服务器的应用程序在连接时被告知要使用的新地址。
  • 配置提供程序。Sentinel充当客户端服务发现的授权源:客户端连接到Sentinel,以请求负责给定服务的当前Redis主机的地址。如果发生故障转移,哨兵将报告新地址

 

在redis安装后,会自带sentinel哨兵程序,

 

修改sentinel.conf配置文件

bind 127.0.0.1
port 26380
daemonize yes
logfile /var/log/redis-sentinel.log
sentinel monitor mymaster 127.0.0.1 6380 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
  • sentinel monitor mymaster 127.0.0.1 6380 2 说明
    • mymaster 为sentinel监护的redis主从集群起名
    • 127.0.0.1 6300 为主从中任一台机器地址
    • 2 表示有两台以的sentinel认为某一台redis宕机后,才会进行自动故障转移。

启动方式:

redis-sentinel sentinel.conf

高可用方案注意事项

  • 至少三个sentinel以上
  • sentinel要分散运行在不同的机器上

测试

1:查看所有redis进程

[root@peiqi bin]# ps aux | grep redis
root       1714  0.1  0.2 147460 10104 ?        Sl   08:53   0:24 redis-server 192.168.50.129:6379
root       1753  0.1  0.2 147460 10092 ?        Sl   08:54   0:24 redis-server 192.168.50.129:6378
root       1792  0.1  0.3 149508 11884 ?        Ssl  08:54   0:24 redis-server 192.168.50.129:7001 [cluster]
root       1797  0.1  0.3 149508 11908 ?        Ssl  08:55   0:24 redis-server 192.168.50.129:7002 [cluster]
root       1802  0.1  0.3 149508 11900 ?        Ssl  08:55   0:24 redis-server 192.168.50.129:7003 [cluster]
root      44789  0.1  0.3 149508 11916 ?        Ssl  10:00   0:21 redis-server 192.168.50.129:7004 [cluster]
root      44794  0.1  0.3 149508 11916 ?        Ssl  10:01   0:20 redis-server 192.168.50.129:7005 [cluster]
root      44812  0.1  0.3 149508 11948 ?        Ssl  10:01   0:20 redis-server 192.168.50.129:7006 [cluster]
root      45446  0.2  0.2 145412  7752 ?        Ssl  15:31   0:00 redis-sentinel 192.168.50.129:26379 [sentinel]
root      45451  0.3  0.2 145412  7748 ?        Ssl  15:31   0:00 redis-sentinel 192.168.50.129:26380 [sentinel]
root      45456  1.0  0.2 145412  7704 ?        Ssl  15:31   0:00 redis-sentinel 192.168.50.129:26381 [sentinel]
root      45461  0.0  0.0 112812   976 pts/2    S+   15:31   0:00 grep --color=auto redis
[root@peiqi bin]

2:进入主节点,查看主/从复制信息(主节点)

[root@peiqi bin]# redis-cli -h 192.168.50.129 -p 6379
192.168.50.129:6379> info Replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.50.129,port=6378,state=online,offset=62446,lag=1
master_replid:551ecb2b80a1cf9de0b97f73ee69fb2cd5128724
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:62732
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:62732
192.168.50.129:6379>

2:进入从节点,查看主/从复制信息(从节点)

[root@peiqi cluster]# redis-cli -h 192.168.50.129 -p 6378
192.168.50.129:6378> info Replication
# Replication
role:slave
master_host:192.168.50.129
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:71511
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:551ecb2b80a1cf9de0b97f73ee69fb2cd5128724
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:71511
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:71511
192.168.50.129:6378>

3:关掉主节点,查看从节点是自动切换为主节点(这儿要稍微等会,因为配有时间参数)

192.168.50.129:6379> shutdown
not connected>

等会参看从从节点(已将从节点切换为主节点),当开启主节点后,从节点又开始切换回从节点(这是网上说的,但我实践出来的理论是会把原来的主节点变成从节点)

192.168.50.129:6378> info Replication
# Replication
role:master
connected_slaves:0
master_replid:e8b08994005adae4e0520482ac7a08af66a91352
master_replid2:551ecb2b80a1cf9de0b97f73ee69fb2cd5128724
master_repl_offset:146098
second_repl_offset:144360
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:146098

注意:这儿从节点切换为主节的时候会有个个问题!

因为从节点只能都信息,为不能写信息,这是在python中的处理方式如下:

Python客户端使用

# redis 哨兵
REDIS_SENTINELS = [
    (127.0.0.1, 26380),
    (127.0.0.1, 26381),
    (127.0.0.1, 26382),
]
REDIS_SENTINEL_SERVICE_NAME = mymaster

from redis.sentinel import Sentinel
_sentinel = Sentinel(REDIS_SENTINELS)
redis_master = _sentinel.master_for(REDIS_SENTINEL_SERVICE_NAME)
redis_slave = _sentinel.slave_for(REDIS_SENTINEL_SERVICE_NAME)
使用示例

# 读数据,master读不到去slave读
try:
    real_code = redis_master.get(key)
except ConnectionError as e:
    real_code = redis_slave.get(key)

# 写数据,只能在master里写
try:
    current_app.redis_master.delete(key)
except ConnectionError as e:
    logger.error(e)

 

  

 

 

 

  

 

 

 

 

 

 

redis 的高可用

上一篇:iOS之禁止所有输入法的表情


下一篇:appium的截图