配置 redis 集群

  1. 三主三从集群设置
Master1(6381) Master2(6382) Master3(6383)
Slave1(6384)  Slave2(6385)  Slave3(6386)
  1. 拉取镜像启动容器
docker pull redis:6.0.8
docker run -d --name redis-node-1 --net host --privileged=true -v  /data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6381
docker run -d --name redis-node-2 --net host --privileged=true -v /data/redis/share/redis-node-2:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6382
docker run -d --name redis-node-3 --net host --privileged=true -v /data/redis/share/redis-node-3:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6383
docker run -d --name redis-node-4 --net host --privileged=true -v /data/redis/share/redis-node-4:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6384
docker run -d --name redis-node-5 --net host --privileged=true -v /data/redis/share/redis-node-5:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6385
docker run -d --name redis-node-6 --net host --privileged=true -v /data/redis/share/redis-node-6:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6386

docker ps
    CONTAINER ID   IMAGE          NAMES
    ded0061df2ce   redis:6.0.8    redis-node-6
    402b0073a401   redis:6.0.8    redis-node-5
    78f3028f13be   redis:6.0.8    redis-node-4
    5d96881f585d   redis:6.0.8    redis-node-3
    d087d97a7507   redis:6.0.8    redis-node-2
    beb8f99d6645   redis:6.0.8    redis-node-1
  1. 主从配置
# --cluster-replicas 1 为每个master分配一个slave

docker exec -it redis-node-1 /bin/bash
redis-cli --cluster create 192.168.2.241:6381 192.168.2.241:6382 192.168.2.241:6383 192.168.2.241:6384 192.168.2.241:6385 192.168.2.241:6386 --cluster-replicas 1
  1. 查看集群
# 以 6381 为切入点进入
root@test:/data# redis-cli -p 6381(默认6379)
    127.0.0.1:6381> CLUSTER INFO
        cluster_state:ok
        cluster_slots_assigned:16384
        cluster_slots_ok:16384
        cluster_slots_pfail:0
        cluster_slots_fail:0
        cluster_known_nodes:6
        cluster_size:3
        cluster_current_epoch:6
        cluster_my_epoch:1
        cluster_stats_messages_ping_sent:314
        cluster_stats_messages_pong_sent:309
        cluster_stats_messages_sent:623
        cluster_stats_messages_ping_received:304
        cluster_stats_messages_pong_received:314
        cluster_stats_messages_meet_received:5
        cluster_stats_messages_received:623

    127.0.0.1:6381> CLUSTER NODES
        661ef7b3f4730b71f2d76d87e6baad2229e01399 192.168.2.241:6383@16383 master - 0 1678706342394 3 connected 10923-16383
        c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381@16381 myself,master - 0 1678706341000 1 connected 0-5460
        15807cacb21f0431d28113c094d961ed9561bd57 192.168.2.241:6386@16386 slave 661ef7b3f4730b71f2d76d87e6baad2229e01399 0 1678706343396 3 connected
        140d21198470f2e066ac376f560c0641c98327ae 192.168.2.241:6382@16382 master - 0 1678706342000 2 connected 5461-10922
        9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 192.168.2.241:6384@16384 slave c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 0 1678706341392 1 connected
        27d931c81c5245ac12c2d379d8af22756dfe0f1b 192.168.2.241:6385@16385 slave 140d21198470f2e066ac376f560c0641c98327ae 0 1678706341000 2 connected
    127.0.0.1:6381>
  1. 数据读写存储
docker exec -it redis-node-1 /bin/bash
    redis-cli -p 6381 -c(优化路由, 以集群而不是单机模式)
        127.0.0.1:6381> KEYS *
        127.0.0.1:6381> FLUSHALL

        127.0.0.1:6381> set k1 v1
            -> Redirected to slot [12706] located at 192.168.2.241:6383
            OK

        192.168.2.241:6383> set k2 v2
            -> Redirected to slot [449] located at 192.168.2.241:6381
            OK

        192.168.2.241:6381> set k3 v3
            OK

        192.168.2.241:6381> set k4 v4
            -> Redirected to slot [8455] located at 192.168.2.241:6382
            OK

    # 查看集群信息
    redis-cli --cluster check 192.168.2.241:6381
        192.168.2.241:6381 (c0c7c1a9...) -> 2 keys | 5461 slots | 1 slaves.
        192.168.2.241:6383 (661ef7b3...) -> 1 keys | 5461 slots | 1 slaves.
        192.168.2.241:6382 (140d2119...) -> 1 keys | 5462 slots | 1 slaves.
        [OK] 4 keys in 3 masters.
        0.00 keys per slot on average.
        >>> Performing Cluster Check (using node 192.168.2.241:6381)
        M: c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381
        slots:[0-5460] (5461 slots) master
        1 additional replica(s)
        M: 661ef7b3f4730b71f2d76d87e6baad2229e01399 192.168.2.241:6383
        slots:[10923-16383] (5461 slots) master
        1 additional replica(s)
        S: 15807cacb21f0431d28113c094d961ed9561bd57 192.168.2.241:6386
        slots: (0 slots) slave
        replicates 661ef7b3f4730b71f2d76d87e6baad2229e01399
        M: 140d21198470f2e066ac376f560c0641c98327ae 192.168.2.241:6382
        slots:[5461-10922] (5462 slots) master
        1 additional replica(s)
        S: 9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 192.168.2.241:6384
        slots: (0 slots) slave
        replicates c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f
        S: 27d931c81c5245ac12c2d379d8af22756dfe0f1b 192.168.2.241:6385
        slots: (0 slots) slave
        replicates 140d21198470f2e066ac376f560c0641c98327ae
        [OK] All nodes agree about slots configuration.
        >>> Check for open slots...
        >>> Check slots coverage...
        [OK] All 16384 slots covered.

  1. 主从切换查看
# 停止一个主节点
 docker stop redis-node-1
 docker exec -it redis-node-2 /bin/bash
    redis-cli -p 6382 -c
        127.0.0.1:6382> CLUSTER NODES
            661ef7b3f4730b71f2d76d87e6baad2229e01399 192.168.2.241:6383@16383 master - 0 1678708057000 3 connected 10923-16383
            140d21198470f2e066ac376f560c0641c98327ae 192.168.2.241:6382@16382 myself,master - 0 1678708053000 2 connected 5461-10922
            9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 192.168.2.241:6384@16384 master - 0 1678708057686 7 connected 0-5460
            27d931c81c5245ac12c2d379d8af22756dfe0f1b 192.168.2.241:6385@16385 slave 140d21198470f2e066ac376f560c0641c98327ae 0 1678708056684 2 connected
            c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381@16381 master,fail - 1678707913388 1678707909000 1 disconnected
            15807cacb21f0431d28113c094d961ed9561bd57 192.168.2.241:6386@16386 slave 661ef7b3f4730b71f2d76d87e6baad2229e01399 0 1678708054681 3 connected

        192.168.2.241:6383> get k1
            "v1"

        192.168.2.241:6383> get k2
            -> Redirected to slot [449] located at 192.168.2.241:6384
            "v2"

        192.168.2.241:6384> get k3
            "v3"

        192.168.2.241:6384> get k4
            -> Redirected to slot [8455] located at 192.168.2.241:6382
            "v4"

# 启动停止的主节点
 docker start redis-node-1
 docker exec -it redis-node-1 /bin/bash
    redis-cli -p 6381 -c
        127.0.0.1:6381> CLUSTER NODES # 还是slave, 要想6381还是master, 可以停掉6384再启动
            27d931c81c5245ac12c2d379d8af22756dfe0f1b 192.168.2.241:6385@16385 slave 140d21198470f2e066ac376f560c0641c98327ae 0 1678708466000 2 connected
            661ef7b3f4730b71f2d76d87e6baad2229e01399 192.168.2.241:6383@16383 master - 0 1678708467908 3 connected 10923-16383
            c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381@16381 myself,slave 9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 0 1678708467000 7 connected
            140d21198470f2e066ac376f560c0641c98327ae 192.168.2.241:6382@16382 master - 0 1678708464000 2 connected 5461-10922
            9bc832d8b0f8eb5643653f0ce7c8346a0a13d28c 192.168.2.241:6384@16384 master - 0 1678708465905 7 connected 0-5460
            15807cacb21f0431d28113c094d961ed9561bd57 192.168.2.241:6386@16386 slave 661ef7b3f4730b71f2d76d87e6baad2229e01399 0 1678708466906 3 connected


  1. 扩容到 4 主 4 从
docker run -d --name redis-node-7 --net host --privileged=true -v /data/redis/share/redis-node-7:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6387
docker run -d --name redis-node-8 --net host --privileged=true -v /data/redis/share/redis-node-8:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6388
docker exec -it redis-node-7 /bin/bash
    # 添加一个master节点
    redis-cli --cluster add-node 192.168.2.241:6387 192.168.2.241:6381(相当于通知集群的老大6381)
    # 查看
    redis-cli --cluster check 192.168.2.241:6381
    # 重新分配槽位
    redis-cli --cluster reshard 192.168.2.241:6381
        ...How many slots do you want to move (from 1 to 16384)? 4096 (16384/Master的个数)
        ...What is the receiving node ID? f23674b12839887c47761c40017fd1e44a2a3c45 (6387的id)
        ...Source node #1: all
    # 查看
    redis-cli --cluster check 192.168.2.241:6381

    # 为什么6387是3个新的区间, 以前的还是连续?
    # 重新分配成本太高, 所以前3家各自匀出来一部分, 从6381/6382/6383三个旧节点分别匀出1364个坑位给新节点6387
    # M: f23674b12839887c47761c40017fd1e44a2a3c45 192.168.2.241:6387
    # slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master

    # 添加从节点
    redis-cli --cluster add-node 192.168.2.241:6388 192.168.2.241:6387 --cluster-slave --cluster-master-id f23674b12839887c47761c40017fd1e44a2a3c45
    # 查看
    redis-cli --cluster check 192.168.2.241:6381
  1. 缩容 6387 6388 下线
# 移除节点
redis-cli --cluster del-node 192.168.2.241:6388 6811637e126da89ec173afe67d2b2f14b4a27b1e(6388的id)

# 将6387的槽号清空, 重新分配, 本例将清出来的槽号都给6381
redis-cli --cluster reshard 192.168.2.241:6381
    ...How many slots do you want to move (from 1 to 16384)? 4096
    ...What is the receiving node ID? c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f(6381的id)
    ...Source node #1: f23674b12839887c47761c40017fd1e44a2a3c45(6387的id, 告知删除哪个)
    ...Source node #2: done

# 查看
redis-cli --cluster check 192.168.2.241:6381
# M: c0c7c1a9029eb5dc4b13c0807d4bd9f1a394a54f 192.168.2.241:6381
#    slots:[0-6826],[10923-12287] (8192 slots) master
#    1 additional replica(s)
...
# M: f23674b12839887c47761c40017fd1e44a2a3c45 192.168.2.241:6387
#    slots: (0 slots) master

# 删除6387节点
redis-cli --cluster del-node 192.168.2.241:6387 f23674b12839887c47761c40017fd1e44a2a3c45

# 查看
redis-cli --cluster check 192.168.2.241:6381
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。