美文网首页
05给女朋友讲讲Redis-集群搭建(哨兵模式)

05给女朋友讲讲Redis-集群搭建(哨兵模式)

作者: XueFengDong | 来源:发表于2021-07-14 15:39 被阅读0次

    一、Redis安装

    准备三台主机,分别安装Redis(本文以redis6.0.6为例)
    我这里:

    10.27.3.121 (主)
    10.27.3.122 (从)
    10.27.3.123 (从)

    1.下载并解压redis安装包

    wget http://download.redis.io/releases/redis-6.0.6.tar.gz
    
    tar -zxvf redis-6.0.6.tar.gz
    

    2.编译安装
    进入解压后的redis目录下

    cd redis-6.0.6
    

    检测是否有gcc环境,若提示无此命令则需要先安装gcc环境(此处gcc版本如果低于5.3,后续编译可能会报错)

    gcc -v
    
    yum install -y gcc
    

    如果gcc低于5.3版本执行以下命令升级

    yum -y install centos-release-scl
    yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
     
    scl enable devtoolset-9 bash
    #注意:scl命令启用只是临时的,推出xshell或者重启就会恢复到原来的gcc版本。
    #如果要长期生效的话,执行如下:
    echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
    
    image.png

    编译到指定目录

    make PREFIX=/usr/local/redis6 install
    

    如果看到此提示,表示编译成功


    image.png

    安装Redis,进入解压后redis目录的utils目录下

    cd utils
    

    执行install_server.sh脚本

    ./install_server.sh
    

    如果报下图错误


    image.png

    打开install_server.sh,注释掉下面的内容:

    #_pid_1_exe="$(readlink -f /proc/1/exe)"
    #if [ "${_pid_1_exe##*/}" = systemd ]
    #then
    #   echo "This systems seems to use systemd."
    #   echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
    #   exit 1
    #fi
    #unset _pid_1_exe
    

    重新执行

    [root@localhost 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] 16379
    Please select the redis config file name [/etc/redis/16379.conf] /usr/local/redis6/conf/redis.conf
    Please select the redis log file name [/var/log/redis_16379.log] /usr/local/redis6/log/redis.log
    Please select the data directory for this instance [/var/lib/redis/16379] 
    Selected default - /var/lib/redis/16379
    Please select the redis executable path [] /usr/local/redis6/bin/redis-server
    Selected config:
    Port           : 16379
    Config file    : /usr/local/redis6/conf/redis.conf
    Log file       : /usr/local/redis6/log/redis.log
    Data dir       : /var/lib/redis/16379
    Executable     : /usr/local/redis6/bin/redis-server
    Cli Executable : /usr/local/redis6/bin/redis-cli
    Is this ok? Then press ENTER to go on or Ctrl-C to abort.
    Copied /tmp/16379.conf => /etc/init.d/redis_16379
    Installing service...
    Successfully added to chkconfig!
    Successfully added to runlevels 345!
    Starting Redis server...
    Installation successful!
    

    至此redis安装成功,运行脚本后默认会启动redis,查看redis进程


    image.png

    以后就可以使用系统服务的命令来操作redis了:

    systemctl stop redis_16379
    systemctl status redis_16379
    systemctl start redis_16379
    systemctl restart redis_16379
    

    redis.conf配置
    开启守护进程模式

    daemonize yes
    

    去除bind的ip,注释掉下面这一行,否则无法使用远程连接,只能本地连接redis。

    # bind 127.0.0.1
    

    关闭保护模式,将protected-mode的yes改为no,也是开启远程连接。

    protected-mode no
    

    改完后重启redis

    systemctl restart redis_16379
    

    三台服务器都需要安装。

    二、集群搭建

    1.从节点的配置(两台从节点主机都需要配置)
    从节点也是redis服务,只需要修改redis.conf配置文件中的内容,重启redis即可

     # 声明主节点,旧的版本是slaveof配置,两者等价
    replicaof 10.27.3.121 16379
    
    # 配置主节点访问密码,若主节点没有密码则可忽略
    masterauth 123456
    

    配置完毕后启动从节点服务,可以在主节点的redis日志中看到两台从机连接成功,构建了redis复制集(主从结构)。


    image.png
    image.png

    2.哨兵节点的配置
    哨兵模式的主要作用就是监控Redis集群,所以我们搭建哨兵模式之前要确认我们的Redis集群已经成功搭建了。哨兵模式的搭建其实也相对比较简单,只需要配置主节点监控即可。
    进入主节点的redis解压缩目录下,将sentinel.conf配置文件移动到redis安装目录下

    mv sentinel.conf /usr/local/redis6/conf/
    

    修改sentinel.conf配置文件

    # sentinel监听端口,默认是26379,可以修改。
    port 26379
    # 是否开启守护进程
    daemonize yes
    # pid文件所在目录
    pidfile "/var/run/redis-sentinel.pid"
    # sentinel日志所在目录
    logfile "/usr/local/redis6/log/redis-sentinel.log"
    # 哨兵sentinel的工作目录
    dir "/tmp"
    # 告诉sentinel去监听地址为ip:port的一个master,这里的master-name可以自定义,quorum是一个数字,指明当有多少个sentinel认为一个master失效时,master才算真正失效。master-name只能包含英文字母,数字,和“.-_”这三个字符需要注意的是master-ip 要写真实的ip地址而不要用回环地址(127.0.0.1)
    sentinel monitor mymaster 10.27.3.121 16379 2
    # 这个配置项指定了需要多少失效时间,一个master才会被这个sentinel主观地认为是不可用的。 单位是毫秒,默认为30秒
    sentinel down-after-milliseconds mymaster 10000
    # failover-timeout 可以用在以下这些方面: 
    
         # 1. 同一个sentinel对同一个master两次failover之间的间隔时间。
    
         # 2. 当一个slave从一个错误的master那里同步数据开始计算时间。直到slave被纠正为向正确的master那里同步数据时。
    
         # 3.当想要取消一个正在进行的failover所需要的时间。  
    
         # 4.当进行failover时,配置所有slaves指向新的master所需的最大时间。不过,即使过了这个超时,slaves依然会被正确配置为指向master,但是就不按parallel-syncs所配置的规则来了。
    sentinel failover-timeout mymaster 180000
    

    启动sentinel

    /usr/local/redis6/bin/redis-sentinel /usr/local/redis6/conf/sentinel.conf
    

    查看sentinel日志

    tail -f /usr/local/redis6/log/redis-sentinel.log
    
    image.png

    三、测试

    当前三台主机
    10.27.3.121(主节点)


    image.png

    10.27.3.122(从节点)


    image.png

    10.27.3.123(从节点)


    image.png

    如果此时当主节点挂了

    systemctl stop redis_16379
    

    哨兵会自动选举出新的主节点,我们发现10.27.3.122变成了主节点


    image.png

    10.27.3.123(从节点)


    image.png
    哨兵模式正常工作。

    相关文章

      网友评论

          本文标题:05给女朋友讲讲Redis-集群搭建(哨兵模式)

          本文链接:https://www.haomeiwen.com/subject/auhtpltx.html