redis 集群管理场景方案

一、创建集群

1、命令格式

更多请参考 1.6 redis-cli --cluster 说明

2、场景自测

(1)1个节点创建集群,失败

 展开源码

3、小结

 

无从节点

1、若无从节点,调用redis-cli --cluster create命令创建集群:

(1)1个节点:报错,提示Wrong number of arguments;
(2)2个节点:报错,提示At least 3 nodes are required.
(3)3个节点:成功,且默认初始化成3个主节点,slots按[0-5460] [5461-10922] [10923-16383]分布;
(3)N个节点(N>=3):成功,默认初始化成N个主节点;

2、若有从节点,调用redis-cli --cluster create --cluster-replicas 1 (指定从节点个数为1)命令创建集群:
(1)N个节点(N < 6):报错,提示At least 6 nodes are required;
(2)N个节点(N >= 6):成功,将创建N/2组节点,多余的节点将作为extra replicas节点挂到任一一组节点下。

二、缩容

(一)删除从节点

1、命令格式

redis-cli --cluster del-node <ip>:<port> <node_id>  -a <auth>

说明:

1)<ip>:<port> 是必传参数,集群中任一节点即可,通过该节点获取集群信息;

2)<node_id> 是待删除节点的节点id

3)<auth> 是集群密码;

注意:

1)从节点可以直接删除,且从节点删除之后,会被shutdown;

2)主节点不能直接删除,即使即使没有key也不能删除;

3)被删除掉的节点重新起来之后不能自动加入集群,但其和主的复制还是正常的,也可以通过该节点看到集群信息(通过其他正常节点已经看不到该被删除节点的信息)。

4)被删除节点若想要再次加入集群,需要先在该节点执行cluster reset命令,再用add-node命令加入集群,进行增量同步复制。

更多请参考 1.6 redis-cli --cluster 说明

2、场景自测

 展开源码

3、小结

(二)删除主节点

1、命令格式

redis-cli --cluster del-node <ip>:<port> <node_id>  -a <auth>

说明:

1)<ip>:<port> 是必传参数,集群中任一节点即可,通过该节点获取集群信息;

2)<node_id> 是待删除节点的节点id

3)<auth> 是集群密码;

注意:

1)从节点可以直接删除,且从节点删除之后,会被shutdown;

2)主节点不能直接删除,即使即使没有key也不能删除;

3)被删除掉的节点重新起来之后不能自动加入集群,但其和主的复制还是正常的,也可以通过该节点看到集群信息(通过其他正常节点已经看不到该被删除节点的信息)。

4)被删除节点若想要再次加入集群,需要先在该节点执行cluster reset命令,再用add-node命令加入集群,进行增量同步复制。

2、场景自测

# step-1:查看当前集群健康状态

[root@owner-test01 ~]# redis-cli --cluster check 192.168.251.134:7000 -a RedisCaocao1818

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

192.168.251.134:7000 (4b38e5e9...) -> 0 keys | 5461 slots | 0 slaves.

192.168.251.135:7001 (dd96f8dc...) -> 1 keys | 5461 slots | 0 slaves.

192.168.251.134:7002 (68212ae7...) -> 3 keys | 5462 slots | 1 slaves.

[OK] 5 keys in 3 masters.

0.00 keys per slot on average.

>>> Performing Cluster Check (using node 192.168.251.134:7000)

M: 4b38e5e9bc22a354c032b172a762a8eb760ea73a 192.168.251.134:7000

   slots:[0-5460] (5461 slots) master

S: 6b92b7f96fd4a6dcb76e3d8ff4c85ed6baafaab8 192.168.251.134:7001

   slots: (0 slots) slave

   replicates 68212ae7f976cb78d66f19388539ad6b51fe598e

M: dd96f8dc2c2d8403d7220f701531979354f18940 192.168.251.135:7001

   slots:[10923-16383] (5461 slots) master

M: 68212ae7f976cb78d66f19388539ad6b51fe598e 192.168.251.134:7002

   slots:[5461-10922] (5462 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.

# step-2:移除指定主节点192.168.251.134:7000

[root@owner-test01 ~]# redis-cli --cluster del-node 192.168.251.134:7000 4b38e5e9bc22a354c032b172a762a8eb760ea73a -a RedisCaocao1818

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

>>> Removing node 4b38e5e9bc22a354c032b172a762a8eb760ea73a from cluster 192.168.251.134:7000

[ERR] Node 192.168.251.134:7000 is not empty! Reshard data away and try again.

# 虽然该主节点没有keys,依旧报错,原因是该主节点有分配slots。

# 根据提示,需将slots先reshard迁移走再下线节点。

# step-3:将192.168.251.134:7000节点的5461个slots迁移到192.168.251.135:7001节点

[root@owner-test02 ~]#redis-cli --cluster reshard 192.168.251.134:7000 --cluster-from 4b38e5e9bc22a354c032b172a762a8eb760ea73a --cluster-to dd96f8dc2c2d8403d7220f701531979354f18940 --cluster-slots 5450 --cluster-yes -a RedisCaocao1818

# 迁移过程信息见slot迁移场景介绍

# step-4:再次尝试下线该主节点

[root@owner-test02 ~]# redis-cli --cluster del-node 192.168.251.134:7002 4b38e5e9bc22a354c032b172a762a8eb760ea73a -a RedisCaocao1818

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

>>> Removing node 4b38e5e9bc22a354c032b172a762a8eb760ea73a from cluster 192.168.251.134:7002

>>> Sending CLUSTER FORGET messages to the cluster...

>>> SHUTDOWN the node.

# 相关日志内容

10771:M 28 Feb 2020 04:44:52.827 - Accepted 192.168.251.135:35614

10771:M 28 Feb 2020 04:44:52.828 . I/O error reading from node link: connection closed

10771:M 28 Feb 2020 04:44:52.829 . I/O error reading from node link: connection closed

10771:M 28 Feb 2020 04:44:52.829 . I/O error reading from node link: connection closed

10771:M 28 Feb 2020 04:44:52.830 # User requested shutdown...

10771:M 28 Feb 2020 04:44:52.830 * Calling fsync() on the AOF file.

10771:M 28 Feb 2020 04:44:52.830 * Saving the final RDB snapshot before exiting.

10771:M 28 Feb 2020 04:44:52.836 * DB saved on disk

10771:M 28 Feb 2020 04:44:52.836 * Removing the pid file.

10771:M 28 Feb 2020 04:44:52.836 # Redis is now ready to exit, bye bye...

3、小结

二、扩容

(一)添加从节点

1、命令格式

redis-cli --cluster add-node <new_host>:<new_port> <existing_host>:<existing_port> --cluster-slave --cluster-master-id <master_node_id> -a <auth>

说明:

1)<new_host>:<new_port> 是新增节点,这里即新增从节点;

2)<existing_host>:<existing_port> 是集群中已存在的任一节点,用来获取集群信息;

3)--cluster-slave 是从节点标识,即添加的新节点是从节点

4)--cluster-master-id <master_node_id> 是主节点的node id;如果不指定会把新增从节点随机分配到任意一个主节点;

5)<auth> 是集群密码;

2、场景自测

# step-1:检查当前集群状态

[root@owner-test01 logs]# redis-cli --cluster check 192.168.251.134:7002 -a RedisCaocao1818

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

192.168.251.134:7002 (68212ae7...) -> 0 keys | 5462 slots | 0 slaves.

192.168.251.135:7001 (dd96f8dc...) -> 0 keys | 5461 slots | 0 slaves.

192.168.251.134:7000 (4b38e5e9...) -> 0 keys | 5461 slots | 0 slaves.

[OK] 0 keys in 3 masters.

0.00 keys per slot on average.

>>> Performing Cluster Check (using node 192.168.251.134:7002)

M: 68212ae7f976cb78d66f19388539ad6b51fe598e 192.168.251.134:7002

   slots:[5461-10922] (5462 slots) master

M: dd96f8dc2c2d8403d7220f701531979354f18940 192.168.251.135:7001

   slots:[10923-16383] (5461 slots) master

M: 4b38e5e9bc22a354c032b172a762a8eb760ea73a 192.168.251.134:7000

   slots:[0-5460] (5461 slots) master

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

# step-2:新启动192.168.251.134:7001 redis实例

redis-server /ccdata/redis/7001/conf/redis.conf

# step-3:将192.168.251.134:7001加入集群,且作为192.168.251.134:7002主节点的从节点

[root@owner-test01 ~]# redis-cli --cluster add-node 192.168.251.134:7001 192.168.251.134:7002 --cluster-slave --cluster-master-id 68212ae7f976cb78d66f19388539ad6b51fe598e -a RedisCaocao1818

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

>>> Adding node 192.168.251.134:7001 to cluster 192.168.251.134:7002

>>> Performing Cluster Check (using node 192.168.251.134:7002)

M: 68212ae7f976cb78d66f19388539ad6b51fe598e 192.168.251.134:7002

   slots:[5461-10922] (5462 slots) master

M: dd96f8dc2c2d8403d7220f701531979354f18940 192.168.251.135:7001

   slots:[10923-16383] (5461 slots) master

M: 4b38e5e9bc22a354c032b172a762a8eb760ea73a 192.168.251.134:7000

   slots:[0-5460] (5461 slots) master

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

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

# 添加失败,提示192.168.251.134:7001节点下的node table不为空,这是因为该节点之前加入过其他集群,节点重启后并未重置节点node table

# step-4:登录192.168.251.134:7001节点,执行cluster reset命令,再次重试成功

[root@owner-test01 ~]# redis-cli --cluster add-node 192.168.251.134:7001 192.168.251.134:7002 --cluster-slave --cluster-master-id 68212ae7f976cb78d66f19388539ad6b51fe598e -a RedisCaocao1818

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

>>> Adding node 192.168.251.134:7001 to cluster 192.168.251.134:7002

>>> Performing Cluster Check (using node 192.168.251.134:7002)

M: 68212ae7f976cb78d66f19388539ad6b51fe598e 192.168.251.134:7002

   slots:[5461-10922] (5462 slots) master

M: dd96f8dc2c2d8403d7220f701531979354f18940 192.168.251.135:7001

   slots:[10923-16383] (5461 slots) master

M: 4b38e5e9bc22a354c032b172a762a8eb760ea73a 192.168.251.134:7000

   slots:[0-5460] (5461 slots) master

[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.251.134:7001 to make it join the cluster.

Waiting for the cluster to join

>>> Configure node as replica of 192.168.251.134:7002.

[OK] New node added correctly.

3、小结

(二)添加主节点

1、命令格式

redis-cli --cluster add-node <new_host>:<new_port> <existing_host>:<existing_port> -a <auth>

说明:

1)<new_host>:<new_port> 是新增节点,这里即新增主节点;

2)<existing_host>:<existing_port> 是集群中已存在的、任一节点,用来获取集群信息;

3)<auth> 是集群密码

2、场景自测

 展开源码

3、小结

三、slots迁移/重分配

1、命令格式

redis-cli --cluster reshard <host>:<port>  

    --cluster-from <source_node_id>

    --cluster-to <dest_node_id>

    --cluster-slots <arg>

    --cluster-yes

    --cluster-timeout <arg>

    --cluster-pipeline <arg>

    --cluster-replace

说明:

1)<host>:<port> 必传参数,集群中任一节点即可,通过该节点获取集群信息;

2)--cluster-from  <source_node_id> 指定源节点ID

3)--cluster-to <dest_node_id>  指定目标节点ID

4)--cluster-slots <arg> 指定待迁移slots的数量,,默认按slot id顺序生成reshard plan

5)--cluster-yes  若指定该参数,reshard plan不会交互式询问,直接执行;不指定参数,则列出reshard plan询问是否按plan执行;

6)--cluster-timeout <arg> 定义migrate命令的超时时间

7)--cluster-pipeline <arg> 定义cluster getkeysinslot命令一次取出的key数量,默认值是10

8)--cluster-replace 若有相同的key则replace

2、场景自测

 展开源码

3、小结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tudou186

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值