环境

  • 操作系统: CentOS7.7,关闭 firewalld 和 selinux
  • Redis: 5.0.6
  • master 实例
    • 10.0.4.110:6371
    • 10.0.4.110:6372
    • 10.0.4.110:6373
  • slave 实例
    • 10.0.4.111:6371
    • 10.0.4.111:6372
    • 10.0.4.111:6373

编译 redis server

  • 在 CentOS7.7 测试服务器上下载、编译

    1
    2
    3
    4
    
    wget http://download.redis.io/releases/redis-5.0.6.tar.gz
    tar xzf redis-5.0.6.tar.gz
    cd redis-5.0.6
    make
  • 抽取安装文件

    1
    2
    3
    
    mkdir -p /opt/redis_cluster/{bin,conf}
    cp src/{redis-server,redis-cli,redis-benchmark} /opt/redis_cluster/bin/
    # redis-check-rdb 和 redis-check-aof 与 redis-server 校验值一致,无需复制
  • 创建 /opt/redis_cluster/conf/redis.conf,常用配置如下

     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
    
    bind 0.0.0.0
    protected-mode yes
    tcp-backlog 511
    timeout 3600
    tcp-keepalive 300
    daemonize no
    supervised no
    loglevel notice
    always-show-logo no
    save 900 1
    save 300 10
    save 60 10000
    stop-writes-on-bgsave-error yes
    rdbcompression yes
    rdbchecksum yes
    dbfilename dump.rdb
    #masterauth "123456"
    replica-serve-stale-data yes
    replica-read-only yes
    repl-diskless-sync no
    repl-diskless-sync-delay 5
    repl-disable-tcp-nodelay no
    replica-priority 100
    #requirepass "123456"
    maxclients 10000
    maxmemory 4294967296
    maxmemory-policy volatile-lru
    lazyfree-lazy-eviction no
    lazyfree-lazy-expire no
    lazyfree-lazy-server-del no
    replica-lazy-flush no
    appendonly no
    appendfilename "appendonly.aof"
    appendfsync everysec
    no-appendfsync-on-rewrite no
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    aof-load-truncated yes
    aof-use-rdb-preamble yes
    lua-time-limit 5000
    cluster-enabled yes
    cluster-node-timeout 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
    stream-node-max-bytes 4096
    stream-node-max-entries 100
    activerehashing yes
    client-output-buffer-limit normal 0 0 0
    client-output-buffer-limit replica 256mb 64mb 60
    client-output-buffer-limit pubsub 32mb 8mb 60
    hz 10
    dynamic-hz yes
    aof-rewrite-incremental-fsync yes
    rdb-save-incremental-fsync yes
  • 创建 /opt/redis_cluster/redis@.service,配置如下

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    [Unit]
    Description=Redis cluster -- node %i
    After=network.target
    
    [Service]
    ExecStartPre=/usr/bin/ls -d /var/lib/redis_cluster/%i
    ExecStart=/opt/redis_cluster/bin/redis-server /opt/redis_cluster/conf/redis.conf \
        --daemonize no \
        --port %i \
        --pidfile /var/run/redis_%i.pid \
        --logfile /var/log/redis_cluster/%i.log \
        --dir /var/lib/redis_cluster/%i \
        --cluster-config-file /var/lib/redis_cluster/%i/nodes.conf
    ExecStop=/opt/redis_cluster/bin/redis-cli -h 127.0.0.1 -p %i shutdown
    Restart=on-failure
    RestartSec=1
    
    [Install]
    WantedBy=multi-user.target

部署

  • 把测试机的 /opt/redis_cluster 目录复制到 10.0.4.110 和 10.0.4.111 上

    1
    2
    
    scp -r /opt/redis_cluster 10.0.4.110:/opt/
    scp -r /opt/redis_cluster 10.0.4.111:/opt/
  • 在 10.0.4.110 和 10.0.4.111 上都执行以下操作

    1
    2
    3
    4
    
    mv /opt/redis_cluster/redis@.service /usr/lib/systemd/system/
    systemctl daemon-reload
    mkdir -p /var/lib/redis_cluster/{6371..6373}
    mkdir -p /var/log/redis_cluster
  • 在 10.0.4.110 和 10.0.4.111 上配置实例开机启动

    1
    2
    3
    
    systemctl enable redis@6371
    systemctl enable redis@6372
    systemctl enable redis@6373
  • 在 10.0.4.110 和 10.0.4.111 上启动全部实例

    1
    2
    3
    
    systemctl start redis@6371
    systemctl start redis@6372
    systemctl start redis@6373

配置集群

  • 在 10.0.4.110 或 10.0.4.111 上创建集群,指定三个 master 实例

    1
    2
    
    /opt/redis_cluster/bin/redis-cli \
        --cluster create 10.0.4.110:6371 10.0.4.110:6372 10.0.4.110:6373
  • 输出如下图

  • 同意配置,输入 “yes”,回车后输出如下图

  • 查看集群当前实例

    1
    
    /opt/redis_cluster/bin/redis-cli -p 6371 cluster nodes
  • 输出如下图

  • 添加 slave 实例 10.0.4.111:6371,同步 master 10.0.4.110:6371

    1
    2
    3
    4
    5
    6
    7
    
    /opt/redis_cluster/bin/redis-cli \
        --cluster add-node 10.0.4.111:6371 10.0.4.110:6371 \
        --cluster-slave \
        --cluster-master-id 5be0edaf74d1eec50bbe675f9698397023a943b7
    # 上述命令中,10.0.4.111:6371 是待添加实例,10.0.4.110:6371 是已存在集群中的任一实例
    # --cluster-slave 指定添加 slave 实例
    # --cluster-master-id 指定 10.0.4.110:6371 的 id,即本次添加实例的 master
  • 输出如下图

  • 添加 slave 实例 10.0.4.111:6372,同步 master 10.0.4.110:6372

    1
    2
    3
    4
    
    /opt/redis_cluster/bin/redis-cli \
        --cluster add-node 10.0.4.111:6372 10.0.4.110:6371 \
        --cluster-slave \
        --cluster-master-id 96f2225fdd054d8a16b31726fd1eb0b66c612646
  • 输出如下图

  • 添加 slave 实例 10.0.4.111:6373,同步 master 10.0.4.110:6373

    1
    2
    3
    4
    
    /opt/redis_cluster/bin/redis-cli \
        --cluster add-node 10.0.4.111:6373 10.0.4.110:6371 \
        --cluster-slave \
        --cluster-master-id cd60c238e66c8b4a31355bee4610444a47acef86
  • 输出如下图

  • 查看集群当前实例

    1
    
    /opt/redis_cluster/bin/redis-cli -p 6371 cluster nodes
  • 输出如下图

参考