---
title: "Redis Sentinel"
date: 2019-10-30T11:37:36+08:00
lastmod: 2019-10-30T11:37:36+08:00
tags: ["redis", "sentinel", "哨兵"]
categories: ["database"]
---

## 环境
主机名 | 地址 | 操作系统 | 初始状态
---- | ---- | ---- | ----
sentinel86 | 10.1.14.86 | CentOS7.4 | null
redis87 | 10.1.14.87 | CentOS7.4 | master
redis88 | 10.1.14.88 | CentOS7.4 | slave
redis89 | 10.1.14.89 | CentOS7.4 | slave

## 安装 redis
- 在全部服务器上安装 redis
    ```bash
    yum install epel-release
    yum install redis
    ```

## redis master 配置
- 在 redis87 上修改 /etc/redis.conf
    ```
    bind 0.0.0.0
    ```

## redis slave 配置
- 在 redis88 和 redis89 上修改 /etc/redis.conf
    ```
    bind 0.0.0.0
    slaveof 10.1.14.87 6379
    ```

## redis sentinel 配置
- 在 sentinel86 上创建三个 sentinel 配置文件
    ```bash
    cd /etc/
    cp redis-sentinel.conf redis-sentinel-16379.conf
    cp redis-sentinel.conf redis-sentinel-26379.conf
    mv redis-sentinel.conf redis-sentinel-36379.conf
    chown redis.root redis-sentinel-?6379.conf
    ```
- 修改 /etc/redis-sentinel-16379.conf
    ```
    bind 0.0.0.0
    port 16379
    dir /var/log/redis/16379
    sentinel monitor mymaster 10.1.14.87 6379 2
    sentinel down-after-milliseconds mymaster 8000
    logfile /var/log/redis/16379/sentinel.log
    ```
- 修改 /etc/redis-sentinel-26379.conf
    ```
    bind 0.0.0.0
    port 26379
    dir /var/log/redis/26379
    sentinel monitor mymaster 10.1.14.87 6379 2
    sentinel down-after-milliseconds mymaster 8000
    logfile /var/log/redis/26379/sentinel.log
    ```
- 修改 /etc/redis-sentinel-36379.conf
    ```
    bind 0.0.0.0
    port 36379
    dir /var/log/redis/36379
    sentinel monitor mymaster 10.1.14.87 6379 2
    sentinel down-after-milliseconds mymaster 8000
    logfile /var/log/redis/36379/sentinel.log
    ```
- 创建日志目录
    ```bash
    cd /var/log/redis/
    mkdir 16379 26379 36379
    chown redis.redis ?6379 -R
    ```
- 创建两个 sentinel service 文件
    ```bash
    cd /usr/lib/systemd/system/
    cp redis-sentinel.service redis-sentinel-16379.service
    cp redis-sentinel.service redis-sentinel-26379.service
    mv redis-sentinel.service redis-sentinel-36379.service
    ```
- 修改 /usr/lib/systemd/system/redis-sentinel-16379.service
    ```
    ExecStart=/usr/bin/redis-sentinel /etc/redis-sentinel-16379.conf --daemonize no
    ExecStop=/usr/libexec/redis-shutdown redis-sentinel-16379
    ```
- 修改 /usr/lib/systemd/system/redis-sentinel-26379.service
    ```
    ExecStart=/usr/bin/redis-sentinel /etc/redis-sentinel-26379.conf --daemonize no
    ExecStop=/usr/libexec/redis-shutdown redis-sentinel-26379
    ```
- 修改 /usr/lib/systemd/system/redis-sentinel-36379.service
    ```
    ExecStart=/usr/bin/redis-sentinel /etc/redis-sentinel-36379.conf --daemonize no
    ExecStop=/usr/libexec/redis-shutdown redis-sentinel-36379
    ```
- 更新服务
    ```bash
    systemctl daemon-reload
    ```

## 启动 redis 服务
- 在 redis87、redis88 和 redis89 上启动 redis 服务
    ```bash
    systemctl start redis
    # 查看当前节点主从信息
    redis-cli info replication
    ```

## 启动 redis sentinel 监控
- 在 sentinel86 上启动 sentinel 监管程序
    ```bash
    systemctl start redis-sentinel-16379
    systemctl start redis-sentinel-26379
    systemctl start redis-sentinel-36379
    ```

