• CentOS 7 Redis cluster 集群安装


    环境准备

    本次只是安装实验,如想了解 集群原理
    
    引申阅读:redis-cluster原理
    https://www.cnblogs.com/williamjie/p/9505180.html
    https://www.cnblogs.com/mengchunchen/p/10059436.html
    https://www.cnblogs.com/williamjie/p/11132211.html
    
    系统版本 IP 主机名 redis 版本 redis 端口
    CentOS 7.5 10.0.20.21 node01 redis-5.0.7 7001,7002
    CentOS 7.5 10.0.20.22 node02 redis-5.0.7 7003,7004
    CentOS 7.5 10.0.20.23 node03 redis-5.0.7 7005,7006

    每个节点启动 两个 redis 实例,本次仅为演示,如生产使用,建议单节点,请根据实际情况进行选择。

    安装编译环境

    下载好 redis 源码包后,需要手动编译,然后才可使用

    在所有节点安装:

    yum install gcc gcc-c++ -y
    

    Redis 安装与配置

    下载 编译 安装

    常规操作,具体不赘述

    wget http://download.redis.io/releases/redis-5.0.7.tar.gz
    tar xf redis-5.0.7.tar.gz
    cd redis-5.0.7
    make MALLOC=libc
    make install
    

    systemd 脚本,在所有节点都添加

    cat /etc/systemd/system/redis@.service
    
    [Unit]
    Description=Redis persistent key-value database
    After=network.target
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    ExecStart=/usr/local/bin/redis-server /etc/redis/%I/redis.conf --supervised systemd
    ExecStop=/usr/local/bin/redis-cli -p %I shutdown
    Type=notify
    User=root
    Group=root
    RuntimeDirectory=redis
    RuntimeDirectoryMode=0755
    
    [Install]
    WantedBy=multi-user.target
    

    脚本自行修改。

    创建集群所需目录

    node01:

    mkdir /etc/redis/{7001,7002} -pv
    mkdir /data/redis/{7001,7002} -pv
    

    node2

    mkdir /etc/redis/{7003,7004} -pv
    mkdir /data/redis/{7003,7004} -pv
    

    node3

    mkdir /etc/redis/{7005,7006} -pv
    mkdir /data/redis/{7005,7006} -pv
    

    配置文件

    完整的配置文件在编译安装目录下有一个redis.conf 配置文件;

    本次预先创建一个配置文件模板,然后基于这个文件来修改配置

    在三台节点均操作:

    cat >/etc/redis/redis.conf<<EOF
    daemonize yes
    bind 0.0.0.0
    port 6379
    logfile "/var/log/redis_6379.log"
    dir "/data/redis/6379"
    appendonly yes
    cluster-enabled yes
    cluster-config-file nodes-6379.conf
    cluster-node-timeout 5000
    EOF
    

    然后根据前面设定的端口,对每个节点的配置文件进行修改如下:

    node01

    sed  "s@6379@7001@g" /etc/redis/redis.conf > /etc/redis/7001/redis.conf
    sed  "s@6379@7002@g" /etc/redis/redis.conf > /etc/redis/7002/redis.conf
    

    node02

    sed  "s@6379@7003@g" /etc/redis/redis.conf > /etc/redis/7003/redis.conf
    sed  "s@6379@7004@g" /etc/redis/redis.conf > /etc/redis/7004/redis.conf
    

    node03

    sed  "s@6379@7005@g" /etc/redis/redis.conf > /etc/redis/7005/redis.conf
    sed  "s@6379@7006@g" /etc/redis/redis.conf > /etc/redis/7006/redis.conf
    

    启动所有节点redis

    node01

    systemctl start redis@7001
    systemctl start redis@7002
    
    ps aux|grep redis
    root      9072  0.0  0.4 144536  4928 ?        Ssl  13:23   0:04 /usr/local/bin/redis-server 0.0.0.0:7001 [cluster]
    root      9282  0.0  0.4 144536  4900 ?        Ssl  15:22   0:00 /usr/local/bin/redis-server 0.0.0.0:7002 [cluster]
    

    node02

    systemctl start redis@7003
    systemctl start redis@7004
    
    ps aux | grep redis
    root      9610  0.0  0.4 144536  4900 ?        Rsl  15:27   0:00 /usr/local/bin/redis-server 0.0.0.0:7003 [cluster]
    root      9620  0.0  0.4 144536  4852 ?        Ssl  15:27   0:00 /usr/local/bin/redis-server 0.0.0.0:7004 [cluster]
    

    node03

    systemctl start redis@7005
    systemctl start redis@7006
    
    ps aux | grep redis
    root      9469  0.0  0.4 144536  4848 ?        Ssl  15:28   0:00 /usr/local/bin/redis-server 0.0.0.0:7005 [cluster]
    root      9479  0.0  0.4 144536  4948 ?        Ssl  15:28   0:00 /usr/local/bin/redis-server 0.0.0.0:7006 [cluster]
    

    创建集群

    注意事项

    当前环境使用的是三台节点,每个节点上运行 两个 redis 实例;
    依照 redis-cluster 的原理,要避免同一个节点上的两个实例为主从关系;
    否则当该节点宕机后,无法实现主从切换,存放在该主从的数据,就会丢失数据

    创建集群的命令中,建立的是三主三从,那么顺序就是:

    1. 前三个实例是master主节点
    2. 第四个实例是第一个master 的从节点
    3. 第五个实例是第二个master 的从节点
    4. 第六个实例是第三个master 的从节点

    所以对应的命令顺序为: redis-cli --cluster create 10.0.20.21:7001 10.0.20.22:7003 10.0.20.23:7005 10.0.20.23:7006 10.0.20.21:7002 10.0.20.22:7004 --cluster-replicas 1

    [root@node01 ~]# redis-cli --cluster create 10.0.20.21:7001 10.0.20.22:7003 10.0.20.23:7005 10.0.20.23:7006 10.0.20.21:7002 10.0.20.22:7004  --cluster-replicas 1
    >>> Performing hash slots allocation on 6 nodes...
    Master[0] -> Slots 0 - 5460
    Master[1] -> Slots 5461 - 10922
    Master[2] -> Slots 10923 - 16383
    Adding replica 10.0.20.22:7004 to 10.0.20.21:7001
    Adding replica 10.0.20.23:7006 to 10.0.20.22:7003
    Adding replica 10.0.20.21:7002 to 10.0.20.23:7005
    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[0-5460] (5461 slots) master
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[5461-10922] (5462 slots) master
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[10923-16383] (5461 slots) master
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       replicates fde786908139df0b962fa027511a1a4329582d75
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    Can I set the above configuration? (type 'yes' to accept): yes
    >>> Nodes configuration updated
    >>> Assign a different config epoch to each node
    >>> Sending CLUSTER MEET messages to join the cluster
    Waiting for the cluster to join
    ...
    >>> Performing Cluster Check (using node 10.0.20.21:7001)
    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       slots: (0 slots) slave
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       slots: (0 slots) slave
       replicates fde786908139df0b962fa027511a1a4329582d75
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       slots: (0 slots) slave
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    集群输出解析

    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[0-5460] (5461 slots) master
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[5461-10922] (5462 slots) master
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[10923-16383] (5461 slots) master
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       replicates fde786908139df0b962fa027511a1a4329582d75
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    

    上面创建集群,输出了一段信息,M 开头的为 master 实例,S 开头的为 slave 实例;

    区分主从

    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[0-5460] (5461 slots) master
    
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    

    M 后面有遗传HASH值,所有的实例都会有,S 下面有 replicates 对应的一个Hash值,这个就是该从实例对应的主实例;

    还可以使用 redis-cli -p 7001 cluster nodes 命令查看

    [root@node01 ~]# redis-cli -p 7001 cluster nodes
    ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001@17001 myself,master - 0 1592900593000 1 connected 0-5460
    cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004@17004 slave ef2753888bb9b96972b5626a8cc9e01e48a23165 0 1592900593035 6 connected
    ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002@17002 slave fde786908139df0b962fa027511a1a4329582d75 0 1592900594000 5 connected
    fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005@17005 master - 0 1592900593536 3 connected 10923-16383
    3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003@17003 master - 0 1592900594539 2 connected 5461-10922
    55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006@17006 slave 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 0 1592900594939 4 connected
    

    也可以进入到redis-cli交互模式,查看集群

    [root@node02 ~]# redis-cli -c -p 7003
    127.0.0.1:7003> cluster nodes
    3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003@17003 myself,master - 0 1592901489000 2 connected 5461-10922
    55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006@17006 slave 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 0 1592901489339 4 connected
    fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005@17005 master - 0 1592901488000 3 connected 10923-16383
    ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002@17002 slave fde786908139df0b962fa027511a1a4329582d75 0 1592901489539 5 connected
    cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004@17004 slave ef2753888bb9b96972b5626a8cc9e01e48a23165 0 1592901489000 1 connected
    ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001@17001 master - 0 1592901488336 1 connected 0-5460
    

    测试集群读写

    在node01上连接7001端口,然后写入一个数据

    [root@node01 ~]# redis-cli -c -p 7001 -h 10.0.20.21
    10.0.20.21:7001> set aaa 111
    -> Redirected to slot [10439] located at 10.0.20.22:7003
    OK
    10.0.20.22:7003>
    

    然后在 node02 上连接7003 端口来读取数据

    [root@node02 ~]# redis-cli -c -p 7003
    127.0.0.1:7003> get aaa
    "111"
    127.0.0.1:7003>
    

    由此可以看出,redis-cluster 集群可以自动哈希后,把数据存入到一个计算后的 一个 主从 的 slot中。

    因此 程序在使用配置的时候,可以直接配置集群中任意实例的端口,redis-cluster 集群会自动把数据取模写入集群;但这样会对redis-cluster的性能有一定的损耗;

    如果程序支持hash取模的话,可以把所有节点的所有实例,都配置到配置文件中,在存取数据程序自身知道数据的位置,这样可以提高redis-cluster的性能;

    slot 数量含义

    redis集群中,通过``

    添加实例

    就不新家服务器,在node01上增加 7007,7008 两个端口的实例,然后加入集群;

    添加新节点
    mkdir /etc/redis/{7007,7008} -pv
    mkdir /data/redis/{7007,7008} -pv
    
    sed  "s@6379@7007@g" /etc/redis/redis.conf > /etc/redis/7007/redis.conf
    sed  "s@6379@7008@g" /etc/redis/redis.conf > /etc/redis/7008/redis.conf
    
    [root@node01 ~]# ps aux|grep redis
    root      9072  0.0  0.5 148892  5896 ?        Ssl  13:23   0:07 /usr/local/bin/redis-server 0.0.0.0:7001 [cluster]
    root      9282  0.0  0.4 145696  4996 ?        Ssl  15:22   0:03 /usr/local/bin/redis-server 0.0.0.0:7002 [cluster]
    root      9385  0.0  0.4 144536  4844 ?        Ssl  16:37   0:00 /usr/local/bin/redis-server 0.0.0.0:7007 [cluster]
    root      9395  0.0  0.4 144536  4860 ?        Ssl  16:37   0:00 /usr/local/bin/redis-server 0.0.0.0:7008 [cluster]
    

    添加 主 7007 加入集群

    模拟 7007 为主,7008为从

    新的实例加入集群,需要在加入节点是,指定集群的 其中一个 主 即可加入

    如 通过 node01 节点中的 7001 主,加入新的 实例 7007。当然这里也可以通过别的节点的主实例都可以

    [root@node01 ~]# redis-cli --cluster add-node 10.0.20.21:7007 10.0.20.21:7001
    >>> Adding node 10.0.20.21:7007 to cluster 10.0.20.21:7001
    >>> Performing Cluster Check (using node 10.0.20.21:7001)
    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       slots: (0 slots) slave
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       slots: (0 slots) slave
       replicates fde786908139df0b962fa027511a1a4329582d75
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       slots: (0 slots) slave
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    >>> Send CLUSTER MEET to node 10.0.20.21:7007 to make it join the cluster.
    [OK] New node added correctly.
    

    这样 就添加成功了,检查

    [root@node01 ~]# redis-cli cluster check 10.0.20.21:7001
    Could not connect to Redis at 127.0.0.1:6379: Connection refused
    [root@node01 ~]# redis-cli --cluster check 10.0.20.21:7001
    10.0.20.21:7001 (ef275388...) -> 0 keys | 5461 slots | 1 slaves.
    10.0.20.21:7007 (ad19acb2...) -> 0 keys | 0 slots | 0 slaves.
    10.0.20.23:7005 (fde78690...) -> 0 keys | 5461 slots | 1 slaves.
    10.0.20.22:7003 (3d37f16b...) -> 1 keys | 5462 slots | 1 slaves.
    [OK] 1 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 10.0.20.21:7001)
    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       slots: (0 slots) slave
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       slots: (0 slots) slave
       replicates fde786908139df0b962fa027511a1a4329582d75
    M: ad19acb278eea60c06a8c3858962d4c5ea3df135 10.0.20.21:7007
       slots: (0 slots) master
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       slots: (0 slots) slave
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    从上面检查可以看出,有四个M开头的 master 实例

    添加 从 7008 加入集群

    下面添加 7008 实例加入 集群,并成为 7007 的从节点

    添加方式有两种

    1. redis-cli --cluster add-node 10.0.20.21:7008 10.0.20.21:7007 --cluster-slave(不指定主节点)
    2. redis-cli --cluster add-node 10.0.20.21:7008 10.0.20.21:7007 --cluster-slave --cluster-master-id ad19acb278eea60c06a8c3858962d4c5ea3df135 (这是指定 7007 为主实例)

    那么使用第二种:

    [root@node01 ~]# redis-cli --cluster add-node  10.0.20.21:7008 10.0.20.21:7007 --cluster-slave --cluster-master-id ad19acb278eea60c06a8c3858962d4c5ea3df135
    >>> Adding node 10.0.20.21:7008 to cluster 10.0.20.21:7007
    >>> Performing Cluster Check (using node 10.0.20.21:7007)
    M: ad19acb278eea60c06a8c3858962d4c5ea3df135 10.0.20.21:7007
       slots: (0 slots) master
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       slots: (0 slots) slave
       replicates fde786908139df0b962fa027511a1a4329582d75
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       slots: (0 slots) slave
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       slots: (0 slots) slave
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[0-5460] (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.
    >>> Send CLUSTER MEET to node 10.0.20.21:7008 to make it join the cluster.
    Waiting for the cluster to join
    
    >>> Configure node as replica of 10.0.20.21:7007.
    [OK] New node added correctly.
    

    上面显示添加成功,检查看一下

    [root@node01 ~]# redis-cli --cluster check 10.0.20.21:7001
    10.0.20.21:7001 (ef275388...) -> 0 keys | 5461 slots | 1 slaves.
    10.0.20.21:7007 (ad19acb2...) -> 0 keys | 0 slots | 1 slaves.
    10.0.20.23:7005 (fde78690...) -> 0 keys | 5461 slots | 1 slaves.
    10.0.20.22:7003 (3d37f16b...) -> 1 keys | 5462 slots | 1 slaves.
    [OK] 1 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 10.0.20.21:7001)
    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    S: b5d84995cca80e1e0c518f76d910adeaf21becf8 10.0.20.21:7008
       slots: (0 slots) slave
       replicates ad19acb278eea60c06a8c3858962d4c5ea3df135
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       slots: (0 slots) slave
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       slots: (0 slots) slave
       replicates fde786908139df0b962fa027511a1a4329582d75
    M: ad19acb278eea60c06a8c3858962d4c5ea3df135 10.0.20.21:7007
       slots: (0 slots) master
       1 additional replica(s)
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       slots: (0 slots) slave
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    由上面的输出显示,两个新的实例 7007 和 7008 均成功加入到集群中;

    分配slot

    仔细观察发现,新添加的 7007 主实例的 slots0,那么这个时候是不能提供服务的,因为没有slots,无法存数据,这是需要重新分配的,也就是要重新分片

    [root@node01 ~]# redis-cli --cluster check 10.0.20.21:7001
    10.0.20.21:7001 (ef275388...) -> 0 keys | 5461 slots | 1 slaves.
    10.0.20.21:7007 (ad19acb2...) -> 0 keys | 0 slots | 1 slaves.
    10.0.20.23:7005 (fde78690...) -> 0 keys | 5461 slots | 1 slaves.
    10.0.20.22:7003 (3d37f16b...) -> 1 keys | 5462 slots | 1 slaves.
    [OK] 1 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 10.0.20.21:7001)
    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    S: b5d84995cca80e1e0c518f76d910adeaf21becf8 10.0.20.21:7008
       slots: (0 slots) slave
       replicates ad19acb278eea60c06a8c3858962d4c5ea3df135
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       slots: (0 slots) slave
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       slots: (0 slots) slave
       replicates fde786908139df0b962fa027511a1a4329582d75
    M: ad19acb278eea60c06a8c3858962d4c5ea3df135 10.0.20.21:7007
       slots: (0 slots) master
       1 additional replica(s)
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       slots: (0 slots) slave
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    那么重新分片的命令中,指定任意一个 主 实例即可

    [root@node01 ~]# redis-cli --cluster reshard 10.0.20.21:7001
    >>> Performing Cluster Check (using node 10.0.20.21:7001)
    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[0-5460] (5461 slots) master
       1 additional replica(s)
    S: b5d84995cca80e1e0c518f76d910adeaf21becf8 10.0.20.21:7008
       slots: (0 slots) slave
       replicates ad19acb278eea60c06a8c3858962d4c5ea3df135
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       slots: (0 slots) slave
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       slots: (0 slots) slave
       replicates fde786908139df0b962fa027511a1a4329582d75
    M: ad19acb278eea60c06a8c3858962d4c5ea3df135 10.0.20.21:7007
       slots: (0 slots) master
       1 additional replica(s)
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[10923-16383] (5461 slots) master
       1 additional replica(s)
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[5461-10922] (5462 slots) master
       1 additional replica(s)
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       slots: (0 slots) slave
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    How many slots do you want to move (from 1 to 16384)? 2000                  <----  这里的意思是询问 想要移动多少个 slots ,这里需要根据实际情况填写,我就填写2000 
    What is the receiving node ID? ad19acb278eea60c06a8c3858962d4c5ea3df135     <----  这里需要填写2000 个 slots给那个主实例,一定是主实例,这里填写 7007 对应的
    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                                                         <----  这里有两个选项 all 表示 会把其他所有正常的主节点的slots重新hash分配出来2000个slots,或者是从其中一个主节点上分配出来2000个slots,这里选择 all,需要根据实际情况
        ........... ............
        Moving slot 11586 from fde786908139df0b962fa027511a1a4329582d75
        Moving slot 11587 from fde786908139df0b962fa027511a1a4329582d75
        Moving slot 11588 from fde786908139df0b962fa027511a1a4329582d75
    Do you want to proceed with the proposed reshard plan (yes/no)? yes         <----  这里问是否继续,输入yes
    Moving slot 11582 from 10.0.20.23:7005 to 10.0.20.21:7007: 
    Moving slot 11583 from 10.0.20.23:7005 to 10.0.20.21:7007: 
    Moving slot 11584 from 10.0.20.23:7005 to 10.0.20.21:7007: 
    Moving slot 11585 from 10.0.20.23:7005 to 10.0.20.21:7007: 
    Moving slot 11586 from 10.0.20.23:7005 to 10.0.20.21:7007: 
    Moving slot 11587 from 10.0.20.23:7005 to 10.0.20.21:7007: 
    Moving slot 11588 from 10.0.20.23:7005 to 10.0.20.21:7007:
    ........... ..............
    

    当完成上面的操作后,再次查看

    [root@node01 ~]# redis-cli --cluster check 10.0.20.21:7001
    10.0.20.21:7001 (ef275388...) -> 0 keys | 4795 slots | 1 slaves.
    10.0.20.21:7007 (ad19acb2...) -> 0 keys | 1999 slots | 1 slaves.
    10.0.20.23:7005 (fde78690...) -> 0 keys | 4795 slots | 1 slaves.
    10.0.20.22:7003 (3d37f16b...) -> 1 keys | 4795 slots | 1 slaves.
    [OK] 1 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 10.0.20.21:7001)
    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[666-5460] (4795 slots) master
       1 additional replica(s)
    S: b5d84995cca80e1e0c518f76d910adeaf21becf8 10.0.20.21:7008
       slots: (0 slots) slave
       replicates ad19acb278eea60c06a8c3858962d4c5ea3df135
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       slots: (0 slots) slave
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       slots: (0 slots) slave
       replicates fde786908139df0b962fa027511a1a4329582d75
    M: ad19acb278eea60c06a8c3858962d4c5ea3df135 10.0.20.21:7007
       slots:[0-665],[5461-6127],[10923-11588] (1999 slots) master
       1 additional replica(s)
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[11589-16383] (4795 slots) master
       1 additional replica(s)
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[6128-10922] (4795 slots) master
       1 additional replica(s)
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       slots: (0 slots) slave
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    实例7007 已经被分配到 1999 个,这个slots不会特别准确,因为需要根据 一系列的算法 hash啥的,来给的;此时,7007 就可以提供服务啦;

    删除节点

    删除节点分流程为两种:

    1. 删除从节点
    2. 删除主节点,需要先把主节点的slots 释放,重新分配,然后删除从节点,再删除主节点即可

    那么都来演示一下:删除上面添加的实例

    删除从节点

    从节点的删除,直接即可,无需别的操作;

    删除的时候,需要指定集群中的任意主实例连接集群

    [root@node01 ~]# redis-cli --cluster del-node 10.0.20.21:7001 b5d84995cca80e1e0c518f76d910adeaf21becf8
    >>> Removing node b5d84995cca80e1e0c518f76d910adeaf21becf8 from cluster 10.0.20.21:7001
    >>> Sending CLUSTER FORGET messages to the cluster...
    >>> SHUTDOWN the node.
    

    再次查看集群:

    [root@node01 ~]# redis-cli --cluster check 10.0.20.21:7001
    10.0.20.21:7001 (ef275388...) -> 0 keys | 4795 slots | 1 slaves.
    10.0.20.21:7007 (ad19acb2...) -> 0 keys | 1999 slots | 0 slaves.
    10.0.20.23:7005 (fde78690...) -> 0 keys | 4795 slots | 1 slaves.
    10.0.20.22:7003 (3d37f16b...) -> 1 keys | 4795 slots | 1 slaves.
    [OK] 1 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 10.0.20.21:7001)
    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[666-5460] (4795 slots) master
       1 additional replica(s)
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       slots: (0 slots) slave
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       slots: (0 slots) slave
       replicates fde786908139df0b962fa027511a1a4329582d75
    M: ad19acb278eea60c06a8c3858962d4c5ea3df135 10.0.20.21:7007
       slots:[0-665],[5461-6127],[10923-11588] (1999 slots) master
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[11589-16383] (4795 slots) master
       1 additional replica(s)
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[6128-10922] (4795 slots) master
       1 additional replica(s)
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       slots: (0 slots) slave
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    此时已经 7008 从实例已经没有了,被删除了;

    删除主实例

    删除主实例之前,需要先把 slots 从改实例上移除,重新分片命令,需要指定任意主实例即可

    移除slots的步骤跟 之前添加类似;

    [root@node01 ~]# redis-cli --cluster reshard 10.0.20.21:7001
    >>> Performing Cluster Check (using node 10.0.20.21:7001)
    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[666-5460] (4795 slots) master
       1 additional replica(s)
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       slots: (0 slots) slave
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       slots: (0 slots) slave
       replicates fde786908139df0b962fa027511a1a4329582d75
    M: ad19acb278eea60c06a8c3858962d4c5ea3df135 10.0.20.21:7007
       slots:[0-665],[5461-6127],[10923-11588] (1999 slots) master
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[11589-16383] (4795 slots) master
       1 additional replica(s)
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[6128-10922] (4795 slots) master
       1 additional replica(s)
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       slots: (0 slots) slave
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    How many slots do you want to move (from 1 to 16384)? 1999                          <--- 这里指定需要重新分片slots的数量
    What is the receiving node ID? fde786908139df0b962fa027511a1a4329582d75             <--- 这里指定要把那个主实例的slots的重新分片
    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: ad19acb278eea60c06a8c3858962d4c5ea3df135                            <--- 这里指定拿出来的slots 分配给那个主实例,这里输入的是 7005 主实例,注意,一定是主实例
    Source node #2: done                                                                <--- 这里输入 done,表示结束
        ... ... ... ... ... ...
        Moving slot 11583 from ad19acb278eea60c06a8c3858962d4c5ea3df135
        Moving slot 11584 from ad19acb278eea60c06a8c3858962d4c5ea3df135
        Moving slot 11585 from ad19acb278eea60c06a8c3858962d4c5ea3df135
        Moving slot 11586 from ad19acb278eea60c06a8c3858962d4c5ea3df135
        Moving slot 11587 from ad19acb278eea60c06a8c3858962d4c5ea3df135
        Moving slot 11588 from ad19acb278eea60c06a8c3858962d4c5ea3df135
    Do you want to proceed with the proposed reshard plan (yes/no)? yes                 <--- 这里输入yes确认
    ... ... ... ...
    Moving slot 13585 from 10.0.20.23:7005 to 10.0.20.21:7007: 
    Moving slot 13586 from 10.0.20.23:7005 to 10.0.20.21:7007: 
    Moving slot 13587 from 10.0.20.23:7005 to 10.0.20.21:7007: 
    

    当完成后再次查看:

    [root@node01 ~]# redis-cli --cluster check 10.0.20.21:7001
    10.0.20.21:7001 (ef275388...) -> 0 keys | 4795 slots | 1 slaves.
    10.0.20.21:7007 (ad19acb2...) -> 0 keys | 0 slots | 0 slaves.
    10.0.20.23:7005 (fde78690...) -> 0 keys | 6794 slots | 1 slaves.
    10.0.20.22:7003 (3d37f16b...) -> 1 keys | 4795 slots | 1 slaves.
    [OK] 1 keys in 4 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 10.0.20.21:7001)
    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[666-5460] (4795 slots) master
       1 additional replica(s)
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       slots: (0 slots) slave
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       slots: (0 slots) slave
       replicates fde786908139df0b962fa027511a1a4329582d75
    M: ad19acb278eea60c06a8c3858962d4c5ea3df135 10.0.20.21:7007
       slots: (0 slots) master
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[0-665],[5461-6127],[10923-16383] (6794 slots) master
       1 additional replica(s)
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[6128-10922] (4795 slots) master
       1 additional replica(s)
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       slots: (0 slots) slave
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    上面检查可以看到,7007 主实例的 slots 已经为 0,那么此时,就可以删除此主实例了

    [root@node01 ~]# redis-cli --cluster del-node 10.0.20.21:7001 ad19acb278eea60c06a8c3858962d4c5ea3df135
    >>> Removing node ad19acb278eea60c06a8c3858962d4c5ea3df135 from cluster 10.0.20.21:7001
    >>> Sending CLUSTER FORGET messages to the cluster...
    >>> SHUTDOWN the node.
    

    再次查看集群状态

    [root@node01 ~]# redis-cli --cluster check 10.0.20.21:7001
    10.0.20.21:7001 (ef275388...) -> 0 keys | 4795 slots | 1 slaves.
    10.0.20.23:7005 (fde78690...) -> 0 keys | 6794 slots | 1 slaves.
    10.0.20.22:7003 (3d37f16b...) -> 1 keys | 4795 slots | 1 slaves.
    [OK] 1 keys in 3 masters.
    0.00 keys per slot on average.
    >>> Performing Cluster Check (using node 10.0.20.21:7001)
    M: ef2753888bb9b96972b5626a8cc9e01e48a23165 10.0.20.21:7001
       slots:[666-5460] (4795 slots) master
       1 additional replica(s)
    S: cc97f886d15205641d933fc14a373c5bb8adb07c 10.0.20.22:7004
       slots: (0 slots) slave
       replicates ef2753888bb9b96972b5626a8cc9e01e48a23165
    S: ee349f3224e3ce1221f0b2fc2b963ab5313c6248 10.0.20.21:7002
       slots: (0 slots) slave
       replicates fde786908139df0b962fa027511a1a4329582d75
    M: fde786908139df0b962fa027511a1a4329582d75 10.0.20.23:7005
       slots:[0-665],[5461-6127],[10923-16383] (6794 slots) master
       1 additional replica(s)
    M: 3d37f16b11eaaad2abdd0514dc29354d42ae35c3 10.0.20.22:7003
       slots:[6128-10922] (4795 slots) master
       1 additional replica(s)
    S: 55f824bece26391289dd00b8d8791e775c46252c 10.0.20.23:7006
       slots: (0 slots) slave
       replicates 3d37f16b11eaaad2abdd0514dc29354d42ae35c3
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
    

    7007 7008 实例均已被移除集群;

  • 相关阅读:
    谷歌在线测试题
    C++ 重载(overload)、重写(overrride)、重定义(redefine)总结
    C/S架构和B/S架构的概念和区别
    hadoop 及hbase zookeeper 经常出现问题
    Hadoop上的中文分词与词频统计实践 (有待学习 http://www.cnblogs.com/jiejue/archive/2012/12/16/2820788.html)
    HBase数据的导入和导出
    浅谈hbase表中数据导出导入(也就是备份)
    学习大数据看门的几本书
    hadoop pig入门总结
    Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别
  • 原文地址:https://www.cnblogs.com/winstom/p/13185475.html
Copyright © 2020-2023  润新知