• Redis-sentinel集群


    Redis-sentinel集群

    环境:
    CentOS 6.5 x64

    官方网站:Redis
    http://www.redis.io/
    http://www.redis.cn

    http://www.redis.cn/topics/cluster-tutorial.html
    http://www.redis.cn/topics/sentinel.html
    http://redis.io/topics/sentinel-clients
    参考文档:
    https://github.com/LittlePeng/redis-monitor
    https://github.com/twitter/twemproxy
    http://blog.csdn.net/pi9nc/article/details/17735653
    http://redis.readthedocs.org/en/latest/topic/sentinel.html

    以redis-2.8.18为例
    单机redis
    一.安装编译依赖库
    [root@master ~]# yum -y install gcc gcc-c++ make tcl-devel

    二.下载并安装
    [root@master ~]# wget http://download.redis.io/releases/redis-2.8.18.tar.gz
    [root@master ~]# tar -xvf redis-2.8.18.tar.gz  -C /usr/local/src
    [root@master ~]# cd /usr/local/src/redis-2.8.18
    [root@master redis-2.8.18]# ls
    00-RELEASENOTES  COPYING  Makefile   redis.conf        sentinel.conf  utils
    BUGS             deps     MANIFESTO  runtest           src
    CONTRIBUTING     INSTALL  README     runtest-sentinel  tests
    [root@master redis-2.8.18]# make && make install
    [root@master redis-2.8.18]# which redis-server
    /usr/local/bin/redis-server

    三.配置redis sysV管控脚本
    [root@master redis-2.8.18]# cd utils/
    [root@master utils]# ls
    build-static-symbols.tcl  mkrelease.sh           redis-sha1.rb
    generate-command-help.rb  redis-copy.rb          speed-regression.tcl
    hyperloglog               redis_init_script      whatisdoing.sh
    install_server.sh         redis_init_script.tpl
    [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!
    默认的话,一路回车即可。init脚本会自动生成
    [root@master utils]# mv /etc/init.d/redis_6379 /etc/init.d/redis
    [root@master utils]# /etc/init.d/redis
    Please use start, stop, restart or status as first argument
    [root@master utils]# /etc/init.d/redis status
    Redis is running (4796)
    [root@master utils]# /etc/init.d/redis restart
    Stopping ...
    Redis stopped
    Starting Redis server...
    [root@master utils]# chkconfig --add redis
    [root@master utils]# chkconfig redis on
    [root@master utils]# chkconfig --list redis
    redis_6379         0:off    1:off    2:on    3:on    4:on    5:on    6:off
    [root@master utils]# netstat -tunlp|grep redis
    tcp            0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      4812/redis-server *
    可以看到,redis己成功运行并监听在6379端口上。


    redis-sentinel
    集群监控
    Redis-sentinel是Redis官方提供的实例监控管理、通知和实例失效备援服务,是Redis集群的管理工具,用来监控其他redis instance的工作情况并且进行故障恢复,来提高集群的高可用性。

    一.安装同上

    二.
    配置redis-sentinel sysV管控脚本
    [root@master redis]# cd /usr/local/src/redis-2.8.18/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] 26379
    Please select the redis config file name [/etc/redis/26379.conf] /etc/redis/sentinel.conf
    Please select the redis log file name [/var/log/redis_26379.log] /var/log/redis_sentinel.log
    Please select the data directory for this instance [/var/lib/redis/26379] /var/lib/redis/sentinel
    Please select the redis executable path [/usr/local/bin/redis-server]
    Selected config:
    Port           : 26379
    Config file    : /etc/redis/sentinel.conf
    Log file       : /var/log/redis/sentinel.log
    Data dir       : /var/lib/redis/sentinel
    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/26379.conf => /etc/init.d/redis_26379
    Installing service...
    Successfully added to chkconfig!
    Successfully added to runlevels 345!
    Starting Redis server...
    Installation successful!

    三.修改配置文件/etc/redis/sentinel.conf
    [root@slave redis]# /etc/init.d/redis_26379 stop
    Stopping ...
    Redis stopped
    [root@master ~]# cd /etc/redis/
    [root@master redis]# cp -fv /usr/local/src/redis-2.8.18/sentinel.conf .
    cp: overwrite `./sentinel.conf'? y
    `/usr/local/src/redis-2.8.18/sentinel.conf' -> `./sentinel.conf'
    [root@master redis]# mv /etc/init.d/redis_26379 /etc/init.d/redis_sentinel
    [root@master redis]# vim /etc/init.d/redis_sentinel
    PIDFILE=/var/run/redis.pid
    $EXEC $CONF --sentinel
    echo "Redis started"

    redis-sentinel集群模式需要在管控脚本吕加入--sentinel参数,如上,
    redis是修改的,
    --sentinel
    echo "Redis started"是添加的。

    补充:
    也可以通过redis-sentinel命令来启动服务,效果一样
    [root@master ~]# cp -fv /usr/local/src/redis-2.8.18/src/redis-sentinel /usr/local/bin/
    `/usr/local/src/redis-2.8.18/src/redis-sentinel' -> `/usr/local/bin/redis-sentinel'
    [root@master ~]# vim /etc/init.d/redis_sentinel
    EXEC=/usr/local/bin/redis-sentinel
    PIDFILE=/var/run/redis.pid
    echo "Redis started"

    [root@master redis]# /etc/init.d/redis_sentinel restart
    /var/run/redis_26379.pid does not exist, process is not running
    Starting Redis server...
    [1880] 18 Dec 17:18:57.606 * Increased maximum number of open files to 10032 (it was originally set to 1024).

                   _._                                                  

               _.-``__ ''-._                                             

          _.-``    `.  `_.  ''-._           Redis 2.8.18 (00000000/0) 64 bit

      .-`` .-```.  ```/    _.,_ ''-._                                   

            ,       .-`  | `,    )     Running in sentinel mode

     |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379

        `-._   `._    /     _.-'    |     PID: 70563

      `-._    `-._  `-./  _.-'    _.-'                                   

     |`-._`-._    `-.__.-'    _.-'_.-'|                                  

        `-._`-._        _.-'_.-'    |           http://redis.io        

      `-._    `-._`-.__.-'_.-'    _.-'                                   

     |`-._`-._    `-.__.-'    _.-'_.-'|                                  

        `-._`-._        _.-'_.-'    |                                  

      `-._    `-._`-.__.-'_.-'    _.-'                                   

          `-._    `-.__.-'    _.-'                                       

              `-._        _.-'                                           

                  `-.__.-' 

    [1880] 18 Dec 17:18:57.608 # Sentinel runid is 79d27d0eb9c06622660dd3261ea56f2ffe6b77f6
    [1880] 18 Dec 17:18:57.608 # +monitor master mymaster 127.0.0.1 6379 quorum 2
    daemonize yes #在配置文件/etc/redis/sentinel.conf中加入该参数可以后台运行


    加入开机启动项
    [root@master ~]# chkconfig --add redis_sentinel
    [root@master ~]# chkconfig redis_sentinel on
    [root@master ~]# chkconfig --list redis_sentinel
    redis_sentinel     0:off    1:off    2:on    3:on    4:on    5:on    6:off
    [root@master ~]# netstat -tunlp|grep redis
    tcp            0 0.0.0.0:26379               0.0.0.0:*                   LISTEN      1898/redis-server *



    配置集群
    Sentinel: 192.168.8.80
    Master: 192.168.8.81
    Slave: 192.168.8.82

    Master
    redis配置文件,使用默认的配置文件就可以了

    Slave redis配置文件,需要指定Master主机

    slaveof 192.168.8.81 6379

    [root@slave redis]# /etc/init.d/redis restart
    Stopping ...
    Redis stopped
    Starting Redis server...
    slave修改配置文件后需要重启服务才能和master建立主从关系。
    查看master节点信息
    [root@slave redis]# redis-cli -h 192.168.8.81 info Replication
    # Replication
    role:master
    connected_slaves:1
    slave0:ip=192.168.8.82,port=6379,state=online,offset=29,lag=1

    master_repl_offset:29
    repl_backlog_active:1
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:2
    repl_backlog_histlen:28
    查看slave节点信息
    [root@slave redis]# redis-cli -h 192.168.8.82 info Replication
    # Replication
    role:slave
    master_host:192.168.8.81
    master_port:6379
    master_link_status:up
    master_last_io_seconds_ago:8
    master_sync_in_progress:0
    slave_repl_offset:85
    slave_priority:100
    slave_read_only:1
    connected_slaves:0
    master_repl_offset:0
    repl_backlog_active:0
    repl_backlog_size:1048576
    repl_backlog_first_byte_offset:0
    repl_backlog_histlen:0
    可以看到,master和slave已经成功建立主从关系。

    配置集群监控sentinel

    [root@sentinel redis]# vim sentinel.conf
    #daemonize yes
    port 26379
    logfile /var/log/redis/sentinel.log

    sentinel monitor mymaster 192.168.8.81 6379 1
    sentinel down-after-milliseconds mymaster 5000
    sentinel failover-timeout mymaster 900000
    sentinel parallel-syncs mymaster 2


    #sentinel monitor mymaster 192.168.8.71 6379 1
    #sentinel down-after-milliseconds mymaster 5000
    #sentinel failover-timeout mymaster 900000
    #sentinel parallel-syncs mymaster 2

    提示:可以监控多个主从

    [root@sentinel redis]# /etc/init.d/redis_sentinel restart
    /var/run/redis.pid does not exist, process is not running
    Starting Redis server...
    [25497] 20 Dec 13:33:04.377 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                                                 

                   _._                                                  

               _.-``__ ''-._                                             

          _.-``    `.  `_.  ''-._           Redis 2.8.18 (00000000/0) 64 bit

      .-`` .-```.  ```/    _.,_ ''-._                                   

            ,       .-`  | `,    )     Running in sentinel mode

     |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379

        `-._   `._    /     _.-'    |     PID: 25497

      `-._    `-._  `-./  _.-'    _.-'                                   

     |`-._`-._    `-.__.-'    _.-'_.-'|                                  

        `-._`-._        _.-'_.-'    |           http://redis.io        

      `-._    `-._`-.__.-'_.-'    _.-'                                   

     |`-._`-._    `-.__.-'    _.-'_.-'|                                  

        `-._`-._        _.-'_.-'    |                                  

      `-._    `-._`-.__.-'_.-'    _.-'                                   

          `-._    `-.__.-'    _.-'                                       

              `-._        _.-'                                           

                  `-.__.-' 

    [25497] 20 Dec 13:33:04.379 # Sentinel runid is c1a5ea6985be2f345f1439fd6e96994c8bf02d8e
    [25497] 20 Dec 13:33:04.379 # +monitor master mymaster 192.168.8.81 6379 quorum 1
    [25497] 20 Dec 13:33:04.382 * +slave slave 192.168.8.82:6379 192.168.8.82 6379 @ mymaster 192.168.8.81 6379
    可以看到sentinel己成功监控到master和slave
    补充:安全设置
    可以置客户端访问密码
    Master配置文件中加入
    requirepass test123

    Slave
    配置文件中加入
    requirepass test123
    masterauth test123

    Redis-sentinel配置文件中加入
    sentinel auth-pass mymaster test123


     查看监控信息

    [root@sentinel ~]# redis-cli -h 192.168.8.80 -p 26379 info Sentinel
    # Sentinel
    sentinel_masters:1
    sentinel_tilt:0
    sentinel_running_scripts:0
    sentinel_scripts_queue_length:0
    master0:name=mymaster,status=ok,address=192.168.8.81:6379,slaves=1,sentinels=1
    或者
    [root@sentinel ~]# redis-cli -p 26379
    127.0.0.1:26379> PING
    PONG
    127.0.0.1:26379> SENTINEL masters
    1)  1) "name"
        2) "mymaster"
        3) "ip"
        4) "192.168.8.81"
        5) "port"
        6) "6379"
        7) "runid"
        8) "8765726c8f96596128c54df0769bda6e42316d8f"
        9) "flags"
       10) "master"
       11) "pending-commands"
       12) "0"
       13) "last-ping-sent"
       14) "0"
       15) "last-ok-ping-reply"
       16) "302"
       17) "last-ping-reply"
       18) "302"
       19) "down-after-milliseconds"
       20) "5000"
       21) "info-refresh"
       22) "7140"
       23) "role-reported"
       24) "master"
       25) "role-reported-time"
       26) "509174"
       27) "config-epoch"
       28) "0"
       29) "num-slaves"
       30) "1"
       31) "num-other-sentinels"
       32) "0"
       33) "quorum"
       34) "1"
       35) "failover-timeout"
       36) "900000"
       37) "parallel-syncs"
       38) "2"
    127.0.0.1:26379> SENTINEL slaves mymaster
    1)  1) "name"
        2) "192.168.8.82:6379"
        3) "ip"
        4) "192.168.8.82"
        5) "port"
        6) "6379"
        7) "runid"
        8) "7fadb0805198cb4a0c68eb6e8aad1580378d9c4d"
        9) "flags"
       10) "slave"
       11) "pending-commands"
       12) "0"
       13) "last-ping-sent"
       14) "0"
       15) "last-ok-ping-reply"
       16) "720"
       17) "last-ping-reply"
       18) "720"
       19) "down-after-milliseconds"
       20) "5000"
       21) "info-refresh"
       22) "5366"
       23) "role-reported"
       24) "slave"
       25) "role-reported-time"
       26) "527579"
       27) "master-link-down-time"
       28) "0"
       29) "master-link-status"
       30) "ok"
       31) "master-host"
       32) "192.168.8.81"
       33) "master-port"
       34) "6379"
       35) "slave-priority"
       36) "100"
       37) "slave-repl-offset"
       38) "37976"
    127.0.0.1:26379> SENTINEL get-master-addr-by-name mymaster
    1) "192.168.8.81"
    2) "6379"


    故障群集转移测试
    [root@sentinel ~]# redis-cli -h 192.168.8.81 -p 6379 shutdown
    关闭master节点的redis服务,查看sentinel监控日志
    [25497] 20 Dec 13:48:51.510 # +sdown master mymaster 192.168.8.81 6379
    [25497] 20 Dec 13:48:51.510 # +odown master mymaster 192.168.8.81 6379 #quorum 1/1
    [25497] 20 Dec 13:48:51.510 # +new-epoch 3
    [25497] 20 Dec 13:48:51.510 # +try-failover master mymaster 192.168.8.81 6379
    [25497] 20 Dec 13:48:51.565 # +vote-for-leader c1a5ea6985be2f345f1439fd6e96994c8bf02d8e 3
    [25497] 20 Dec 13:48:51.565 # +elected-leader master mymaster 192.168.8.81 6379
    [25497] 20 Dec 13:48:51.565 # +failover-state-select-slave master mymaster 192.168.8.81 6379
    [25497] 20 Dec 13:48:51.632 # +selected-slave slave 192.168.8.82:6379 192.168.8.82 6379 @ mymaster 192.168.8.81 6379
    [25497] 20 Dec 13:48:51.632 * +failover-state-send-slaveof-noone slave 192.168.8.82:6379 192.168.8.82 6379 @ mymaster 192.168.8.81 6379
    [25497] 20 Dec 13:48:51.684 * +failover-state-wait-promotion slave 192.168.8.82:6379 192.168.8.82 6379 @ mymaster 192.168.8.81 6379
    [25497] 20 Dec 13:48:52.615 # +promoted-slave slave 192.168.8.82:6379 192.168.8.82 6379 @ mymaster 192.168.8.81 6379
    [25497] 20 Dec 13:48:52.615 # +failover-state-reconf-slaves master mymaster 192.168.8.81 6379
    [25497] 20 Dec 13:48:52.676 # +failover-end master mymaster 192.168.8.81 6379
    [25497] 20 Dec 13:48:52.676 # +switch-master mymaster 192.168.8.81 6379 192.168.8.82 6379
    [25497] 20 Dec 13:48:52.676 * +slave slave 192.168.8.81:6379 192.168.8.81 6379 @ mymaster 192.168.8.82 6379
    [25497] 20 Dec 13:48:57.712 # +sdown slave 192.168.8.81:6379 192.168.8.81 6379 @ mymaster 192.168.8.82 6379
    可以看到,master己成功切换至192.168.8.82,相应地192.168.8.81则成为192.168.8.82的slave
    [root@sentinel ~]# redis-cli -h 192.168.8.80 -p 26379 info Sentinel
    # Sentinel
    sentinel_masters:1
    sentinel_tilt:0
    sentinel_running_scripts:0
    sentinel_scripts_queue_length:0
    master0:name=mymaster,status=ok,address=192.168.8.82:6379,slaves=1,sentinels=1


    当手动重启192.168.8.81时,sentinel将其convert为slave
    [root@master ~]# /etc/init.d/redis start
    Starting Redis server...

    [25497] 20 Dec 13:58:42.556 # -sdown slave 192.168.8.81:6379 192.168.8.81 6379 @ mymaster 192.168.8.82 6379
    [25497] 20 Dec 13:58:52.557 * +convert-to-slave slave 192.168.8.81:6379 192.168.8.81 6379 @ mymaster 192.168.8.82 6379

  • 相关阅读:
    爬虫入门---Python2和Python3的不同
    Python学习笔记——文件
    Python字典笔记
    Python列表操作——模拟实现栈和队列
    元组的cmp()内建函数
    Unicode基本概念
    神经网络基本的一些概念
    LLDB调试基本使用
    HTML实现跳转到页面指定位置
    通过pod导入第三方框架
  • 原文地址:https://www.cnblogs.com/lixuebin/p/10814451.html
Copyright © 2020-2023  润新知