• Linux docker 搭建Redis主从复制集群、新增(扩容)主/从节点、删除(缩容)主/从节点


    Docker Redis集群搭建

    docker的redis集群模式自带哨兵模式,宕机自动主从切换,因此不用启动哨兵。

    下载镜像并创建数据存放文件夹

    docker pull redis:6.2.6
    mkdir -p /docker_data/redis/
    

    启动容器并把数据映射到宿主机数据存放文件夹

    docker run -d --net host --name redis-6381 --privileged=true --restart=always -v /docker_data/redis/data6381:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6381
    
    docker run -d --net host --name redis-6382 --privileged=true --restart=always -v /docker_data/redis/data6382:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6382
    
    docker run -d --net host --name redis-6383 --privileged=true --restart=always -v /docker_data/redis/data6383:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6383
    
    docker run -d --net host --name redis-6384 --privileged=true --restart=always -v /docker_data/redis/data6384:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6384
    
    docker run -d --net host --name redis-6385 --privileged=true --restart=always -v /docker_data/redis/data6385:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6385
    
    docker run -d --net host --name redis-6386 --privileged=true --restart=always -v /docker_data/redis/data6386:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6386
    

    进入其中一台容器,查看集群是否搭建成功

    docker exec -it redis-6381 /bin/bash
    root@centos7:/data# redis-cli --cluster create --cluster-replicas 1 192.168.56.11:6381 192.168.56.11:6382 192.168.56.11:6383 192.168.56.11:6384 192.168.56.11:6385 192.168.56.11:6386
    Can I set the above configuration? (type 'yes' to accept): yes	#输入yes
    #打印下面两行说明集群搭建成功
    [OK] All nodes agree about slots configuration.
    [OK] All 16384 slots covered.
    #进入集群
    root@centos7:/data# redis-cli -c -p 6381
    #输入CLUSTER NODES打印集群信息
    127.0.0.1:6381> CLUSTER NODES
    292a262f5776b38183d20f18f7223784f4132c45 192.168.56.11:6385@16385 slave 3ef7ce3bd0ad513f38932df78a1431604edc5702 0 1664417153000 3 connected
    69c41dd5e58bdc80cf5473463e4bc746e076e0f8 192.168.56.11:6382@16382 master - 0 1664417153522 2 connected 5461-10922
    06f9840971ed78e36fe3ac8066569035e5ce5f8c 192.168.56.11:6386@16386 slave b6a1d3cfb99642adde37a21b552cf7d0087d4ede 0 1664417154532 1 connected
    b6a1d3cfb99642adde37a21b552cf7d0087d4ede 192.168.56.11:6381@16381 myself,master - 0 1664417152000 1 connected 0-5460
    4a75693d4fd1e1ab3f0ce8685bbe2712ab3e8742 192.168.56.11:6384@16384 slave 69c41dd5e58bdc80cf5473463e4bc746e076e0f8 0 1664417154000 2 connected
    3ef7ce3bd0ad513f38932df78a1431604edc5702 192.168.56.11:6383@16383 master - 0 1664417151504 3 connected 10923-16383
    #退出redis-cli
    exit
    #退出容器
    exit
    

    扩容

    1. 新增主节点,如:6387
    2. 给新主节点分配槽位
    3. 新增从节点,如:6388从机绑定6387主机ID

    Redis集群新增主机

    docker run -d --net host --name redis-6387 --privileged=true --restart=always -v /docker_data/redis/data6387:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6387
    
    docker run -d --net host --name redis-6388 --privileged=true --restart=always -v /docker_data/redis/data6388:/data redis:6.2.6 --cluster-enabled yes --appendonly yes --port 6388
    
    #进入容器
    docker exec -it redis-6381 /bin/bash
    
    #把6387加入集群
    root@centos:/data# redis-cli --cluster add-node 192.168.56.11:6387 192.168.56.11:6381
    
    #成功则打印
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    >>> Send CLUSTER MEET to node 192.168.56.11:6387 to make it join the cluster.
    [OK] New node added correctly.
    
    #查看6387是否加入集群
    root@mariadb:/data# redis-cli --cluster check 192.168.56.11:6381
    192.168.56.11:6381 (b6a1d3cf...) -> 0 keys | 5461 slots | 1 slaves.
    192.168.56.11:6382 (69c41dd5...) -> 0 keys | 5462 slots | 1 slaves.
    192.168.56.11:6387 (1655424a...) -> 0 keys | 0 slots | 0 slaves.
    192.168.56.11:6383 (3ef7ce3b...) -> 0 keys | 5461 slots | 1 slaves.
    [OK] 0 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.56.11:6381)
    M: b6a1d3cfb99642adde37a21b552cf7d0087d4ede 192.168.56.11:6381
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    S: 292a262f5776b38183d20f18f7223784f4132c45 192.168.56.11:6385
       slots: (0 slots) slave
       replicates 3ef7ce3bd0ad513f38932df78a1431604edc5702
    M: 69c41dd5e58bdc80cf5473463e4bc746e076e0f8 192.168.56.11:6382
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 06f9840971ed78e36fe3ac8066569035e5ce5f8c 192.168.56.11:6386
       slots: (0 slots) slave
       replicates b6a1d3cfb99642adde37a21b552cf7d0087d4ede
    M: 1655424ad41a079aff535f8c920c186213dccbea 192.168.56.11:6387
       slots: (0 slots) master
    S: 4a75693d4fd1e1ab3f0ce8685bbe2712ab3e8742 192.168.56.11:6384
       slots: (0 slots) slave
       replicates 69c41dd5e58bdc80cf5473463e4bc746e076e0f8
    M: 3ef7ce3bd0ad513f38932df78a1431604edc5702 192.168.56.11:6383
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    
    #重新分配槽位
    root@mariadb:/data# redis-cli --cluster reshard 192.168.56.11:6381
    M: b6a1d3cfb99642adde37a21b552cf7d0087d4ede 192.168.56.11:6381
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    S: 292a262f5776b38183d20f18f7223784f4132c45 192.168.56.11:6385
       slots: (0 slots) slave
       replicates 3ef7ce3bd0ad513f38932df78a1431604edc5702
    M: 69c41dd5e58bdc80cf5473463e4bc746e076e0f8 192.168.56.11:6382
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 06f9840971ed78e36fe3ac8066569035e5ce5f8c 192.168.56.11:6386
       slots: (0 slots) slave
       replicates b6a1d3cfb99642adde37a21b552cf7d0087d4ede
    M: 1655424ad41a079aff535f8c920c186213dccbea 192.168.56.11:6387
       slots: (0 slots) master
    S: 4a75693d4fd1e1ab3f0ce8685bbe2712ab3e8742 192.168.56.11:6384
       slots: (0 slots) slave
       replicates 69c41dd5e58bdc80cf5473463e4bc746e076e0f8
    M: 3ef7ce3bd0ad513f38932df78a1431604edc5702 192.168.56.11:6383
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    
    #分配槽位数量=16384/master数量,现在加上6387是4台master,因此除4
    How many slots do you want to move (from 1 to 16384)? 4096
    
    #node ID是新增节点的ID,由redis-cli --cluster reshard 192.168.56.11:6381命令查出来的新增节点6387的ID
    What is the receiving node ID? 1655424ad41a079aff535f8c920c186213dccbea
    
    #all所有主节点分配给新节点部分槽位
    Please enter all the source node IDs.
      Type 'all' to use all the nodes as source nodes for the hash slots.
      Type 'done' once you entered all the source nodes IDs.
    Source node #1: all
    
    #你想继续进行拟议的重新洗牌计划吗?yes
    Do you want to proceed with the proposed reshard plan (yes/no)? yes
    
    #分配完成后重新查看节点信息,新节点槽位不连续是因为从其它所有主节点各自分配一部分给新节点。
    root@centos:/data# redis-cli --cluster check 192.168.56.11:6381
    192.168.56.11:6381 (b6a1d3cf...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.56.11:6382 (69c41dd5...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.56.11:6387 (1655424a...) -> 0 keys | 4096 slots | 0 slaves.
    192.168.56.11:6383 (3ef7ce3b...) -> 0 keys | 4096 slots | 1 slaves.
    [OK] 0 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.56.11:6381)
    M: b6a1d3cfb99642adde37a21b552cf7d0087d4ede 192.168.56.11:6381
       slots:[1365-5460] (4096 slots) master
       1 additional replica(s)
    S: 292a262f5776b38183d20f18f7223784f4132c45 192.168.56.11:6385
       slots: (0 slots) slave
       replicates 3ef7ce3bd0ad513f38932df78a1431604edc5702
    M: 69c41dd5e58bdc80cf5473463e4bc746e076e0f8 192.168.56.11:6382
       slots:[6827-10922] (4096 slots) master
       1 additional replica(s)
    S: 06f9840971ed78e36fe3ac8066569035e5ce5f8c 192.168.56.11:6386
       slots: (0 slots) slave
       replicates b6a1d3cfb99642adde37a21b552cf7d0087d4ede
    M: 1655424ad41a079aff535f8c920c186213dccbea 192.168.56.11:6387
       slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
    S: 4a75693d4fd1e1ab3f0ce8685bbe2712ab3e8742 192.168.56.11:6384
       slots: (0 slots) slave
       replicates 69c41dd5e58bdc80cf5473463e4bc746e076e0f8
    M: 3ef7ce3bd0ad513f38932df78a1431604edc5702 192.168.56.11:6383
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    新增Redis集群从机

    add-node 新节点 主机节点
    –cluster-slave 表示新节点作为从机
    –cluster-master-id是主机6387由root@centos:/data# redis-cli --cluster check 192.168.56.11:6381查询6387的主机ID

    root@centos:/data# redis-cli --cluster add-node 192.168.56.11:6388 192.168.56.11:6387 --cluster-slave --cluster-master-id 1655424ad41a079aff535f8c920c186213dccbea
    
    #成功则打印
    >>> Adding node 192.168.56.11:6388 to cluster 192.168.56.11:6387
    >>> Performing Cluster Check (using node 192.168.56.11:6387)
    M: 1655424ad41a079aff535f8c920c186213dccbea 192.168.56.11:6387
       slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
    S: 4a75693d4fd1e1ab3f0ce8685bbe2712ab3e8742 192.168.56.11:6384
       slots: (0 slots) slave
       replicates 69c41dd5e58bdc80cf5473463e4bc746e076e0f8
    M: 69c41dd5e58bdc80cf5473463e4bc746e076e0f8 192.168.56.11:6382
       slots:[6827-10922] (4096 slots) master
       1 additional replica(s)
    M: b6a1d3cfb99642adde37a21b552cf7d0087d4ede 192.168.56.11:6381
       slots:[1365-5460] (4096 slots) master
       1 additional replica(s)
    S: 06f9840971ed78e36fe3ac8066569035e5ce5f8c 192.168.56.11:6386
       slots: (0 slots) slave
       replicates b6a1d3cfb99642adde37a21b552cf7d0087d4ede
    M: 3ef7ce3bd0ad513f38932df78a1431604edc5702 192.168.56.11:6383
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
    S: 292a262f5776b38183d20f18f7223784f4132c45 192.168.56.11:6385
       slots: (0 slots) slave
       replicates 3ef7ce3bd0ad513f38932df78a1431604edc5702
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    >>> Send CLUSTER MEET to node 192.168.56.11:6388 to make it join the cluster.
    Waiting for the cluster to join
    
    >>> Configure node as replica of 192.168.56.11:6387.
    [OK] New node added correctly.
    
    
    #查看新节点是否加入集群
    root@centos:/data# redis-cli --cluster check 192.168.56.11:6381
    192.168.56.11:6381 (b6a1d3cf...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.56.11:6382 (69c41dd5...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.56.11:6387 (1655424a...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.56.11:6383 (3ef7ce3b...) -> 0 keys | 4096 slots | 1 slaves.
    [OK] 0 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.56.11:6381)
    M: b6a1d3cfb99642adde37a21b552cf7d0087d4ede 192.168.56.11:6381
       slots:[1365-5460] (4096 slots) master
       1 additional replica(s)
    S: 292a262f5776b38183d20f18f7223784f4132c45 192.168.56.11:6385
       slots: (0 slots) slave
       replicates 3ef7ce3bd0ad513f38932df78a1431604edc5702
    M: 69c41dd5e58bdc80cf5473463e4bc746e076e0f8 192.168.56.11:6382
       slots:[6827-10922] (4096 slots) master
       1 additional replica(s)
    S: 06f9840971ed78e36fe3ac8066569035e5ce5f8c 192.168.56.11:6386
       slots: (0 slots) slave
       replicates b6a1d3cfb99642adde37a21b552cf7d0087d4ede
    M: 1655424ad41a079aff535f8c920c186213dccbea 192.168.56.11:6387
       slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
       1 additional replica(s)
    S: b208730b6296fddb690c395aeaf0c7b090525ea3 192.168.56.11:6388
       slots: (0 slots) slave
       replicates 1655424ad41a079aff535f8c920c186213dccbea
    S: 4a75693d4fd1e1ab3f0ce8685bbe2712ab3e8742 192.168.56.11:6384
       slots: (0 slots) slave
       replicates 69c41dd5e58bdc80cf5473463e4bc746e076e0f8
    M: 3ef7ce3bd0ad513f38932df78a1431604edc5702 192.168.56.11:6383
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    缩容

    1. 删除从节点,如:6388
    2. 清除槽位,重新分配
    3. 删除主节点,如:6387
    4. 恢复三主三从

    删除从节点

    id是6388的ID,可以由root@centos:/data# redis-cli --cluster check 192.168.56.11:6388查询出来

    #进入docker容器
    root@centos:/data# redis-cli --cluster del-node 192.168.56.11:6388 b208730b6296fddb690c395aeaf0c7b090525ea3
    #查看是否删除成功
    root@centos:/data# redis-cli --cluster check 192.168.56.11:6381
    

    清除槽位,重新分配

    root@centos:/data# redis-cli --cluster reshard 192.168.56.11:6381
    
    >>> Performing Cluster Check (using node 192.168.56.11:6381)
    M: b6a1d3cfb99642adde37a21b552cf7d0087d4ede 192.168.56.11:6381
       slots:[1365-5460] (4096 slots) master
       1 additional replica(s)
    S: 292a262f5776b38183d20f18f7223784f4132c45 192.168.56.11:6385
       slots: (0 slots) slave
       replicates 3ef7ce3bd0ad513f38932df78a1431604edc5702
    M: 69c41dd5e58bdc80cf5473463e4bc746e076e0f8 192.168.56.11:6382
       slots:[6827-10922] (4096 slots) master
       1 additional replica(s)
    S: 06f9840971ed78e36fe3ac8066569035e5ce5f8c 192.168.56.11:6386
       slots: (0 slots) slave
       replicates b6a1d3cfb99642adde37a21b552cf7d0087d4ede
    M: 1655424ad41a079aff535f8c920c186213dccbea 192.168.56.11:6387
       slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
    S: 4a75693d4fd1e1ab3f0ce8685bbe2712ab3e8742 192.168.56.11:6384
       slots: (0 slots) slave
       replicates 69c41dd5e58bdc80cf5473463e4bc746e076e0f8
    M: 3ef7ce3bd0ad513f38932df78a1431604edc5702 192.168.56.11:6383
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    
    #给了多少还多少,上面分配了4096,归还4096
    How many slots do you want to move (from 1 to 16384)? 4096
    
    #归还给哪个主节点的ID,如:6381主节点的ID
    What is the receiving node ID? b6a1d3cfb99642adde37a21b552cf7d0087d4ede
    Please enter all the source node IDs.
      Type 'all' to use all the nodes as source nodes for the hash slots.
      Type 'done' once you entered all the source nodes IDs.
    
    #需要删除的节点ID,如:6387
    Source node #1: 1655424ad41a079aff535f8c920c186213dccbea
    #执行
    Source node #2: done
    
    #你想继续实施拟议的重新进货计划吗?yes
    Do you want to proceed with the proposed reshard plan (yes/no)? yes
    
    #查看集群槽位是否归还给6381主节点
    root@centos:/data# redis-cli --cluster check 192.168.56.11:6381
    192.168.56.11:6381 (b6a1d3cf...) -> 0 keys | 8192 slots | 1 slaves.
    192.168.56.11:6382 (69c41dd5...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.56.11:6387 (1655424a...) -> 0 keys | 0 slots | 0 slaves.
    192.168.56.11:6383 (3ef7ce3b...) -> 0 keys | 4096 slots | 1 slaves.
    [OK] 0 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.56.11:6381)
    M: b6a1d3cfb99642adde37a21b552cf7d0087d4ede 192.168.56.11:6381
       slots:[0-6826],[10923-12287] (8192 slots) master
       1 additional replica(s)
    S: 292a262f5776b38183d20f18f7223784f4132c45 192.168.56.11:6385
       slots: (0 slots) slave
       replicates 3ef7ce3bd0ad513f38932df78a1431604edc5702
    M: 69c41dd5e58bdc80cf5473463e4bc746e076e0f8 192.168.56.11:6382
       slots:[6827-10922] (4096 slots) master
       1 additional replica(s)
    S: 06f9840971ed78e36fe3ac8066569035e5ce5f8c 192.168.56.11:6386
       slots: (0 slots) slave
       replicates b6a1d3cfb99642adde37a21b552cf7d0087d4ede
    M: 1655424ad41a079aff535f8c920c186213dccbea 192.168.56.11:6387
       slots: (0 slots) master
    S: 4a75693d4fd1e1ab3f0ce8685bbe2712ab3e8742 192.168.56.11:6384
       slots: (0 slots) slave
       replicates 69c41dd5e58bdc80cf5473463e4bc746e076e0f8
    M: 3ef7ce3bd0ad513f38932df78a1431604edc5702 192.168.56.11:6383
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    删除主节点,如:6387(确保6387已归还槽位给其它主节点

    root@centos:/data# redis-cli --cluster del-node 192.168.56.11:6387 1655424ad41a079aff535f8c920c186213dccbea
    
    #查看是否删除成功
    root@centos:/data# redis-cli --cluster check 192.168.56.11:6381
    192.168.56.11:6381 (b6a1d3cf...) -> 0 keys | 8192 slots | 1 slaves.
    192.168.56.11:6382 (69c41dd5...) -> 0 keys | 4096 slots | 1 slaves.
    192.168.56.11:6383 (3ef7ce3b...) -> 0 keys | 4096 slots | 1 slaves.
    [OK] 0 keys in 3 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 192.168.56.11:6381)
    M: b6a1d3cfb99642adde37a21b552cf7d0087d4ede 192.168.56.11:6381
       slots:[0-6826],[10923-12287] (8192 slots) master
       1 additional replica(s)
    S: 292a262f5776b38183d20f18f7223784f4132c45 192.168.56.11:6385
       slots: (0 slots) slave
       replicates 3ef7ce3bd0ad513f38932df78a1431604edc5702
    M: 69c41dd5e58bdc80cf5473463e4bc746e076e0f8 192.168.56.11:6382
       slots:[6827-10922] (4096 slots) master
       1 additional replica(s)
    S: 06f9840971ed78e36fe3ac8066569035e5ce5f8c 192.168.56.11:6386
       slots: (0 slots) slave
       replicates b6a1d3cfb99642adde37a21b552cf7d0087d4ede
    S: 4a75693d4fd1e1ab3f0ce8685bbe2712ab3e8742 192.168.56.11:6384
       slots: (0 slots) slave
       replicates 69c41dd5e58bdc80cf5473463e4bc746e076e0f8
    M: 3ef7ce3bd0ad513f38932df78a1431604edc5702 192.168.56.11:6383
       slots:[12288-16383] (4096 slots) master
       1 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    
  • 相关阅读:
    ISIS 协议常用基本配置总结
    用一个极致简单的场景演练领域建模
    Python是什么?一篇文章带你全面了解Python
    【Golang】数组 && 切片
    王牌代码静态测试工具Helix QAC 2022.2 中的新增功能(1)
    docker安装配置elasticsearch,kibana和IK分词器
    详细分析Uniapp中的轮播图基本知识(附Demo)
    Simulink中如何使用数组以及创建数组、对数组索引、赋值
    java-net-php-python-springboot办公自动化系统计算机毕业设计程序
    强化学习——学习笔记3
  • 原文地址:https://blog.csdn.net/weixin_43933728/article/details/127093742