016、Redis的分布式集群(Cluster)架构管理之扩容实战案例
本文最后更新于 66 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com

Redis的分布式集群(Cluster)架构管理之扩容实战案例

一.准备两组redis实例

1.现有集群查看

[root@redis201.oldboyedu.com ~]# redis-cli -p 10001 CLUSTER NODES
88e97940a7371296fb3d51589440dc43ea256599 172.200.1.201:10000 slave 022fdb712d56996201a0e892bf9547d0ede1dd3b 0 1614911124899 10 connected
d973719a920e2b1d8a14d43213bab3bad43cbc8a 172.200.1.201:10005 master - 0 1614911125403 8 connected 10923-16383
c1146f293fca9207a60358b849be8ae6af7c6e2f 172.200.1.201:10002 slave d973719a920e2b1d8a14d43213bab3bad43cbc8a 0 1614911124396 8 connected
022fdb712d56996201a0e892bf9547d0ede1dd3b 172.200.1.201:10003 master - 0 1614911124899 10 connected 0-5460
c907ac66c16daa720735e89addc5a854c3c40a0f 172.200.1.201:10001 myself,master - 0 0 11 connected 5461-10922
a0f75a28e4b9b60b2c1910a20c15fc92e02c2d67 172.200.1.201:10004 slave c907ac66c16daa720735e89addc5a854c3c40a0f 0 1614911125907 11 connected
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# redis-cli -p 10003 CLUSTER NODES | grep master
022fdb712d56996201a0e892bf9547d0ede1dd3b 172.200.1.201:10003 myself,master - 0 0 10 connected 0-5460
c907ac66c16daa720735e89addc5a854c3c40a0f 172.200.1.201:10001 master - 0 1614911127915 11 connected 5461-10922
d973719a920e2b1d8a14d43213bab3bad43cbc8a 172.200.1.201:10005 master - 0 1614911128924 8 connected 10923-16383
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# redis-cli -p 10005 CLUSTER NODES | grep slave
88e97940a7371296fb3d51589440dc43ea256599 172.200.1.201:10000 slave 022fdb712d56996201a0e892bf9547d0ede1dd3b 0 1614911134025 10 connected
c1146f293fca9207a60358b849be8ae6af7c6e2f 172.200.1.201:10002 slave d973719a920e2b1d8a14d43213bab3bad43cbc8a 0 1614911133521 8 connected
a0f75a28e4b9b60b2c1910a20c15fc92e02c2d67 172.200.1.201:10004 slave c907ac66c16daa720735e89addc5a854c3c40a0f 0 1614911132516 11 connected
[root@redis201.oldboyedu.com ~]# 

以上每项的参数说明如下:
    id: 
        节点ID,是一个40字节的随机字符串,这个值在节点启动的时候创建,并且永远不会改变(除非使用CLUSTER RESET HARD命令)。
    ip:port: 
        客户端与节点通信使用的地址.
    flags: 
        逗号分割的标记位,可能的值有: myself, master, slave, fail?, fail, handshake, noaddr, noflags. 下一部分将详细介绍这些标记.
    master: 
        如果节点是slave,并且已知master节点,则这里列出master节点ID,否则的话这里列出”-“。
    ping-sent: 
        最近一次发送ping的时间,这个时间是一个unix毫秒时间戳,0代表没有发送过.
    pong-recv: 
        最近一次收到pong的时间,使用unix时间戳表示.
    config-epoch: 
        节点的epoch值(or of the current master if the node is a slave)。每当节点发生失败切换时,都会创建一个新的,独特的,递增的epoch。如果多个节点竞争同一个哈希槽时,epoch值更高的节点会抢夺到。
    link-state: 
        node-to-node集群总线使用的链接的状态,我们使用这个链接与集群中其他节点进行通信.值可以是 connected 和 disconnected.
    slot: 
        哈希槽值或者一个哈希槽范围. 从第9个参数开始,后面最多可能有16384个 数(limit never reached)。代表当前节点可以提供服务的所有哈希槽值。
        如果只是一个值,那就是只有一个槽会被使用。如果是一个范围,这个值表示为起始槽-结束槽,节点将处理包括起始槽和结束槽在内的所有哈希槽。

    参考链接:
        http://www.redis.cn/commands/cluster-nodes.html

温馨提示:
    (1)如果有"myself"字样,说明你正在连接的是哪个节点;
    (2)注意观察槽位编号信息,比如"172.200.1.201:10000 master"被分配到的槽位号是"0-5460",而"172.200.1.201:10001"被分配到的槽位号是"5461-10922","172.200.1.201:10002 master"被分配到的槽位号是"10923-16383";
    (3)每个redis实例都有其独有的id编号,比如"172.200.1.201:10000 master"的编号为"88e97940a7371296fb3d51589440dc43ea256599",这有点类似于MySQL的"server_uuid";

2.创建多Redis实例使用的配置文件,数据,日志目录

[root@redis201.oldboyedu.com ~]# mkdir -pv /oldboyedu/softwares/redis1000{6..7}
mkdir: 已创建目录 "/oldboyedu/softwares/redis10006"
mkdir: 已创建目录 "/oldboyedu/softwares/redis10007"
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# mkdir -pv /oldboyedu/data/redis1000{6..7}
mkdir: 已创建目录 "/oldboyedu/data/redis10006"
mkdir: 已创建目录 "/oldboyedu/data/redis10007"
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# mkdir -pv /oldboyedu/logs/redis1000{6..7}
mkdir: 已创建目录 "/oldboyedu/logs/redis10006"
mkdir: 已创建目录 "/oldboyedu/logs/redis10007"
[root@redis201.oldboyedu.com ~]# 

3.为各个Redis实例创建配置文件

    (1)创建"redis10006"实例的配置文件
        [root@redis201.oldboyedu.com ~]# cat > /oldboyedu/softwares/redis10006/redis.conf <<EOF
        > port 10006
        > daemonize yes
        > pidfile /oldboyedu/data/redis10006/redis.pid
        > loglevel notice
        > logfile /oldboyedu/logs/redis10006/redis.log
        > dbfilename dump.rdb
        > dir /oldboyedu/data/redis10006
        > bind 172.200.1.201 127.0.0.1
        > # 配置RDB持久化策略
        > save 900 1
        > save 300 10
        > save 60 10000
        > # 配置AOF持久化
        > appendonly yes
        > appendfsync everysec
        > # 配置Redis Cluster模式
        > protected-mode no
        > cluster-enabled yes
        > cluster-config-file nodes.conf
        > cluster-node-timeout 5000
        > EOF
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis10006/redis.conf 
        port 10006
        daemonize yes
        pidfile /oldboyedu/data/redis10006/redis.pid
        loglevel notice
        logfile /oldboyedu/logs/redis10006/redis.log
        dbfilename dump.rdb
        dir /oldboyedu/data/redis10006
        bind 172.200.1.201 127.0.0.1
        # 配置RDB持久化策略
        save 900 1
        save 300 10
        save 60 10000
        # 配置AOF持久化
        appendonly yes
        appendfsync everysec
        # 配置Redis Cluster模式
        protected-mode no
        cluster-enabled yes
        cluster-config-file nodes.conf
        cluster-node-timeout 5000
        [root@redis201.oldboyedu.com ~]# 

    (2)创建"redis10007"实例的配置文件
        [root@redis201.oldboyedu.com ~]# cat > /oldboyedu/softwares/redis10007/redis.conf <<EOF
        > port 10007
        > daemonize yes
        > pidfile /oldboyedu/data/redis10007/redis.pid
        > loglevel notice
        > logfile /oldboyedu/logs/redis10007/redis.log
        > dbfilename dump.rdb
        > dir /oldboyedu/data/redis10007
        > bind 172.200.1.201 127.0.0.1
        > # 配置RDB持久化策略
        > save 900 1
        > save 300 10
        > save 60 10000
        > # 配置AOF持久化
        > appendonly yes
        > appendfsync everysec
        > # 配置Redis Cluster模式
        > protected-mode no
        > cluster-enabled yes
        > cluster-config-file nodes.conf
        > cluster-node-timeout 5000
        > EOF
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# cat /oldboyedu/softwares/redis10007/redis.conf 
        port 10007
        daemonize yes
        pidfile /oldboyedu/data/redis10007/redis.pid
        loglevel notice
        logfile /oldboyedu/logs/redis10007/redis.log
        dbfilename dump.rdb
        dir /oldboyedu/data/redis10007
        bind 172.200.1.201 127.0.0.1
        # 配置RDB持久化策略
        save 900 1
        save 300 10
        save 60 10000
        # 配置AOF持久化
        appendonly yes
        appendfsync everysec
        # 配置Redis Cluster模式
        protected-mode no
        cluster-enabled yes
        cluster-config-file nodes.conf
        cluster-node-timeout 5000
        [root@redis201.oldboyedu.com ~]# 

4.启动新增的两个redis实例

    (1)启动新增的redis之前的监听端口:
        [root@redis201.oldboyedu.com ~]# ss -ntl
        State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
        LISTEN      0      128                                                        127.0.0.1:10000                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10000                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:10001                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10001                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:10002                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10002                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:10003                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10003                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:10004                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10004                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:10005                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10005                                                                          *:*                  
        LISTEN      0      128                                                                *:22                                                                             *:*                  
        LISTEN      0      128                                                        127.0.0.1:20000                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20000                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:20001                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20001                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:20002                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20002                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:20003                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20003                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:20004                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20004                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:20005                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20005                                                                          *:*                  
        LISTEN      0      128                                                             [::]:22                                                                          [::]:*                  

        [root@redis201.oldboyedu.com ~]# 

    (2)启动redis
        [root@redis201.oldboyedu.com ~]# ps -ef | grep redis
        root      50114      1  0 3月04 ?       00:01:23 redis-server 172.200.1.201:10000 [cluster]
        root      50196      1  0 3月04 ?       00:01:21 redis-server 172.200.1.201:10001 [cluster]
        root      50278      1  0 3月04 ?       00:01:20 redis-server 172.200.1.201:10002 [cluster]
        root      50360      1  0 3月04 ?       00:01:18 redis-server 172.200.1.201:10003 [cluster]
        root      50442      1  0 3月04 ?       00:01:20 redis-server 172.200.1.201:10004 [cluster]
        root      50563      1  0 3月04 ?       00:01:20 redis-server 172.200.1.201:10005 [cluster]
        root      54958  52769  0 10:33 pts/0    00:00:00 grep --color=auto redis
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-server /oldboyedu/softwares/redis10006/redis.conf 
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-server /oldboyedu/softwares/redis10007/redis.conf 
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# ps -ef | grep redis
        root      50114      1  0 3月04 ?       00:01:23 redis-server 172.200.1.201:10000 [cluster]
        root      50196      1  0 3月04 ?       00:01:21 redis-server 172.200.1.201:10001 [cluster]
        root      50278      1  0 3月04 ?       00:01:20 redis-server 172.200.1.201:10002 [cluster]
        root      50360      1  0 3月04 ?       00:01:18 redis-server 172.200.1.201:10003 [cluster]
        root      50442      1  0 3月04 ?       00:01:20 redis-server 172.200.1.201:10004 [cluster]
        root      50563      1  0 3月04 ?       00:01:20 redis-server 172.200.1.201:10005 [cluster]
        root      55039      1  0 10:33 ?        00:00:00 redis-server 172.200.1.201:10006 [cluster]
        root      55121      1  0 10:34 ?        00:00:00 redis-server 172.200.1.201:10007 [cluster]
        root      55203  52769  0 10:34 pts/0    00:00:00 grep --color=auto redis
        [root@redis201.oldboyedu.com ~]# 

    (3)启动redis之后的监听端口:
        [root@redis201.oldboyedu.com ~]# ss -ntl
        State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
        LISTEN      0      128                                                        127.0.0.1:10000                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10000                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:10001                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10001                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:10002                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10002                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:10003                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10003                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:10004                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10004                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:10005                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10005                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:10006                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10006                                                                          *:*                  
        LISTEN      0      128                                                                *:22                                                                             *:*                  
        LISTEN      0      128                                                        127.0.0.1:10007                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:10007                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:20000                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20000                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:20001                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20001                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:20002                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20002                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:20003                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20003                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:20004                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20004                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:20005                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20005                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:20006                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20006                                                                          *:*                  
        LISTEN      0      128                                                        127.0.0.1:20007                                                                          *:*                  
        LISTEN      0      128                                                    172.200.1.201:20007                                                                          *:*                  
        LISTEN      0      128                                                             [::]:22                                                                          [::]:*                  
        [root@redis201.oldboyedu.com ~]# 

二.将准备的redis实例添加到现有的redis cluster集群中已实现扩容

1.添加master节点,添加master节点时的语法格式: "redis-trib.rb add-node new_host:new_port existing_host:existing_port"

[root@redis201.oldboyedu.com ~]# redis-trib.rb add-node 172.200.1.201:10006 172.200.1.201:10001  # 将"172.200.1.201:10006"设置为master节点。
>>> Adding node 172.200.1.201:10006 to cluster 172.200.1.201:10001
>>> Performing Cluster Check (using node 172.200.1.201:10001)
M: c907ac66c16daa720735e89addc5a854c3c40a0f 172.200.1.201:10001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 88e97940a7371296fb3d51589440dc43ea256599 172.200.1.201:10000
   slots: (0 slots) slave
   replicates 022fdb712d56996201a0e892bf9547d0ede1dd3b
M: d973719a920e2b1d8a14d43213bab3bad43cbc8a 172.200.1.201:10005
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: c1146f293fca9207a60358b849be8ae6af7c6e2f 172.200.1.201:10002
   slots: (0 slots) slave
   replicates d973719a920e2b1d8a14d43213bab3bad43cbc8a
M: 022fdb712d56996201a0e892bf9547d0ede1dd3b 172.200.1.201:10003
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: a0f75a28e4b9b60b2c1910a20c15fc92e02c2d67 172.200.1.201:10004
   slots: (0 slots) slave
   replicates c907ac66c16daa720735e89addc5a854c3c40a0f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.200.1.201:10006 to make it join the cluster.
[OK] New node added correctly.
[root@redis201.oldboyedu.com ~]# 
[root@redis201.oldboyedu.com ~]# redis-cli -p 10003 CLUSTER NODES | grep master  # 注意观察新加入的节点"172.200.1.201:10006"目前是没有被分片槽位的。
022fdb712d56996201a0e892bf9547d0ede1dd3b 172.200.1.201:10003 myself,master - 0 0 10 connected 0-5460
c907ac66c16daa720735e89addc5a854c3c40a0f 172.200.1.201:10001 master - 0 1614912247269 11 connected 5461-10922
6943a42e5c72a9a0d3c4d9ec1954132ab914465d 172.200.1.201:10006 master - 0 1614912247771 12 connected
d973719a920e2b1d8a14d43213bab3bad43cbc8a 172.200.1.201:10005 master - 0 1614912245756 8 connected 10923-16383
[root@redis201.oldboyedu.com ~]# 

2.转移槽位(slot)进行重新分片

    (1)我们最开始集群数量是3个master和3个slave,现在打算新增一个master节点,因此要对16384槽位划分为4个master平均分配,计算结果大概每个节点需要4096个slot。
        [root@redis201.oldboyedu.com ~]# echo 16384 / 4 | bc
        4096
        [root@redis201.oldboyedu.com ~]# 

    (2)未重新分片slot之前:
        [root@redis201.oldboyedu.com ~]# redis-cli -p 10003 CLUSTER NODES | grep master  # 注意观察新加入的节点"172.200.1.201:10006"目前是没有被分片槽位的。
        022fdb712d56996201a0e892bf9547d0ede1dd3b 172.200.1.201:10003 myself,master - 0 0 10 connected 0-5460
        c907ac66c16daa720735e89addc5a854c3c40a0f 172.200.1.201:10001 master - 0 1614912247269 11 connected 5461-10922
        6943a42e5c72a9a0d3c4d9ec1954132ab914465d 172.200.1.201:10006 master - 0 1614912247771 12 connected
        d973719a920e2b1d8a14d43213bab3bad43cbc8a 172.200.1.201:10005 master - 0 1614912245756 8 connected 10923-16383
        [root@redis201.oldboyedu.com ~]# 

    (3)将现有集群的slot转移到新加入的节点中:(如果有交互式界面,依次手动输入"4096","6943a42e5c72a9a0d3c4d9ec1954132ab914465d","all"和"yes")
        [root@redis201.oldboyedu.com ~]# redis-trib.rb reshard 172.200.1.201:10001
        >>> Performing Cluster Check (using node 172.200.1.201:10001)
        M: c907ac66c16daa720735e89addc5a854c3c40a0f 172.200.1.201:10001
           slots:5461-10922 (5462 slots) master
           1 additional replica(s)
        M: 6943a42e5c72a9a0d3c4d9ec1954132ab914465d 172.200.1.201:10006
           slots: (0 slots) master
           0 additional replica(s)
        S: 88e97940a7371296fb3d51589440dc43ea256599 172.200.1.201:10000
           slots: (0 slots) slave
           replicates 022fdb712d56996201a0e892bf9547d0ede1dd3b
        M: d973719a920e2b1d8a14d43213bab3bad43cbc8a 172.200.1.201:10005
           slots:10923-16383 (5461 slots) master
           1 additional replica(s)
        S: c1146f293fca9207a60358b849be8ae6af7c6e2f 172.200.1.201:10002
           slots: (0 slots) slave
           replicates d973719a920e2b1d8a14d43213bab3bad43cbc8a
        M: 022fdb712d56996201a0e892bf9547d0ede1dd3b 172.200.1.201:10003
           slots:0-5460 (5461 slots) master
           1 additional replica(s)
        S: a0f75a28e4b9b60b2c1910a20c15fc92e02c2d67 172.200.1.201:10004
           slots: (0 slots) slave
           replicates c907ac66c16daa720735e89addc5a854c3c40a0f
        [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)? 4096
        What is the receiving node ID? 6943a42e5c72a9a0d3c4d9ec1954132ab914465d
        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

            ...

            Moving slot 1358 from 022fdb712d56996201a0e892bf9547d0ede1dd3b
            Moving slot 1359 from 022fdb712d56996201a0e892bf9547d0ede1dd3b
            Moving slot 1360 from 022fdb712d56996201a0e892bf9547d0ede1dd3b
            Moving slot 1361 from 022fdb712d56996201a0e892bf9547d0ede1dd3b
            Moving slot 1362 from 022fdb712d56996201a0e892bf9547d0ede1dd3b
            Moving slot 1363 from 022fdb712d56996201a0e892bf9547d0ede1dd3b
            Moving slot 1364 from 022fdb712d56996201a0e892bf9547d0ede1dd3b
        Do you want to proceed with the proposed reshard plan (yes/no)? yes

            ...

        Moving slot 1348 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1349 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1350 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1351 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1352 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1353 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1354 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1355 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1356 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1357 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1358 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1359 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1360 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1361 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1362 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1363 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        Moving slot 1364 from 172.200.1.201:10003 to 172.200.1.201:10006: 
        [root@redis201.oldboyedu.com ~]# 

    (4)重新分片slot之后:
        [root@redis201.oldboyedu.com ~]# redis-cli -p 10005 CLUSTER NODES | grep master
        022fdb712d56996201a0e892bf9547d0ede1dd3b 172.200.1.201:10003 master - 0 1614915641296 10 connected 1365-5460
        c907ac66c16daa720735e89addc5a854c3c40a0f 172.200.1.201:10001 master - 0 1614915641296 11 connected 6827-10922
        d973719a920e2b1d8a14d43213bab3bad43cbc8a 172.200.1.201:10005 myself,master - 0 0 8 connected 12288-16383
        6943a42e5c72a9a0d3c4d9ec1954132ab914465d 172.200.1.201:10006 master - 0 1614915641799 12 connected 0-1364 5461-6826 10923-12287
        [root@redis201.oldboyedu.com ~]# 

3.添加一个slave节点,添加slave节点时的语法格式: "redis-trib.rb add-node --slave --master-id new_host:new_port existing_host:existing_port"

    (1)未添加一个slave节点之前,查看nodes.conf的配置文件:
        [root@redis201.oldboyedu.com ~]# ll /oldboyedu/data/redis10007/
        总用量 8
        -rw-r--r-- 1 root root   0 3月   5 11:56 appendonly.aof
        -rw-r--r-- 1 root root 112 3月   5 11:56 nodes.conf
        -rw-r--r-- 1 root root   6 3月   5 11:56 redis.pid
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# cat /oldboyedu/data/redis10007/nodes.conf 
        ecf97a38e749c9e530f1da42ec35923e8fd7c8a1 :0 myself,master - 0 0 0 connected
        vars currentEpoch 0 lastVoteEpoch 0
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# redis-cli -p 10003 CLUSTER NODES | grep slave
        a0f75a28e4b9b60b2c1910a20c15fc92e02c2d67 172.200.1.201:10004 slave c907ac66c16daa720735e89addc5a854c3c40a0f 0 1614912027589 11 connected
        88e97940a7371296fb3d51589440dc43ea256599 172.200.1.201:10000 slave 022fdb712d56996201a0e892bf9547d0ede1dd3b 0 1614912025569 10 connected
        c1146f293fca9207a60358b849be8ae6af7c6e2f 172.200.1.201:10002 slave d973719a920e2b1d8a14d43213bab3bad43cbc8a 0 1614912027084 8 connected
        [root@redis201.oldboyedu.com ~]# 

    (2)添加节点
        [root@redis201.oldboyedu.com ~]# redis-trib.rb add-node --slave --master-id 6943a42e5c72a9a0d3c4d9ec1954132ab914465d 172.200.1.201:10007 172.200.1.201:10001
        >>> Adding node 172.200.1.201:10007 to cluster 172.200.1.201:10001
        >>> Performing Cluster Check (using node 172.200.1.201:10001)
        M: c907ac66c16daa720735e89addc5a854c3c40a0f 172.200.1.201:10001
           slots:6827-10922 (4096 slots) master
           1 additional replica(s)
        M: 6943a42e5c72a9a0d3c4d9ec1954132ab914465d 172.200.1.201:10006
           slots:0-1364,5461-6826,10923-12287 (4096 slots) master
           0 additional replica(s)
        S: 88e97940a7371296fb3d51589440dc43ea256599 172.200.1.201:10000
           slots: (0 slots) slave
           replicates 022fdb712d56996201a0e892bf9547d0ede1dd3b
        M: d973719a920e2b1d8a14d43213bab3bad43cbc8a 172.200.1.201:10005
           slots:12288-16383 (4096 slots) master
           1 additional replica(s)
        S: c1146f293fca9207a60358b849be8ae6af7c6e2f 172.200.1.201:10002
           slots: (0 slots) slave
           replicates d973719a920e2b1d8a14d43213bab3bad43cbc8a
        M: 022fdb712d56996201a0e892bf9547d0ede1dd3b 172.200.1.201:10003
           slots:1365-5460 (4096 slots) master
           1 additional replica(s)
        S: a0f75a28e4b9b60b2c1910a20c15fc92e02c2d67 172.200.1.201:10004
           slots: (0 slots) slave
           replicates c907ac66c16daa720735e89addc5a854c3c40a0f
        [OK] All nodes agree about slots configuration.
        >>> Check for open slots...
        >>> Check slots coverage...
        [OK] All 16384 slots covered.
        >>> Send CLUSTER MEET to node 172.200.1.201:10007 to make it join the cluster.
        Waiting for the cluster to join.
        >>> Configure node as replica of 172.200.1.201:10006.
        [OK] New node added correctly.
        [root@redis201.oldboyedu.com ~]# 

    (3)添加一个slave节点之后,再次查看nodes.conf的配置文件:
        [root@redis201.oldboyedu.com ~]# redis-cli -p 10005 CLUSTER NODES | grep slave
        88e97940a7371296fb3d51589440dc43ea256599 172.200.1.201:10000 slave 022fdb712d56996201a0e892bf9547d0ede1dd3b 0 1614916813879 10 connected
        c1146f293fca9207a60358b849be8ae6af7c6e2f 172.200.1.201:10002 slave d973719a920e2b1d8a14d43213bab3bad43cbc8a 0 1614916815392 8 connected
        a0f75a28e4b9b60b2c1910a20c15fc92e02c2d67 172.200.1.201:10004 slave c907ac66c16daa720735e89addc5a854c3c40a0f 0 1614916813375 11 connected
        ecf97a38e749c9e530f1da42ec35923e8fd7c8a1 172.200.1.201:10007 slave 6943a42e5c72a9a0d3c4d9ec1954132ab914465d 0 1614916814886 12 connected
        [root@redis201.oldboyedu.com ~]# 
        [root@redis201.oldboyedu.com ~]# cat /oldboyedu/data/redis10007/nodes.conf 
        022fdb712d56996201a0e892bf9547d0ede1dd3b 172.200.1.201:10003 master - 0 1614916591511 10 connected 1365-5460
        6943a42e5c72a9a0d3c4d9ec1954132ab914465d 172.200.1.201:10006 master - 0 1614916592518 12 connected 0-1364 5461-6826 10923-12287
        c907ac66c16daa720735e89addc5a854c3c40a0f 172.200.1.201:10001 master - 0 1614916590503 11 connected 6827-10922
        88e97940a7371296fb3d51589440dc43ea256599 172.200.1.201:10000 slave 022fdb712d56996201a0e892bf9547d0ede1dd3b 0 1614916590300 10 connected
        a0f75a28e4b9b60b2c1910a20c15fc92e02c2d67 172.200.1.201:10004 slave c907ac66c16daa720735e89addc5a854c3c40a0f 0 1614916592616 11 connected
        ecf97a38e749c9e530f1da42ec35923e8fd7c8a1 172.200.1.201:10007 myself,slave 6943a42e5c72a9a0d3c4d9ec1954132ab914465d 0 0 0 connected
        d973719a920e2b1d8a14d43213bab3bad43cbc8a 172.200.1.201:10005 master - 0 1614916590200 8 connected 12288-16383
        c1146f293fca9207a60358b849be8ae6af7c6e2f 172.200.1.201:10002 slave d973719a920e2b1d8a14d43213bab3bad43cbc8a 0 1614916592616 8 connected
        vars currentEpoch 12 lastVoteEpoch 0
        [root@redis201.oldboyedu.com ~]# 

三.可能会遇到的报错

1.[ERR] Node 10.0.0.108:10801 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

问题原因:
    说是带添加在redis实例数据库不干净。

解决方案:
    (1)强杀指定redis实例,例如: kill -9 <redis 实例在PID>
    (2)删除强杀在redis的所有文件,例如: rm -rf /oldboyedu/data/redis10803/*
    (3)启动redis实例即可

1632630808045

谨此笔记,记录过往。凭君阅览,如能收益,莫大奢望。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇