redis-monitor

Redis-Server 监控机安装


一、安装redis

此过程请参考 这里

  • 复制可执行文件

    1
    2
    sudo cp /usr/local/redis/src/redis-server /usr/local/bin
    sudo cp /usr/local/redis/src/redis-cli /usr/local/bin
  • 创建 redis-monitor 的软链

    1
    2
    cd /usr/local/bin
    sudo ln -s /usr/local/bin/redis-server redis-monitor

二、配置监控

  • 创建redis目录

    1
    2
    sudo mkdir /etc/redis
    sudo mkdir /etc/log/redis
  • 赋予redis目录权限

    1
    2
    sudo chown -R admin:admin /etc/redis
    sudo chown -R admin:admin /etc/log/redis
  • 创建监控配置文件sentinel.conf

    1
    touch /etc/redis/sentinel.conf
  • 修改sentinel.conf增加portal-sessionportal-site配置段

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    # 监控端口
    port 26379
    # 集群master端口及地址,名称支持 A-z 0-9 特殊字符可包含 ".-_"
    sentinel monitor portal-site 172.17.121.33 1221 1
    sentinel monitor portal-session 172.17.121.33 6379 1
    # Master宕机后 N 秒内由slave接替其工作,单位:毫秒
    sentinel down-after-milliseconds portal-site 30000
    sentinel down-after-milliseconds portal-session 30000
    # 设置是否可以由监控发起自动切换
    sentinel can-failover portal-site yes
    sentinel can-failover portal-session yes
    # 设置当master宕机后由 N 个slave同步提供服务
    # 如果仅使用redis供查询使用,请尽量设置一个较低的值以避免同步锁问题
    sentinel parallel-syncs portal-site 1
    sentinel parallel-syncs portal-session 1
    # Default is 15 minutes.
    sentinel failover-timeout portal-site 900000
    sentinel failover-timeout portal-session 900000
  • 创建服务启动脚本

    1
    sudo touch /etc/init.d/redis-monitor
  • 修改redis-monitor脚本内容

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    #!/bin/sh
    #
    # redis - this script starts and stops the redis-server daemon
    #
    # chkconfig: - 85 15
    # description: Redis is a persistent key-value database
    # processname: redis-server
    # config: /etc/redis/sentinel.conf
    # pidfile: /var/run/sentinel.pid
    # lockfile: /var/lcok/sentinel
    # Source function library.
    . /etc/rc.d/init.d/functions
    # Source networking configuration.
    . /etc/sysconfig/network
    # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 0
    # define the exec progrom
    REDIS_EXEC="/usr/local/bin/redis-monitor"
    REDIS_CONF_FILE="/etc/redis/sentinel.conf"
    PROG=$(basename $REDIS_EXEC)
    # if /etc/sysconfig/redis exists,use this config env
    [ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis
    LOCKFILE=/var/lock/redis/sentinel
    start() {
    [ -x $REDIS_EXEC ] || exit 5
    [ -f $REDIS_CONF_FILE ] || exit 6
    echo -n $"Starting $PROG: "
    nohup $REDIS_EXEC /etc/redis/sentinel.conf --sentinel > /var/log/redis/monitor.log 2>&1 &
    echo [ OK ] && touch $LOCKFILE
    return
    }
    stop() {
    pid=`ps -ef|grep $REDIS_EXEC|grep -v grep|awk '{print $2}'`
    if [ ! $pid ]; then
    echo $"$PROG is stoped"
    else
    echo -n $"Stopping $PROG: "
    kill -9 $pid
    echo [ OK ] && rm -f $LOCKFILE
    fi
    return
    }
    rh_status() {
    status $PROG
    }
    rh_status_q() {
    rh_status >/dev/null 2>&1
    }
    restart() {
    stop
    start
    }
    exit_a(){
    echo $"$PROG is running"
    exit 0
    }
    exit_b(){
    echo $"$PROG is stopped"
    exit 0
    }
    case "$1" in
    start)
    rh_status_q && exit_a
    $1
    ;;
    stop)
    rh_status_q || exit_b
    $1
    ;;
    restart|configtest)
    $1
    ;;
    status)
    rh_status
    ;;
    *)
    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
    exit 2
    esac
  • 赋予此脚本可执行权限

    1
    sudo chmod u+x redis-monitor
  • 添加系统启动服务

    1
    2
    sudo chkconfig --add redis-monitor
    sudo chkconfig --level 345 redis-monitor on
  • 启动服务

    1
    sudo service redis-monitor start
  • 验证服务正确启动