Skip to main content

Redis 哨兵模式

服务器IP主机角色
192.168.10.13Redis Master
192.168.10.14Redis Slave1
192.168.10.15Redis Slave2

Redis Master

  1. 下载 redis 安装包

    wget https://pdpublic.mingdao.com/private-deployment/offline/common/redis-6.2.16-glibc2.17-amd64.tar.gz
  2. 解压到安装目录

    tar -zxvf redis-6.2.16-glibc2.17-amd64.tar.gz
    mv redis-6.2.16-glibc2.17-amd64 /usr/local/redis
  3. 调整内核参数

    echo 'net.core.somaxconn = 32768' >> /etc/sysctl.conf
    echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
    sysctl -p
  4. 创建 redis 数据目录

    mkdir /data/redis
  5. 修改配置文件

    cat > /usr/local/redis/redis.conf <<EOF
    bind 0.0.0.0
    protected-mode yes
    port 6379
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /usr/local/redis/redis.pid
    loglevel notice
    logfile /usr/local/redis/redis.log
    databases 16
    save 900 1
    save 300 10
    save 60 100000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir /data/redis
    slave-serve-stale-data yes
    slave-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    slave-priority 100
    lua-time-limit 5000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit slave 256mb 64mb 60
    client-output-buffer-limit pubsub 0 0 0
    hz 10
    requirepass 123456
    masterauth 123456
    maxmemory 16gb
    maxmemory-policy allkeys-lru
    maxclients 100000
    rename-command KEYS ""
    EOF
    • redis 认证与主从认证的密码为 123456,实际部署时注意替换
  6. 创建 redis 用户授权

    useradd -U -M -s /sbin/nologin redis
    chown -R redis:redis /usr/local/redis/ /data/redis
  7. 配置 systemd 管理

    cat > /etc/systemd/system/redis.service <<EOF
    [Unit]
    Description=Redis
    [Service]
    User=redis
    Group=redis
    TasksMax=infinity
    LimitNOFILE=102400
    LimitNPROC=infinity
    LimitCORE=0
    ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
    ExecStop=/usr/bin/kill \$MAINPID
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
  8. 启动 redis 服务并加入开机自启动

    systemctl start redis
    systemctl enable redis

Redis Slave1

  1. 下载 redis 安装包

    wget https://pdpublic.mingdao.com/private-deployment/offline/common/redis-6.2.16-glibc2.17-amd64.tar.gz
  2. 解压到安装目录

    tar -zxvf redis-6.2.16-glibc2.17-amd64.tar.gz
    mv redis-6.2.16-glibc2.17-amd64 /usr/local/redis
  3. 调整内核参数

    echo 'net.core.somaxconn = 32768' >> /etc/sysctl.conf
    echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
    sysctl -p
  4. 创建 redis 数据目录

    mkdir /data/redis
  5. 修改配置文件

    cat > /usr/local/redis/redis.conf <<EOF
    bind 0.0.0.0
    protected-mode yes
    port 6379
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /usr/local/redis/redis.pid
    loglevel notice
    logfile /usr/local/redis/redis.log
    databases 16
    save 900 1
    save 300 10
    save 60 100000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir /data/redis
    slave-serve-stale-data yes
    slave-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    slave-priority 100
    lua-time-limit 5000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit slave 256mb 64mb 60
    client-output-buffer-limit pubsub 0 0 0
    hz 10
    requirepass 123456
    masterauth 123456
    maxmemory 16gb
    maxmemory-policy allkeys-lru
    maxclients 100000
    rename-command KEYS ""
    slaveof 192.168.1.13 6379
    EOF
    • redis 认证与主从认证的密码为 123456,实际部署时注意替换
    • slaveof 参数注意在实际部署过程中进行替换 master ip地址
  6. 创建 redis 用户授权

    useradd -U -M -s /sbin/nologin redis
    chown -R redis:redis /usr/local/redis/ /data/redis
  7. 配置 systemd 管理

    cat > /etc/systemd/system/redis.service <<EOF
    [Unit]
    Description=Redis
    [Service]
    User=redis
    Group=redis
    TasksMax=infinity
    LimitNOFILE=102400
    LimitNPROC=infinity
    LimitCORE=0
    ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
    ExecStop=/usr/bin/kill \$MAINPID
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
  8. 启动 redis 服务并加入开机自启动

    systemctl start redis
    systemctl enable redis

Redis Slave2

  1. 下载 redis 安装包

    wget https://pdpublic.mingdao.com/private-deployment/offline/common/redis-6.2.16-glibc2.17-amd64.tar.gz
  2. 解压到安装目录

    tar -zxvf redis-6.2.16-glibc2.17-amd64.tar.gz
    mv redis-6.2.16-glibc2.17-amd64 /usr/local/redis
  3. 调整内核参数

    echo 'net.core.somaxconn = 32768' >> /etc/sysctl.conf
    echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
    sysctl -p
  4. 创建 redis 数据目录

    mkdir /data/redis
  5. 修改配置文件

    cat > /usr/local/redis/redis.conf <<EOF
    bind 0.0.0.0
    protected-mode yes
    port 6379
    tcp-backlog 511
    timeout 0
    tcp-keepalive 300
    daemonize no
    supervised no
    pidfile /usr/local/redis/redis.pid
    loglevel notice
    logfile /usr/local/redis/redis.log
    databases 16
    save 900 1
    save 300 10
    save 60 100000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    dir /data/redis
    slave-serve-stale-data yes
    slave-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    slave-priority 100
    lua-time-limit 5000
    slowlog-log-slower-than 10000
    slowlog-max-len 128
    latency-monitor-threshold 0
    notify-keyspace-events ""
    hash-max-ziplist-entries 512
    hash-max-ziplist-value 64
    list-max-ziplist-size -2
    list-compress-depth 0
    set-max-intset-entries 512
    zset-max-ziplist-entries 128
    zset-max-ziplist-value 64
    hll-sparse-max-bytes 3000
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit slave 256mb 64mb 60
    client-output-buffer-limit pubsub 0 0 0
    hz 10
    requirepass 123456
    masterauth 123456
    maxmemory 16gb
    maxmemory-policy allkeys-lru
    maxclients 100000
    rename-command KEYS ""
    slaveof 192.168.1.13 6379
    EOF
    • redis 认证与主从认证的密码为 123456,实际部署时注意替换
    • slaveof 参数注意在实际部署过程中进行替换 master ip地址
  6. 创建 redis 用户授权

    useradd -U -M -s /sbin/nologin redis
    chown -R redis:redis /usr/local/redis/ /data/redis
  7. 配置 systemd 管理

    cat > /etc/systemd/system/redis.service <<EOF
    [Unit]
    Description=Redis
    [Service]
    User=redis
    Group=redis
    TasksMax=infinity
    LimitNOFILE=102400
    LimitNPROC=infinity
    LimitCORE=0
    ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
    ExecStop=/usr/bin/kill \$MAINPID
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
  8. 启动 redis 服务并加入开机自启动

    systemctl start redis
    systemctl enable redis

检查各节点主从同步状态

/usr/local/redis/bin/redis-cli -a 123456 info replication

哨兵配置

三台 Redis 服务器都操作

  1. 配置 sentinel 配置文件

    cat > /usr/local/redis/sentinel.conf <<EOF
    sentinel deny-scripts-reconfig yes
    sentinel monitor mymaster $REDIS_MASTER_IP 6379 2
    sentinel auth-pass mymaster 123456
    sentinel down-after-milliseconds mymaster 10000
    sentinel failover-timeout mymaster 60000
    port 26379
    daemonize yes
    pidfile "/usr/local/redis/redis-sentinel.pid"
    logfile "/usr/local/redis/redis-sentinel.log"
    dir "/data/redis/sentinel"
    EOF
    • $REDIS_MASTER_IP 注意在实际部署过程中进行替换 master ip 地址
    • 哨兵密码参数 123456,实际部署时注意替换
  2. 创建目录并授权 redis 相关目录权限

    mkdir -p /data/redis/sentinel
    chown -R redis:redis /data/redis /usr/local/redis
  3. 配置 systemd 管理

    cat > /etc/systemd/system/sentinel.service <<EOF
    [Unit]
    Description=Redis-sentinel
    [Service]
    User=redis
    Group=redis
    Type=forking
    TasksMax=infinity
    LimitNOFILE=102400
    LimitNPROC=infinity
    LimitCORE=0
    ExecStart=/usr/local/redis/bin/redis-sentinel /usr/local/redis/sentinel.conf
    ExecStop=/usr/bin/kill \$MAINPID
    Restart=on-failure
    [Install]
    WantedBy=multi-user.target
    EOF
  4. 启动哨兵

    systemctl start sentinel
    systemctl enable sentinel