• Redis 5 配置 Redis sentinel(哨兵模式)


    先了解一下哨兵都 做了什么工作:Redis 的 Sentinel 系统用于管理多个 Redis 服务器(instance), 该系统执行以下三个任务:
    
    * 监控(Monitoring): Sentinel 会不断地检查你的主服务器和从服务器是否运作正常。
    * 提醒(Notification): 当被监控的某个 Redis 服务器出现问题时, Sentinel 可以通过 API 向管理员或者其他应用程序发送通知。
    * 自动故障迁移(Automatic failover): 当一个主服务器不能正常工作时, Sentinel 会开始一次自动故障迁移操作, 它会将失效主服务器的其中一个从服务器升级为新的主服务器, 并让失效主服务器的其他从服务器改为复制新的主服务器; 当客户端试图连接失效的主服务器时, 集群也会向客户端返回新主服务器的地址, 使得集群可以使用新主服务器代替失效服务器。
       
    Redis Sentinel 是一个分布式系统, 你可以在一个架构中运行多个 Sentinel 进程(progress), 这些进程使用流言协议(gossip protocols)来接收关于主服务器是否下线的信息, 并使用投票协议(agreement protocols)来决定是否执行自动故障迁移, 以及选择哪个从服务器作为新的主服务器。
    
    Redis Sentinel (哨兵)存在一个单独的可执行文件 redis-sentinel , 但实际上它只是一个运行在特殊模式下的 Redis 服务器,通过Redis的哨兵去监控一个redis的集群,如果集群出现故障则自动进行故障迁移。
    
    # 对于Redis Sentinel有两种启动方式,如下: 
        对于 redis-sentinel 程序, 你可以用以下命令来启动 Sentinel 系统:
    
    redis-sentinel /path/to/sentinel.conf
      对于 redis-server 程序, 你可以用以下命令来启动一个运行在 Sentinel 模式下的 Redis 服务器:
    
    redis-server /path/to/sentinel.conf --sentinel
    

    注意: 配置哨兵的前提是主从要先配置完成并运行。

    主机名      IP地址         redis端口     哨兵端口
    redis-1    10.10.120.113     6379         26379  
    redis-2    10.10.116.206     6379         26379  
    redis-3    10.10.48.62       6379         26379  
    

    主从配置与维护:

    一、配置redis集群
    **** 使用哨兵模式 先要搭建redis主从。****

    ### redis  配置文件修改:
    ## redis-1 Master 配置文件修改:
    #bind 10.10.120.113          # 关闭了保护模式,这行就可以不需要。
    daemonize yes               # 默认为no 一定要打开
    protected-mode no           # 默认为yes,这里一定要改成no。关闭保护模式
    port 6379
    logfile "/data/logs/redis.logs"
    databases 16
    requirepass 123456    # 从服务密码设置(访问本机数据连接的Auth密码)
    masterauth 123456     # 若主服务设置了密码需要加上,在设置哨兵时主从之间连接需要(变更主从需要连接master 的密码.建议主从2个选项都设置上)
    maxclients 10000
    appendonly yes                         # 本地最好打开AOF模式,不会因为重启丢失数据。
    appendfilename "appendonly.aof"
    
    
    ## redis-2 slave 配置文件修改:
    # config:
    daemonize yes 
    protected-mode no
    port 6379
    logfile "/data/logs/redis.logs"
    slaveof  10.10.120.113 6379              #Redis主节点IP  端口
    requirepass 123456                # 客户端连接redis需要用到的密码
    masterauth 123456                 #  从库连接主库用到的密码,类似mysql的主从同步账号密码。
    appendonly yes                         # 本地最好打开AOF模式,不会因为重启丢失数据。
    appendfilename "appendonly.aof"
    
    
    ***  一个主从有以下信息既可以成功建立:
    slaveof  10.10.120.113 6379              #Redis主节点IP  端口
    masterauth 123456  
    
    
    ## 验证主从:
    
    [root@h-1 ~]# redis-cli -a 123456
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    127.0.0.1:6379> info Replication
    # Replication
    role:master
    connected_slaves:2
    slave0:ip=10.10.116.206,port=6379,state=online,offset=1932,lag=0
    slave1:ip=10.10.48.62,port=6379,state=online,offset=1932,lag=0
    master_replid:57609f7b3e89bb5351ade82b965a9a9df0453bba
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:1932
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:1932
    127.0.0.1:6379> 
    
    
    

    哨兵配置与维护:

    如果要做 自动故障转移,建议所有的 redis.conf 都设置 masterauth。因为 自动故障 只会重写 主从关系,即 slaveof,不会自动写入 masterauth。如果 Redis 原本没有设置密码,则可以忽略。

    redis-1 sentinel 配置:

    ### sentinel 配置文件修改:
    # sentinel-1 
    port 26379
    daemonize yes                                         # 
    pidfile "/var/run/redis-sentinel.pid"
    logfile "/data/logs/sentinel.logs"
    dir "/tmp"
    sentinel myid 300fcc98aae7579f4f5f687454cfcc4787446e74
    sentinel deny-scripts-reconfig yes
    sentinel monitor mymaster 10.10.120.113 6379 1        # monitor 监控master IP地址和端口,最后的数字1 是有几个哨兵确认即确认主下线。
    sentinel auth-pass mymaster 123456                    # 重点改这个选项,连接主的密码。
    sentinel config-epoch mymaster 24
    sentinel leader-epoch mymaster 24
    protected-mode no 
    sentinel down-after-milliseconds mymaster 5000         #修改心跳为5000毫秒
    sentinel current-epoch 24
    

    redis-2 sentinel 配置:

    # sentinel-2 
    port 26379
    daemonize yes
    pidfile "/var/run/redis-sentinel.pid"
    logfile "/data/logs/sentinel.logs"
    dir "/tmp"
    sentinel myid 300fcc98aae7579f4f5f687454cfcc4787446e74
    sentinel deny-scripts-reconfig yes
    sentinel monitor mymaster 10.10.120.113 6379 1
    sentinel auth-pass mymaster 123456 
    sentinel config-epoch mymaster 24
    sentinel leader-epoch mymaster 24
    protected-mode no
    sentinel down-after-milliseconds mymaster 5000         #修改心跳为5000毫秒
    sentinel current-epoch 24
    
    
    

    redis-3 sentinel 配置:

    # sentinel-3 
    port 26379
    daemonize yes
    pidfile "/var/run/redis-sentinel.pid"
    logfile "/data/logs/sentinel.logs"
    dir "/tmp"
    sentinel myid edf0547582d9358fa95c6b4711945265b5ffa8a1
    sentinel deny-scripts-reconfig yes
    sentinel monitor mymaster 10.10.120.113 6379 1
    sentinel auth-pass mymaster 123456 
    sentinel config-epoch mymaster 24
    sentinel leader-epoch mymaster 24
    protected-mode no
    sentinel current-epoch 24
    sentinel down-after-milliseconds mymaster 5000  #修改心跳为5000毫秒
    
    

    安全性
    对于数据比较重要的节点,主节点会通过设置requirepass参数进行密码 验证,这时所有的客户端访问必须使用auth命令实行校验。从节点与主节点 的复制连接是通过一个特殊标识的客户端来完成,因此需要配置从节点的masterauth参数与主节点密码保持一致,这样从节点才可以正确地连接到主 节点并发起复制流程。

    验证sentinel 结果:

    sentinel info信息:

    [root@h-1 ~]# redis-cli -p 26379
    127.0.0.1:26379> info sentinel
    # Sentinel
    sentinel_masters:1
    sentinel_tilt:0
    sentinel_running_scripts:0
    sentinel_scripts_queue_length:0
    sentinel_simulate_failure_flags:0
    master0:name=mymaster,status=ok,address=10.10.50.119:6379,slaves=2,sentinels=3
    127.0.0.1:26379> 
    
    

    查看monitor 信息,包含publish ping info 等信息:

    
    [root@h-1 ~]# redis-cli -p 6379 -a 123456 
    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
    127.0.0.1:6379> monitor
    OK
    1575018698.113399 [0 10.10.50.119:46434] "PING"
    1575018698.243075 [0 10.10.50.119:46434] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26380,300fcc98aae7579f4f5f687454cfcc4787446e74,25,mymaster,10.10.50.119,6380,25"
    1575018698.243192 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26380,300fcc98aae7579f4f5f687454cfcc4787446e74,25,mymaster,10.10.50.119,6380,25"
    1575018698.539274 [0 10.10.50.119:46436] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26379,6654332ff2807ba9ac514f81f24b7dc261f12658,25,mymaster,10.10.50.119,6380,25"
    1575018698.539447 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26379,6654332ff2807ba9ac514f81f24b7dc261f12658,25,mymaster,10.10.50.119,6380,25"
    1575018698.572941 [0 10.10.50.119:46422] "PING"
    1575018698.572949 [0 10.10.50.119:46422] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26381,edf0547582d9358fa95c6b4711945265b5ffa8a1,25,mymaster,10.10.50.119,6380,25"
    1575018698.860119 [0 10.10.50.119:46436] "PING"
    1575018699.127153 [0 10.10.50.119:46434] "PING"
    1575018699.587644 [0 10.10.50.119:46422] "PING"
    1575018699.796480 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26381,edf0547582d9358fa95c6b4711945265b5ffa8a1,25,mymaster,10.10.50.119,6380,25"
    1575018699.888122 [0 10.10.50.119:46436] "PING"
    1575018700.175129 [0 10.10.50.119:46434] "PING"
    1575018700.299961 [0 10.10.50.119:46434] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26380,300fcc98aae7579f4f5f687454cfcc4787446e74,25,mymaster,10.10.50.119,6380,25"
    1575018700.299989 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26380,300fcc98aae7579f4f5f687454cfcc4787446e74,25,mymaster,10.10.50.119,6380,25"
    1575018700.300194 [0 10.10.50.119:46422] "INFO"
    1575018700.569205 [0 10.10.50.119:6380] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26379,6654332ff2807ba9ac514f81f24b7dc261f12658,25,mymaster,10.10.50.119,6380,25"
    1575018700.569235 [0 10.10.50.119:46436] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26379,6654332ff2807ba9ac514f81f24b7dc261f12658,25,mymaster,10.10.50.119,6380,25"
    1575018700.622766 [0 10.10.50.119:46422] "PING"
    1575018700.622785 [0 10.10.50.119:46422] "PUBLISH" "__sentinel__:hello" "10.10.50.119,26381,edf0547582d9358fa95c6b4711945265b5ffa8a1,25,mymaster,10.10.50.119,6380,25"
    
    
    
    
  • 相关阅读:
    BZOJ3123: [Sdoi2013]森林(启发式合并&主席树)
    BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)
    BZOJ3211: 花神游历各国(线段树)
    BZOJ3207: 花神的嘲讽计划Ⅰ(hash)
    BZOJ3033: 太鼓达人(欧拉回路)
    「LibreOJ NOIP Round #1」旅游路线
    bzoj 2818: Gcd
    bzoj 2006: [NOI2010]超级钢琴
    bzoj 1485: [HNOI2009]有趣的数列
    bzoj 1045: [HAOI2008] 糖果传递
  • 原文地址:https://www.cnblogs.com/zhenxing06/p/12840250.html
Copyright © 2020-2023  润新知