为了保证redis最大程度上能够使用,redis提供了主从同步+Sentinel哨兵机制。
下面是官网地址:https://redis.io/topics/sentinel
redis提供的哨兵是用来看护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
启动方式:
redis-sentinel sentinel.conf
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中的处理方式如下:
# 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)
原文:https://www.cnblogs.com/Live-up-to-your-youth/p/14940793.html