美文网首页
单机部署redis的哨兵模式

单机部署redis的哨兵模式

作者: 程序员小白成长记 | 来源:发表于2021-06-19 18:11 被阅读0次

    前言:

    一、下载&安装

    官方各版本的Redis下载页

    以redis-4.0.9为例

    1.1,下载&解压&安装

    tar -zxvf redis-4.0.9.tar.gz
    cd redis-4.0.9
    make

    1.2,查看版本

    redis的命令放在了redis-4.0.9/src下面
    [root@zhl src]# ./redis-server -v
    Redis server v=4.0.9 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=90a89fe455a78ba9

    二、配置redis.conf文件

    2.1,查看配置

    cat redis.conf | grep -v "#" | grep -v "^$"

    2.2 重点关注的配置

    • port:(主从节点配置关注)服务启动端口号
    • protected-mode:(主从节点配置关注)redis3.2版本后新增protected-mode配置,默认是yes,即开启。设置外部网络连接redis服务,设置方式如下:
      1、关闭protected-mode模式,此时外部网络可以直接访问
      2、开启protected-mode保护模式,需配置bind ip或者设置访问密码
    • requirepass: (主从节点配置关注) redis密码
    • daemonize:(主从节点配置关注)是否以守护进程的方式启动,Yes:以守护进行方式启动(启动后自动在后台运行),No:不以守护进程的方式启动
    • slaveof: (从节点配置关注)需要在slave的配置文件配置
    • masterauth: (从节点配置关注)此参数是当与主连接时的密码验证

    2.3 配置master、slave的conf

    • master节点的关键配置
    bind 127.0.0.1
    protected-mode yes
    port 9090
    daemonize yes
    requirepass 123456
    
    • slave节点的关键配置
    bind 127.0.0.1
    protected-mode yes
    port 9091
    slaveof 127.0.0.1 9090
    masterauth 123456
    requirepass 123456
    

    sed 's/9091/9092/g' redis-9091.conf > redis-9092.conf

    bind 127.0.0.1
    protected-mode yes
    port 9092
    slaveof 127.0.0.1 9090
    masterauth 123456
    requirepass 123456
    

    三、启动redis

    1,启动master
    ./src/redis-server redis-9090.conf
    [root@zhl redis-4.0.9]# ./src/redis-server redis-9090.conf
    30783:C 19 Jun 11:36:51.928 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    30783:C 19 Jun 11:36:51.928 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=30783, just started
    30783:C 19 Jun 11:36:51.928 # Configuration loaded

    2,启动slave
    ./src/redis-server redis-9091.conf
    ./src/redis-server redis-9092.conf

    四、查看redis集群

    4.1 查看master节点

    ./redis-cli -p 9090
    通过info replication可以看到现在有两个slave

    127.0.0.1:9090> info replication
    # Replication
    role:master
    connected_slaves:2
    slave0:ip=127.0.0.1,port=9092,state=online,offset=14,lag=0
    slave1:ip=127.0.0.1,port=9091,state=online,offset=14,lag=1
    master_replid:fe56c3d1900ff6b28d2df4992454415fda0c9cbd
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:14
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:14
    

    4.2 查看slave节点

    ./redis-cli -p 9091
    通过info replication可以看到master

    127.0.0.1:9091> info replication
    # Replication
    role:slave
    master_host:127.0.0.1
    master_port:9090
    master_link_status:up
    master_last_io_seconds_ago:4
    master_sync_in_progress:0
    slave_repl_offset:182
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    master_replid:fe56c3d1900ff6b28d2df4992454415fda0c9cbd
    master_replid2:0000000000000000000000000000000000000000
    master_repl_offset:182
    second_repl_offset:-1
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:1
    repl_backlog_histlen:182
    

    五、配置哨兵

    5.1 关注配置如下

    [root@zhl redis-4.0.9]# cat sentinel-26379.conf | grep -v "#" | grep -v "^$"
    port 26379
    daemonize yes
    dir "/tmp"
    sentinel monitor mymaster 127.0.0.1 9090 2
    sentinel auth-pass mymaster 123456
    sentinel down-after-milliseconds mymaster 30000
    sentinel parallel-syncs mymaster 1
    sentinel failover-timeout mymaster 180000
    

    【注】sentinel auth-pass mymaster 123456配置要放在sentinel down-after-milliseconds mymaster 30000之前,否则会报No such master with specified name.错误,详见参考【9】

    其中,

    • port:哨兵端口号,哨兵也是一个进程,有自己的端口号
    • sentinel monitor mymaster 127.0.0.1 9090 2:代表监控的redis master的ip为127.0.0.1,port为9090,名称为mymaster,名称可以随意配置。最后的2代表有几个哨兵认为master挂了,集群就认为master挂了
    • sentinel down-after-milliseconds mymaster 30000:代表在ping检查时30s无响应就认为主机下线
    • 其他的详见参考【8】

    5.2 其他哨兵

    基于sentinel-26379.conf新建sentinel-26378.confsentinel-26377.conf
    1)sed 's/26379/26378/g' sentinel-26379.conf > sentinel-26378.conf
    2)sed 's/26379/26377/g' sentinel-26379.conf > sentinel-26377.conf
    3)cat sentinel-26379.conf| grep -v "#" | grep -v "^$"

    【注】因为之前启动过sentinel-26379.conf,sentinel在启动后配置文件会发生变化,会生成如下配置(myid和主从信息),在复制的时候如果把myid复制过去会认为所有sentinel都是一个sentinel,无法相互发现组成集群,所以需要删掉 sentinel-26378.confsentinel-26377.conf中的myid字段。可参考【11】

    [root@zhl redis-4.0.9]# cat sentinel-26379.conf| grep -v "#" | grep -v "^$"
    port 26379
    daemonize yes
    dir "/tmp"
    sentinel myid 11209fee9229ec78214dc7097202ccdc22a770ff
    sentinel monitor mymaster 127.0.0.1 9090 2
    sentinel auth-pass mymaster 123456
    sentinel config-epoch mymaster 0
    sentinel leader-epoch mymaster 0
    sentinel known-slave mymaster 127.0.0.1 9091
    sentinel known-slave mymaster 127.0.0.1 9092
    sentinel current-epoch 0
    

    六、启动哨兵,查看状态

    6.1 启动哨兵

    1,启动哨兵
    ./redis-sentinel ../sentinel-26379.conf
    ./redis-sentinel ../sentinel-26378.conf
    ./redis-sentinel ../sentinel-26377.conf

    6.2 查看哨兵和master状态

    【注】启动哨兵后,哨兵的配置文件会发生变化

    [root@zhl src]# ./redis-cli -p 26379
    127.0.0.1:26379> info
    

    关注Sentinel项看集群的status和slaves以及sentinels的个数

    # 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=127.0.0.1:9090,slaves=2,sentinels=3
    

    ps -ef | grep sentinel可以查看当前的sentinels进程

    image.png

    七、故障演练

    1,部署完成哨兵模式是这种进程状态(1主2从+3哨兵)

    image.png

    2,kill掉master,看下会不会自动选主
    发现9092成为了新的master

    # Replication
    role:master
    connected_slaves:1
    slave0:ip=127.0.0.1,port=9091,state=online,offset=1309515,lag=1
    master_replid:4580c0ae133c8349cac0e69386078bdeafb34285
    master_replid2:fe56c3d1900ff6b28d2df4992454415fda0c9cbd
    master_repl_offset:1309648
    second_repl_offset:1288059
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:261073
    repl_backlog_histlen:1048576
    

    看下sentinel-26379.conf发现9092成为了master

    [root@zhl redis-4.0.9]# cat sentinel-26379.conf | grep -v "#" | grep -v "^$"
    protected-mode no
    port 26379
    daemonize yes
    dir "/tmp"
    sentinel myid 11209fee9229ec78214dc7097202ccdc22a770ff
    sentinel monitor mymaster 127.0.0.1 9092 2
    sentinel auth-pass mymaster 123456
    sentinel config-epoch mymaster 1
    sentinel leader-epoch mymaster 2
    sentinel known-slave mymaster 127.0.0.1 9091
    sentinel known-slave mymaster 127.0.0.1 9090
    sentinel known-sentinel mymaster 127.0.0.1 26378 7d0ec2a8dec006f048cac1e9ad0602f338f33b19
    sentinel known-sentinel mymaster 127.0.0.1 26377 a312b71bb4a175739ea710072b42210cf19c7422
    

    八、 常用命令

    1,连接

    ./redis-cli -h ip -p port -a password
    

    2,查看密码

    config get requirepass
    

    3,启动

    ./src/redis-server redis-9090.conf
    

    4,查看当前节点状态

    info replication
    

    九、参考

    【1】Redis protected-mode属性解读
    【2】Redis protected-mode:https://blog.csdn.net/csdn_ds/article/details/72550898
    【3】redis 3.2 protected-mode配置:https://blog.csdn.net/sukexiaozi/article/details/52022449
    【4】Redis 哨兵机制以及灾难演练
    【5】redis设置密码的两种方式:https://blog.csdn.net/qq_38555490/article/details/94336297
    【6】Redis 密码设置和查看密码
    【7】redis.conf配置文件配置项解析
    【8】Redis及其Sentinel配置项详细说明:https://blog.csdn.net/a1282379904/article/details/52335051/
    【9】redis哨兵启动报错No such master with specified name.:https://blog.csdn.net/Aphysia/article/details/106309342
    【10】Redis使用守护进程启动sentinel并指定其日志目录:https://blog.csdn.net/xuxile/article/details/52216923
    【11】Redis sentinel不会自动发现其他哨兵:http://www.voidcn.com/article/p-azpwdkkl-bxy.html
    【12】Redis5.X主从哨兵模式搭建 + springboot整合RedisTemplate:https://blog.csdn.net/wjsxybr0409/article/details/103908931

    相关文章

      网友评论

          本文标题:单机部署redis的哨兵模式

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