• redis 系列25 哨兵Sentinel (高可用演示 下)


    一. Sentinel 高可用环境准备

      1.1 Sentinel 集群环境

    环境 说明
    操作系统版本 CentOS  7.4.1708 
    IP地址 172.168.18.200
    网关Gateway 172.168.18.1
    DNS 172.168.16.11
    三个sentinel服务端口 26379,26380,26381
    Sentinel密码 无 不设置
    是否RDB持久化 不支持
    是否 AOF持久化 不支持

      1.2 Redis主库库环境,主从库搭建在(redis 系列22 复制Replication 下)

    主库ip 172.168.18.201 6379
    从库ip 172.168.18.203 6379,  172.168.18.200 6379

    二.  Sentinel 配置说明

      2.1 启动Sentinel服务方法

        对于启动Sentinel服务有二种方法:

        (1)是使用redis-sentinel程序来启动 redis-sentinel  sentinel.conf。

        (2)是使用redis-server 程序来启动一个运行在Sentinel模式下的Redis服务器 redis-server  sentinel.conf  --sentinel。

        启动 Sentinel 实例必须指定相应的配置文件, 系统会使用配置文件来保存 Sentinel 的当前状态, 并在 Sentinel 重启时通过载入配置文件来进行状态还原。查看redis-sentinel程序,只是一个软链接,如下所示:

        lrwxrwxrwx. 1 root root      12 12月 18 16:30 redis-sentinel -> redis-server     

      2.2  sentinel.conf 参数说明

        下面解说sentinel.conf文件中,所需的至少配置参数描述:

        -- 监控主库, 名称:mymaster可以自定义, IP端口: 127.0.0.1 6379,判断主库客观下线需要2个Sentinel 同意
            sentinel monitor mymaster 127.0.0.1 6379 2
    
        -- 认为主库已经下线所需的毫秒数,例如下线状态超过60000则判定已经下线。
        sentinel down-after-milliseconds mymaster 60000
    
        -- 指定故障转移超时时间,以毫秒为单位,配置所有slaves指向新的master所需的最大时间
        sentinel failover-timeout mymaster 180000
    
        -- 在执行故障转移时, 最多可以有多少个从服务器同时对新的主服务器进行同步,这个值设为 1 来保证每次只有一个slave 处于不能处理命令请求的状态。如果这个数字越大,就意味着越 多的slave因为replication而不可用。
        parallel-syncs mymaster 1
            
        --设置连接master的密码。
        sentinel auth-pass mymaster 123456

    三.  Sentinel高可用搭建

      只使用单个Sentinel进程来监控redis集群是不可靠的,当单个Sentinel进程down后,整个集群系统将无法按照预期的方式运行。所以有必要将sentinel集群,在IP 200的电脑上将启动三个Sentinel进程,实现集群。

      3.1  添加3个Sentinel.conf文件

        在ip 为200的sentinel集群服务器上,在redis运行目录下,增加3个配置文件,名称分别为:Sentinel_26379.conf, Sentinel_26380.conf, Sentinel_26381.conf。相关脚本如下:

        --  Sentinel_26379.conf文件配置参数
        protected-mode no
        port 26379
        sentinel monitor mymaster 172.168.18.201  6379 2
        sentinel auth-pass mymaster 123456
        daemonize yes
        logfile "/usr/local/redis/bin/sentinel_26379.log"
        sentinel down-after-milliseconds mymaster 30000
        sentinel parallel-syncs mymaster 1
        sentinel failover-timeout mymaster 180000
    
        --  Sentinel_26380.conf文件配置参数如下,其它参数与Sentinel_26379文件一样
        port 26380
        logfile "/usr/local/redis/bin/sentinel_26380.log"
    
        
        --  Sentinel_26381.conf文件配置参数如下,其它参数与Sentinel_26379文件一样
        port 26381
        logfile "/usr/local/redis/bin/sentinel_26381.log"

      --增加后文件目录如下:

    [root@localhost bin]# pwd
    /usr/local/redis/bin
    [root@localhost bin]# ls -l
    总用量 22012
    -rw-r--r--. 1 root root      92 12月 18 16:38 dump.rdb
    -rw-r--r--. 1 root root   57765 12月 18 16:36 redis_bak.conf
    -rwxr-xr-x. 1 root root 2452648 12月 18 16:30 redis-benchmark
    -rwxr-xr-x. 1 root root 5754888 12月 18 16:30 redis-check-aof
    -rwxr-xr-x. 1 root root 5754888 12月 18 16:30 redis-check-rdb
    -rwxr-xr-x. 1 root root 2617840 12月 18 16:30 redis-cli
    -rw-r--r--. 1 root root   57762 12月 20 14:22 redis.conf
    lrwxrwxrwx. 1 root root      12 12月 18 16:30 redis-sentinel -> redis-server
    -rwxr-xr-x. 1 root root 5754888 12月 18 16:30 redis-server
    -rw-r--r--. 1 root root    7992 12月 20 14:22 sentinel_26379.conf
    -rw-r--r--. 1 root root    6887 12月 20 14:23 sentinel_26379.log
    -rw-r--r--. 1 root root    7992 12月 20 14:22 sentinel_26380.conf
    -rw-r--r--. 1 root root    7081 12月 20 14:23 sentinel_26380.log
    -rw-r--r--. 1 root root    7992 12月 20 14:22 sentinel_26381.conf
    -rw-r--r--. 1 root root    8465 12月 20 14:23 sentinel_26381.log
    -rw-r--r--. 1 root root    7710 12月 19 14:21 sentinel.conf
    View Code  

      3.2 启动三个sentinel服务

         [root@localhost bin]# pwd
        /usr/local/redis/bin
        [root@localhost bin]# ./redis-sentinel ./sentinel_26379.conf
        [root@localhost bin]# ./redis-sentinel ./sentinel_26380.conf
        [root@localhost bin]# ./redis-sentinel ./sentinel_26381.conf

      (1)查看进程信息

    [root@localhost bin]# ps -ef | grep redis-sentinel
    root       7567      1  0 14:28 ?        00:00:00 ./redis-sentinel *:26379 [sentinel]
    root       7572      1  0 14:28 ?        00:00:00 ./redis-sentinel *:26380 [sentinel]
    root       7577      1  0 14:28 ?        00:00:00 ./redis-sentinel *:26381 [sentinel]
    View Code

      (2)查看主库与sentinel关联信息(连接一个sentinel客户端)

    [root@localhost bin]# ./redis-cli -h 172.168.18.200 -p 26381
    172.168.18.200:26381> sentinel master mymaster
     1) "name"
     2) "mymaster"
     3) "ip"
     4) "172.168.18.201"
     5) "port"
     6) "6379"
     7) "runid"
     8) "26cd40ba173490e2ceac61433211af7dc7716dda"
     9) "flags"
    10) "master"
    11) "link-pending-commands"
    12) "0"
    13) "link-refcount"
    14) "1"
    15) "last-ping-sent"
    16) "0"
    17) "last-ok-ping-reply"
    18) "169"
    19) "last-ping-reply"
    20) "169"
    21) "down-after-milliseconds"
    22) "30000"
    23) "info-refresh"
    24) "3982"
    25) "role-reported"
    26) "master"
    27) "role-reported-time"
    28) "24109"
    29) "config-epoch"
    30) "0"
    31) "num-slaves"
    32) "2"
    33) "num-other-sentinels"
    34) "2"
    35) "quorum"
    36) "2"
    37) "failover-timeout"
    38) "180000"
    39) "parallel-syncs"
    40) "1"
    View Code

      (3)sentinel客户端查看群集信息,可以看到此时主库ip为201

        172.168.18.200:26380> 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=172.168.18.201:6379,slaves=2,sentinels=3

      

    四.Sentinel高可用测试

      4.1 测试主从同步

        -- 主库写入一个键值对
        [root@hsr bin]# ./redis-cli -h 172.168.18.201 -p 6379 -a 123456
        172.168.18.201:6379> set mysentinel  "hello"
        OK
        -- 从库203 读取了该键
        [root@xuegod64 redis-4.0.6]# redis-cli -h 172.168.18.203 -p 6379 -a 123456
        172.168.18.203:6379> get mysentinel
        "hello"
        -- 从库200 读取了该键
        [root@localhost bin]# ./redis-cli -h 172.168.18.200 -p 6379 -a     123456
        172.168.18.200:6379> get mysentinel
        "hello"

      4.2 测试故障转移

        (1)  首先把主库201的down掉

        172.168.18.201:6379> shutdown
        not connected>

        (2) 在sentinel客户端查看群集信息,发现此时已经实现了故障转移,已经将从库 200 升级成为了新主库

        172.168.18.200:26381> 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=172.168.18.200:6379,slaves=2,sentinels=3

        (3) 在redis客户端,查看ip 200的复制信息,角色已成了为master

        172.168.18.200:6379> info replication
        # Replication
        role:master
        connected_slaves:1
        slave0:ip=172.168.18.203,port=6379,state=online,offset=204170,lag=0
        master_replid:7464817ee3337cc8f2b508577287b0f0c385a859
        master_replid2:0000000000000000000000000000000000000000
        master_repl_offset:204170
        second_repl_offset:-1
        repl_backlog_active:1
        repl_backlog_size:1048576
        repl_backlog_first_byte_offset:190908
        repl_backlog_histlen:13263

        (4)此时ip200 的redis服务,由之前的只读,变成了可读写。 

        172.168.18.200:6379> set mastername  "ip200"
        OK
        --此时只有203一个从库,成功读取了该键
        172.168.18.203:6379> get mastername
        "ip200"

         (5)查看其中的一个sentinel日志,下面是关于故障转移的相关信息:

    [root@localhost bin]# cat sentinel_26379.log
    8516:X 20 Dec 14:22:31.394 # +sdown master mymaster 172.168.18.201 6379
    8516:X 20 Dec 14:22:31.496 # +new-epoch 1
    8516:X 20 Dec 14:22:31.499 # +vote-for-leader 300fd3d5b5673885c17942c465ec7a09f8f8e2ad 1
    8516:X 20 Dec 14:22:32.271 # +config-update-from sentinel 300fd3d5b5673885c17942c465ec7a09f8f8e2ad 172.168.18.200 26381 @ mymaster 172.168.18.201 6379
    8516:X 20 Dec 14:22:32.272 # +switch-master mymaster 172.168.18.201 6379 172.168.18.200 6379
    8516:X 20 Dec 14:22:32.272 * +slave slave 172.168.18.203:6379 172.168.18.203 6379 @ mymaster 172.168.18.200 6379
    8516:X 20 Dec 14:22:32.272 * +slave slave 172.168.18.201:6379 172.168.18.201 6379 @ mymaster 172.168.18.200 6379
    8516:X 20 Dec 14:23:02.323 # +sdown slave 172.168.18.201:6379 172.168.18.201 6379 @ mymaster 172.168.18.200 6379

      总结:sentinel高可用是基于复制来实现的。在sentinel实现过程中:首先要先搭建好复制架构,并确保数据同步正常运行;最后在复制基础上,再搭建sentinel群集服务架构,并测试好故障转移切换。

      

  • 相关阅读:
    【Linux安全】安全口令策略设置
    【Linux安全】防止 root 用户远程登录
    【Linux安全】防止任意用户使用 su 切换到 root
    /etc/passwd 结构
    【安全组网】思科IOS设备基础应用
    关于Apache Struts 2 S2-032高危漏洞的一些确认
    [企业级linux安全管理]- 主机安全管理
    [企业级linux安全管理]- 系统日志管理
    [企业级linux安全管理]- 安全管理基础(1)
    编码识别工具:hash-identifier
  • 原文地址:https://www.cnblogs.com/MrHSR/p/10144523.html
Copyright © 2020-2023  润新知