Redis 学习六 集群之水平扩展和缩减

2024-01-08  本文已影响0人  __Y_Q

Redis 3.0 后的版本虽然有了集群的功能, 提供了比哨兵模式更高的性能与可用性, 但是在集群水平的扩展和缩减确实比较麻烦的, 今天就来学习一下 Redis 高可用集群如何做水平扩展与缩减,.
集群实例还是和上一章一样, 采用6个节点分布在三台虚拟机上, 三主三从的模式.

1. 首先启动整个集群

/usr/local/src/redis-6.2.14/src/redis-server /usr/local/redis-cluster/8001/redis.conf
/usr/local/src/redis-6.2.14/src/redis-server /usr/local/redis-cluster/8002/redis.conf
/usr/local/src/redis-6.2.14/src/redis-server /usr/local/redis-cluster/8003/redis.conf
/usr/local/src/redis-6.2.14/src/redis-server /usr/local/redis-cluster/8004/redis.conf
/usr/local/src/redis-6.2.14/src/redis-server /usr/local/redis-cluster/8005/redis.conf
/usr/local/src/redis-6.2.14/src/redis-server /usr/local/redis-cluster/8006/redis.conf

2. 接着链接8001端口的 redis 实例

 /usr/local/src/redis-6.2.14/src/redis-cli -a zhangsan -c -h 192.168.50.88 -p 8001

3. 查看集群的状态

集群状态

从图里可以看出集群运行正常, 三个 master 节点和三个 slave 节点.

这三个 master 节点存储的所有 hash 槽组成 redis 集群的存储槽位, slave 节点是每个主节点的备份从节点, 不显示存储槽位.

4. 创建 8007 和 8008 节点

在上面集群基础上再新增一主 8007, 一从 8008. 并且复制 8001 文件夹下的 redis.conf 文件到 8007 和 8008 文件夹下.

[root@local_1 redis-cluster]# mkdir 8007 8008
[root@local_1 redis-cluster]# cd 8001
[root@local_1 8001]# cp redis.conf /usr/local/redis-cluster/8007/
[root@local_1 8001]# cp redis.conf /usr/local/redis-cluster/8008/
[root@local_1 8001]# vim /usr/local/redis-cluster/8007/redis.conf 

接着修改 8007 中的 redis.conf 文件, 使用命令

:/8001

查找redis.conf内所有包含8001的内容, 改为 8007
同理, 针对 8008 也是这样修改, 只是将redis.conf 中的内容改为 8008.
大概有以下四处需要修改.

port: 800x
pidfile /var/run/redis_800x.pid
dir /usr/local/redis-cluster/800x/
cluster-config-file nodes-800x.conf

5. 启动 8007 和 8008 节点

[root@local_1 8001]# /usr/local/src/redis-6.2.14/src/redis-server /usr/local/redis-cluster/8007/redis.conf 
[root@local_1 8001]# /usr/local/src/redis-6.2.14/src/redis-server /usr/local/redis-cluster/8008/redis.conf
[root@local_1 8001]# ps -ef | grep redis
root      6891     1  0 03:43 ?        00:00:05 /usr/local/src/redis-6.2.14/src/redis-server *:8001 [cluster]
root      6898     1  0 03:43 ?        00:00:05 /usr/local/src/redis-6.2.14/src/redis-server *:8004 [cluster]
root      6958     1  0 04:18 ?        00:00:00 /usr/local/src/redis-6.2.14/src/redis-server *:8007 [cluster]
root      6965     1  0 04:18 ?        00:00:00 /usr/local/src/redis-6.2.14/src/redis-server *:8008 [cluster]
root      6980  6706  0 04:19 pts/0    00:00:00 grep --color=auto redis

6. 配置 8007 为主节点

使用 add-node 命令先将8007添加到集群中.

 /usr/local/src/redis-6.2.14/src/redis-cli -a zhangsan --cluster add-node 192.168.50.88:8007 192.168.50.88:8001

前面的ip:port为新增节点,后面的ip:port为已知存在节.执行后输入内容如下

节点新增

接着查看集群状态

 /usr/local/src/redis-6.2.14/src/redis-cli -a zhangsan -c -h 192.168.50.88 -p 8001
新集群状态

可以看到, 8007 暂时还没有进行 slot hash 槽位的分配, 这时需要为新节点手动分配 hash 槽.

7. 手动分配 hash 槽位

使用redis-cli 命令为 8007 分配 hash 槽. 需要找到集群中任意一个主节点, 对其进行重新分片的工作.

/usr/local/src/redis-6.2.14/src/redis-cli -a zhangsan --cluster reshard 192.168.50.88:8001

输出内容如下

>>> Performing Cluster Check (using node 192.168.50.88:8001)
M: a6a10c95785bc6624d0bf7d8102d4985af41ac7f 192.168.50.88:8001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 2f75987f31a2b985521bec3a8e42bbb61db910c0 192.168.50.171:8006
   slots: (0 slots) slave
   replicates 43757801edfd97adbbb9f70a7e2a27c55ae75c0e
M: 43757801edfd97adbbb9f70a7e2a27c55ae75c0e 192.168.50.117:8002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 85cbcc1dba0ecd812d4108bad9ed0cf34e6eef67 192.168.50.171:8003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 9909cd1f0b53072c451a3ea98760940c5b179ac6 192.168.50.117:8005
   slots: (0 slots) slave
   replicates a6a10c95785bc6624d0bf7d8102d4985af41ac7f
M: 0485db294587639756bf9376862df4efb8085bf3 192.168.50.88:8007
   slots: (0 slots) master
S: 828314705c97d1dd006122b2a189868e7e177ab4 192.168.50.88:8004
   slots: (0 slots) slave
   replicates 85cbcc1dba0ecd812d4108bad9ed0cf34e6eef67
[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)? 600

这里是询问需要多少个槽移动到新的节点上,自己设置,比如600个hash槽. 输入600. 按下回车. 接着会输出

What is the receiving node ID?  0485db294587639756bf9376862df4efb8085bf3

这是询问要把这600个 hash 槽移动到哪个节点上去, 这里需要指定节点ID, 指定的是上面输出内容中 8007 对应的节点 ID 0485db294587639756bf9376862df4efb8085bf3 然后按回车键, 接着继续回询问

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 意思是: 从所有主节点 8001, 8002, 8003 中分别抽取相应的槽位数指定到新节点中, 抽取的总槽数位600个. 接着按下回车, 会进行槽位抽取. 最后还会询问, 是否确认开始执行分片任务. 这里输入 yes.

Do you want to proceed with the proposed reshard plan (yes/no)? yes

结束后, 再查看下最新的集群状态.


分片后的集群状态

可以看到, 现在8007 已经有 hash 槽位了, 也就是说可以在 8007 上进行读写数据了, 到此 8007 已经成功加入到集群中成为了主节点.

7. 配置 8008 为 8007 的子节点

同理, 先将 8008 通过 add-node 的形式添加到集群中.

 /usr/local/src/redis-6.2.14/src/redis-cli -a zhangsan --cluster add-node 192.168.50.88:8008 192.168.50.88:8001

然后查看是否添加成功.

8008添加到集群
看到是一个 master 节点, 也没有被分配任何 hash 槽, 需要执行 replicate 命令来指定当前节点(子节点)的主节点ID 是哪个, 那么就要先链接新添加的 8008 节点的客户端, 然后使用集群命令进行操作, 把自己指定到一个主节点8007下.
在 8008 客户端实例下执行.
192.168.50.88:8008> cluster replicate 0485db294587639756bf9376862df4efb8085bf3

0485db294587639756bf9376862df4efb8085bf3 是 8007 的 ID .

然后再使用 cluster nodes查看集群状态. 可以看到已经将8008变为了8007的子节点.

变为8007的子节点

8 删除8008 子节点

使用 del-node 命令删除节点. 指定删除节点ip和端口, 以及节点对应的id.

 /usr/local/src/redis-6.2.14/src/redis-cli -a zhangsan --cluster del-node 192.168.50.88:8008 c7f59d3eaa4135083b697794f971426bcc8ff038

输出

>>> Removing node c7f59d3eaa4135083b697794f971426bcc8ff038 from cluster 192.168.50.88:8008
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

再去查看节点状态, 8008这个子节点已经被移除.


删除8008节点

9 删除8007 主节点

最后, 删除之前加入的主节点 8007. 步骤会稍微麻烦, 因为主节点里面有分配 hash 槽. 所以必须先把8007中hash 槽放入到其他的可用主节点中去.然后再进行移除节点操作, 不然会出现数据丢失问题.(目前只能将数据迁移到一个节点上, 暂时做不了平均分配功能.) 执行命令如下.

/usr/local/src/redis-6.2.14/src/redis-cli -a zhangsan --cluster reshard 192.168.50.88:8007

输出内容

>>> Performing Cluster Check (using node 192.168.50.88:8007)
M: 0485db294587639756bf9376862df4efb8085bf3 192.168.50.88:8007
   slots:[0-198],[5461-5661],[10923-11121] (599 slots) master
M: 85cbcc1dba0ecd812d4108bad9ed0cf34e6eef67 192.168.50.171:8003
   slots:[11122-16383] (5262 slots) master
   1 additional replica(s)
M: 43757801edfd97adbbb9f70a7e2a27c55ae75c0e 192.168.50.117:8002
   slots:[5662-10922] (5261 slots) master
   1 additional replica(s)
S: 828314705c97d1dd006122b2a189868e7e177ab4 192.168.50.88:8004
   slots: (0 slots) slave
   replicates 85cbcc1dba0ecd812d4108bad9ed0cf34e6eef67
S: 9909cd1f0b53072c451a3ea98760940c5b179ac6 192.168.50.117:8005
   slots: (0 slots) slave
   replicates a6a10c95785bc6624d0bf7d8102d4985af41ac7f
M: a6a10c95785bc6624d0bf7d8102d4985af41ac7f 192.168.50.88:8001
   slots:[199-5460] (5262 slots) master
   1 additional replica(s)
S: 2f75987f31a2b985521bec3a8e42bbb61db910c0 192.168.50.171:8006
   slots: (0 slots) slave
   replicates 43757801edfd97adbbb9f70a7e2a27c55ae75c0e
[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)?  600

询问要迁移多少槽位. 这里输入 600, 然后回车, 会接着询问要迁移到那里, 这里指定 8001 主节点的ID.

What is the receiving node ID? a6a10c95785bc6624d0bf7d8102d4985af41ac7f

接着输入数据源, 也就是 8007 的ID.

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: 0485db294587639756bf9376862df4efb8085bf3

回车后, 再输入 done 开始生成迁移计划.

Source node #2:  done

最后输入 yes, 开始迁移

Do you want to proceed with the proposed reshard plan (yes/no)? yes

然后再看一下集群状态.


槽位迁移

发现8007 下已经没有任何槽位信息了, 说明迁移成功.

最后直接使用 del-node 命令删除 8007节点即可.

 /usr/local/src/redis-6.2.14/src/redis-cli -a zhangsan --cluster del-node 192.168.50.88:8007 0485db294587639756bf9376862df4efb8085bf3

最后查询一次集群状态.


完全删除

大功告成

上一篇 下一篇

猜你喜欢

热点阅读