• 主从模式和哨兵部署


    一、Redis主从模式配置

    1.1、案例拓扑

     

    1.2、环境说明

    主机名称

    IP地址

    redis版本和角色说明

    master

    20.0.0.10

    redis 5.0.7(主)

    slave1

    20.0.0.20

    redis 5.0.7(从)

    slave2

    20.0.0.30

    redis 5.0.7(从)

    1.3、Redis安装

    所有服务器上安装,在master上演示

    [root@master ~]# tar zxf redis-5.0.7.tar.gz
    [root@master ~]# cd redis-5.0.7/
    [root@master redis-5.0.7]# make -j2
    [root@master redis-5.0.7]# make PREFIX=/usr/local/redis install
    [root@master redis-5.0.7]# ln -s /usr/local/redis/bin/* /usr/local/bin/
    [root@master redis-5.0.7]# cd utils/
    [root@master utils]# ./install_server.sh
    Welcome to the redis service installer
    This script will help you easily set up a running redis server
    
    Please select the redis port for this instance: [6379]
    Selecting default: 6379
    Please select the redis config file name [/etc/redis/6379.conf]
    Selected default - /etc/redis/6379.conf
    Please select the redis log file name [/var/log/redis_6379.log]
    Selected default - /var/log/redis_6379.log
    Please select the data directory for this instance [/var/lib/redis/6379]
    Selected default - /var/lib/redis/6379
    Please select the redis executable path [/usr/local/bin/redis-server]
    Selected config:
    Port           : 6379
    Config file    : /etc/redis/6379.conf
    Log file       : /var/log/redis_6379.log
    Data dir       : /var/lib/redis/6379
    Executable     : /usr/local/bin/redis-server
    Cli Executable : /usr/local/bin/redis-cli
    Is this ok? Then press ENTER to go on or Ctrl-C to abort.
    Copied /tmp/6379.conf => /etc/init.d/redis_6379
    Installing service...
    Successfully added to chkconfig!
    Successfully added to runlevels 345!
    Starting Redis server...
    Installation successful!
    

    1.4、修改配置文件

    1.4.1、master配置文件修改

    [root@master src]# vim /etc/redis/6379.conf
    #69行 修改监听地址为20.0.0.10(在实验环境使用),现网环境建议绑定从服务器IP地址
    bind 20.0.0.10
    #136行 开启守护进程
    daemonize yes
    #171行 修改日志文件目录
    logfile /var/log/redis_6379.log
    #263行 修改工作目录
    dir /var/lib/redis/6379
    #699行 开启AOF持久化功能
    appendonly yes
    
    [root@master utils]# /etc/init.d/redis_6379 restart      #开启Redis
    
    [root@master utils]# netstat -anpt | grep redis
    tcp        0      0 20.0.0.10:6379          0.0.0.0:*               LISTEN      57685/redis-server
    

    1.4.2、slave1配置文件修改

    [root@slave1 src]# vim /etc/redis/6379.conf
    #69行 修改监听地址为20.0.0.10(在实验环境使用),现网环境建议绑定从服务器IP地址
    bind 20.0.0.20
    #136行 开启守护进程
    daemonize yes
    #171行 修改日志文件目录
    logfile /var/log/redis_6379.log
    #263行 修改工作目录
    dir /var/lib/redis/6379
    #699行 开启AOF持久化功能
    appendonly yes
    #287行  修改IP和端口
    replicaof 20.0.0.10 6379
    
    [root@slave1 utils]# /etc/init.d/redis_6379 restart      #开启Redis
    
    [root@slave1 utils]# netstat -anpt | grep redis          #查看端口号
    tcp        0      0 20.0.0.20:6379          0.0.0.0:*               LISTEN      19037/redis-server
    tcp        0      0 20.0.0.20:34397         20.0.0.10:6379          ESTABLISHED 19037/redis-server
    

    1.4.2、slave2配置文件修改  

     

    [root@slave2 src]# vim /etc/redis/6379.conf
    #69行 修改监听地址为20.0.0.10(在实验环境使用),现网环境建议绑定从服务器IP地址
    bind 20.0.0.30
    #136行 开启守护进程
    daemonize yes
    #171行 修改日志文件目录
    logfile /var/log/redis_6379.log
    #263行 修改工作目录
    dir /var/lib/redis/6379
    #699行 开启AOF持久化功能
    appendonly yes
    #287行  修改IP和端口
    replicaof 20.0.0.10 6379
    
    [root@slave2 utils]# /etc/init.d/redis_6379 restart      #开启Redis
    
    [root@slave2 utils]# netstat -anpt | grep redis
    tcp        0      0 20.0.0.30:6379          0.0.0.0:*               LISTEN      59182/redis-server
    tcp        0      0 20.0.0.30:46648         20.0.0.10:6379          ESTABLISHED 59182/redis-server
    

    1.5、测试效果

    1.5.1、验证主从效果(master上看日志)

    [root@master utils]# vi /var/log/redis_6379.log
    ......省略部分内容
    57685:M 11 Nov 2020 10:51:17.605 * Replica 20.0.0.20:6379 asks for synchronization         #slave1服务器IP
    57685:M 11 Nov 2020 10:51:17.605 * Full resync requested by replica 20.0.0.20:6379
    57685:M 11 Nov 2020 10:51:17.605 * Starting BGSAVE for SYNC with target: disk
    57685:M 11 Nov 2020 10:51:17.606 * Background saving started by pid 57691
    57691:C 11 Nov 2020 10:51:17.607 * DB saved on disk
    57691:C 11 Nov 2020 10:51:17.607 * RDB: 4 MB of memory used by copy-on-write
    57685:M 11 Nov 2020 10:51:17.658 * Background saving terminated with success
    57685:M 11 Nov 2020 10:51:17.658 * Synchronization with replica 20.0.0.20:6379 succeeded
    57685:M 11 Nov 2020 10:55:07.287 * Replica 20.0.0.30:6379 asks for synchronization          #slave2服务器IP
    57685:M 11 Nov 2020 10:55:07.287 * Full resync requested by replica 20.0.0.30:6379
    57685:M 11 Nov 2020 10:55:07.287 * Starting BGSAVE for SYNC with target: disk
    57685:M 11 Nov 2020 10:55:07.288 * Background saving started by pid 57734
    57734:C 11 Nov 2020 10:55:07.290 * DB saved on disk
    57734:C 11 Nov 2020 10:55:07.290 * RDB: 4 MB of memory used by copy-on-write
    57685:M 11 Nov 2020 10:55:07.372 * Background saving terminated with success
    57685:M 11 Nov 2020 10:55:07.372 * Synchronization with replica 20.0.0.30:6379 succeeded
    

    1.5.2、master上验证从节点 

    [root@master utils]# redis-cli -h 20.0.0.10 -p 6379
    20.0.0.10:6379> info replication
    # Replication
    role:master
    connected_slaves:2
    slave0:ip=20.0.0.20,port=6379,state=online,offset=2702,lag=0
    slave1:ip=20.0.0.30,port=6379,state=online,offset=2702,lag=1
    

    1.5.3、插入数据验证

    master上插入数据
    20.0.0.10:6379> set name lisi
    OK
    20.0.0.10:6379> set score 80
    OK
    20.0.0.10:6379> keys *
    1) "score"
    2) "name"
    20.0.0.10:6379> get name
    "lisi"
    20.0.0.10:6379> get score
    "80"
    
    slave1上查看
    [root@slave1 utils]# redis-cli -h 20.0.0.20 -p 6379
    20.0.0.20:6379> keys *
    1) "score"
    2) "name"
    20.0.0.20:6379> get name
    "lisi"
    20.0.0.20:6379> get score
    "80"
    
    slave2上查看
    [root@slave2 utils]# redis-cli -h 20.0.0.30 -p 6379
    20.0.0.30:6379> keys *
    1) "score"
    2) "name"
    20.0.0.30:6379> get name
    "lisi"
    20.0.0.30:6379> get score
    "80"
    

    二、Redis哨兵模式

    2.1、Redis sentinel介绍

    Redis Sentinel是Redis高可用的实现方案。Sentinel是一个管理多个Redis实例的工具,它可以实现对Redis的监控、通知、自动故障转移。

    2.2、哨兵模式的作用

    ① 监控

    不断的检查master和slave是否正常运行。master存活检测、master与slave运行情况检测

    ② 通知(提醒)

    当被监控的服务器出现问题时,向其他(哨兵间,客户端)发送通知。

    ③ 自动故障转移

    断开master与slave连接,选取一个slave作为master,将其他slave连接到新的master,并告知客户端新的服务器地址

    PS:哨兵也是一台redis服务器,只是不提供数据服务

    哨兵的启动依赖于主从模式,所以须把主从模式安装好的情况下再去做哨兵模式,所有节点上都需要部署哨兵模式,哨兵模式会监控所有的redis工作节点是否正常,当

    master出现问题的时候,因为其他节点与主节点失去联系,因此会投票,投票过半就认为这个master的确出现问题,然后会通知哨兵间,然后从slaves中选取一个作为新的

    master,至少需要3个或3个以上服务器

    2.3、环境说明

    主机名称IP地址redis版本和角色说明
    master 20.0.0.10:6379 redis 5.0.7(主)
    slave1 20.0.0.20:6379 redis 5.0.7(从)
    slave2 20.0.0.30:6379 redis 5.0.7(从)
    master 20.0.0.10:26379 Sentinel1
    slave1 20.0.0.20:26379 Sentinel2
    slave2 20.0.0.30:26379 Sentinel3

    2.4、哨兵模式配置

    所有节点都需要修改

    [root@master ~]# vi redis-5.0.7/sentinel.conf
    17行/protected-mode no                                  #关闭保护模式
    26行/daemonize yes                                      #指定sentinel为后台启动
    36行/logfile "/var/log/sentinel.log"                    #指定日志存放路径
    65行/dir "/var/lib/redis/6379"                          #指定数据库存放路径
    84行/sentinel monitor mymaster 20.0.0.10 6379 2         #至少几个哨兵检测到主服务器故障了,才会进行故障迁移,全部指向masterIP
    113行/sentinel down-after-milliseconds mymaster 30000    #判定服务器down掉的时间周期,默认30000毫秒(30秒)
    146行/sentinel failover-timeout mymaster 180000         #故障节的的最大超时时间为180000(180秒)
    

    2.5、启动哨兵模式

    [root@master ~]# redis-sentinel redis-5.0.7/sentinel.conf &
    [1] 58571
    
    [root@slave1 ~]# redis-sentinel redis-5.0.7/sentinel.conf &
    [1] 19812
    
    [root@slave2 ~]# redis-sentinel redis-5.0.7/sentinel.conf &
    [1] 59917
    

    2.6、查看哨兵信息

    master查看
    [root@master ~]# redis-cli -h 20.0.0.10 -p 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=20.0.0.10:6379,slaves=2,sentinels=3
    
    slave1查看
    [root@slave1 ~]# redis-cli -h 20.0.0.20 -p 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=20.0.0.10:6379,slaves=2,sentinels=3
    
    slave2上查看
    [root@slave2 ~]# redis-cli -h 20.0.0.20 -p 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=20.0.0.10:6379,slaves=2,sentinels=3
    

    2.7、故障模拟

    查看master进程号
    [root@master ~]# ps -ef | grep redis
    root      57685      1  0 10:51 ?        00:00:06 /usr/local/bin/redis-server 20.0.0.10:6379
    root      58567      1  0 12:07 ?        00:00:01 redis-sentinel *:26379 [sentinel]
    root      58649  58470  0 12:15 pts/1    00:00:00 grep --color=auto redis
    
    杀死master上redis-server的进程号
    [root@master ~]# kill -9 57685     #master上redis-server的进程号
    

     2.7.2、验证结果

     

    [root@master ~]# tail -f /var/log/sentinel.log
    58567:X 11 Nov 2020 12:18:14.388 # +failover-state-reconf-slaves master mymaster 20.0.0.10 6379
    58567:X 11 Nov 2020 12:18:14.467 * +slave-reconf-sent slave 20.0.0.20:6379 20.0.0.20 6379 @ mymaster 20.0.0.10 6379
    58567:X 11 Nov 2020 12:18:14.776 # -odown master mymaster 20.0.0.10 6379
    58567:X 11 Nov 2020 12:18:15.428 * +slave-reconf-inprog slave 20.0.0.20:6379 20.0.0.20 6379 @ mymaster 20.0.0.10 6379
    58567:X 11 Nov 2020 12:18:15.428 * +slave-reconf-done slave 20.0.0.20:6379 20.0.0.20 6379 @ mymaster 20.0.0.10 6379
    58567:X 11 Nov 2020 12:18:15.483 # +failover-end master mymaster 20.0.0.10 6379
    58567:X 11 Nov 2020 12:18:15.483 # +switch-master mymaster 20.0.0.10 6379 20.0.0.30 6379                        #从master转到了slave2上
    58567:X 11 Nov 2020 12:18:15.483 * +slave slave 20.0.0.20:6379 20.0.0.20 6379 @ mymaster 20.0.0.30 6379
    58567:X 11 Nov 2020 12:18:15.483 * +slave slave 20.0.0.10:6379 20.0.0.10 6379 @ mymaster 20.0.0.30 6379
    58567:X 11 Nov 2020 12:18:45.535 # +sdown slave 20.0.0.10:6379 20.0.0.10 6379 @ mymaster 20.0.0.30 6379
    
    [root@master ~]# redis-cli -p 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=20.0.0.30:6379,slaves=2,sentinels=3 

    重新启动原旧master

    [root@master ~]# /etc/init.d/redis_6379 restart
    Stopping ...
    Could not connect to Redis at 127.0.0.1:6379: Connection refused
    Waiting for Redis to shutdown ...
    Waiting for Redis to shutdown ...
    Waiting for Redis to shutdown ...
    Waiting for Redis to shutdown ...
    Waiting for Redis to shutdown ...
    直接重启无法启动,需要进入/var/run 讲pid文件删除
    [root@master run]# rm -rf  redis_6379.pid
    [root@master run]# /etc/init.d/redis_6379 restart
    /var/run/redis_6379.pid does not exist, process is not running
    Starting Redis server...
    [root@master run]# netstat -anpt | grep redis
    tcp        0      0 20.0.0.10:6379          0.0.0.0:*               LISTEN      60732/redis-server  
    tcp        0      0 20.0.0.10:6379          20.0.0.30:43102         ESTABLISHED 60732/redis-server 
    启动成功
    

      

      

      

      

      

      

     

      

     

      

  • 相关阅读:
    日记 2018/1/12
    【程序员笔试面试必会——排序①】Python实现 冒泡排序、选择排序、插入排序、归并排序、快速排序、堆排序、希尔排序
    Python笔试、面试 【必看】
    高性能Go并发
    Go连接MySql数据库Error 1040: Too many connections错误解决
    MAC 配置文件 ~/.zshrc
    go-statsd项目
    日记 2017.11.20
    sed 命令详解
    Opentsdb简介(一)
  • 原文地址:https://www.cnblogs.com/tianzhendengni/p/13965944.html
Copyright © 2020-2023  润新知