redis集群的维护(redis-cli --cluster 命令用法)

news2024/10/7 12:22:37

目录

前言

1、 --cluster create  创建集群

2、--cluster check  检查集群

3、--cluster info 查看集群

4、--cluster fix  修复集群

5、--cluster rehard 迁移槽位

6、 --cluster rebalance 平衡集群

7、 --cluster  add-node    集群扩容

8、--cluster del-node      集群缩容

9、 --cluster call  在集群中执行命令

10、--cluster set-timeout     整个集群的cluster-node-timeout时间

11、--cluster import          导入输入至集群

12、--cluster backup         备份集群rdb文件


前言

redis-trib.rb是官方提供的Redis Cluster的管理工具,它提供了集群创建、检查、修复、均衡等命令行工具。Redis Cluster 在5.0之后取消了ruby脚本的支持,而是直接集合到redis-cli里,避免了再安装ruby的相关环境。

[root@localhost bin]# ./redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>
  check          host:port
                 --cluster-search-multiple-owners
  info           host:port
  fix            host:port
                 --cluster-search-multiple-owners
                 --cluster-fix-with-unreachable-masters
  reshard        host:port
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  add-node       new_host:new_port existing_host:existing_port
                 --cluster-slave
                 --cluster-master-id <arg>
  del-node       host:port node_id
  call           host:port command arg arg .. arg
                 --cluster-only-masters
                 --cluster-only-replicas
  set-timeout    host:port milliseconds
  import         host:port
                 --cluster-from <arg>
                 --cluster-from-user <arg>
                 --cluster-from-pass <arg>
                 --cluster-from-askpass
                 --cluster-copy
                 --cluster-replace
  backup         host:port backup_directory
  help           

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

Cluster Manager Options:
  --cluster-yes  Automatic yes to cluster commands prompts

下面我们来详细的说下各命令的用法和作用:

1、 --cluster create  创建集群

create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>

该命令用来创建集群,假设我现在想搭建三主三从的集群127.0.0.1:7001~7006

启动所有节点后,可通过如下命令进行搭建:

redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluster-replicas 1

--cluster-replicas 1 :表示每个主节点需要1个从节点。这里随机分配主从关系,如果需要定制,则可以不加该参数,使用add-node来定制,查看第7节

2、--cluster check  检查集群

check          host:port
                 --cluster-search-multiple-owners #检查是否有槽同时被分配给了多个节点

该命令经常使用,需要拥有槽位的所有节点为启动状态,用于检查集群状态是否正常,只有当所有的槽位正常时,集群状态才OK

3、--cluster info 查看集群

用于查看集群的简易信息

4、--cluster fix  修复集群

fix            host:port
                 --cluster-search-multiple-owners    
                 --cluster-fix-with-unreachable-masters  

该命令十分有用,说明如下:

 --cluster-search-multiple-owners : 是否修复多个拥有者的槽位。当集群中的槽位在迁移过程中,出现意外时,使用fix可使用该参数。fix功能,redis内部在槽位的某些异常情况下会交互式的询问操作者是否同意它的修复策略,一般情况下,默认即可。

 --cluster-fix-with-unreachable-masters : 是否修复不可达的主节点上的槽位。例如,集群中某个主节点就是坏掉了,也没有故障转移成功。此时如何恢复该主节点上的所有槽位呢,这时就可以使用该参数,会将处于该主节点上的所有槽位恢复到存活的主节点上(之前的数据会丢失,仅仅是恢复了槽位)。

5、--cluster rehard 迁移槽位

reshard        host:port
                 --cluster-from <arg>   #槽位来源的节点运行id,多个用,分割,all表示全部节点
                 --cluster-to <arg>       #目标节点的运行id,只允一个
                 --cluster-slots <arg>   #迁移的槽位数
                 --cluster-yes               #是否默认同意集群内部的迁移计划(默认同意就可以)
                 --cluster-timeout <arg>   #迁移命令(migrate)的超时时间
                 --cluster-pipeline <arg>  #迁移key时,一次取出 的key数量,默认10
                 --cluster-replace           #是否直接replace到目标节点

迁移一个或者多个(--cluster-from)节点上的--cluster-slots个槽位至一个目标节点(--cluster-to)上。

 --cluster-from 的值不为all的情况下, --cluster-from不能包含--cluster-to

可以使用如下命令查看整个集群中的所有运行id

[root@localhost bin]# ./redis-cli -h 192.168.84.1 -p 7001 cluster nodes
8ae862c1815ff89e49d6c8801ea0cb074e82855c 192.168.84.1:7006@17006 slave 222df2165b7b9dba291cfa572d472ad292637873 0 1694427906290 3 connected
f6df82f6c67fbe83049d6f95a8473221afee2ee6 192.168.84.1:7004@17004 slave 30c69d9e408015082c1a2145875cd1ec0c7a41ea 0 1694427905000 1 connected
222df2165b7b9dba291cfa572d472ad292637873 192.168.84.1:7003@17003 master - 0 1694427904000 3 connected 10923-16383
4cdc4c8127dc5ea8d15a239fa6c2f3c09b0d5f3a 192.168.84.1:7005@17005 slave 10a4381d9869e293cad95bf75482f506de9f43fe 0 1694427905255 2 connected
30c69d9e408015082c1a2145875cd1ec0c7a41ea 192.168.84.1:7001@17001 myself,master - 0 1694427902000 1 connected 0-5460
10a4381d9869e293cad95bf75482f506de9f43fe 192.168.84.1:7002@17002 master - 0 1694427902000 2 connected 5461-10922

通过集群的配置文件查看也可以

6、 --cluster rebalance 平衡集群

rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>     #迁移命令(migrate)的超时时间
                 --cluster-simulate              # 模拟rebalance操作,不会真正执行迁移操作
                 --cluster-pipeline <arg>    #定义 getkeysinslot命令一次取出的key数量,默认值为10
                 --cluster-threshold <arg>
                 --cluster-replace               #是否直接replace到目标节点

--cluster-weight: 槽位权重(浮点型)比值,例如(这里使用端口号代替运行id): 7001=1.0,7002=1.0,7003=2.0 则表示,总权重为4.0, 7001和7002将分配到 (16384/4)*1  个槽位,7003则是(16384/4)*2个槽位。

 --cluster-threshold: 平衡触发的阈值条件,默认为2.00%。例如上一步,7001和7002应该有4096个槽位,如果7001的槽位数不够4096个,且超过 (4096*0.02 约等于)82个及以上;或者7001的槽位数比4096少于82个及以上,则会触发自平衡。

7、 --cluster  add-node    集群扩容

add-node    new_host:new_port                  #新加入集群的ip和port

                existing_host:existing_port          #集群中任一节点的ip和port
                 --cluster-slave                             #新节点作为从节点,默认随机一个主节点
                 --cluster-master-id <arg>            #给新节点指定主节点,值为节点的运行id

可用于向集群中添加主、从节点(即扩容)

假设我现在集群中有7001 7002 7003三个主节点了,想为7001添加一个从节点127.0.0.1:7006,则可执行如下命令:

redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7001  --cluster-slave --cluster-master-id 30c69d9e408015082c1a2145875cd1ec0c7a41ea

 不传入--cluster-slave --cluster-master-id 参数,表示添加一个主节点。

当添加的是一个主节点时,此时,该主节点没有任何槽位,可以使用rebalance或者reshard来迁移槽位给它。

8、--cluster del-node      集群缩容

del-node    host:port node_id         #删除给定的一个节点,成功后关闭该节点服务

也就是缩容。

删除一个主节点要求先迁移走改主节点上的槽位(数据),这时就可以使用reshard命令了。

[root@localhost bin]# ./redis-cli --cluster del-node 192.168.84.1:7002 10a4381d9869e293cad95bf75482f506de9f43fe
>>> Removing node 10a4381d9869e293cad95bf75482f506de9f43fe from cluster 192.168.84.1:7002
[ERR] Node 192.168.84.1:7002 is not empty! Reshard data away and try again.

9、 --cluster call  在集群中执行命令

call           host:port command arg arg .. arg               #在集群的所有节点执行相关命令
                 --cluster-only-masters                                #是否只在主节点上执行
                 --cluster-only-replicas                                #是否只在从节点上执行

10、--cluster set-timeout     整个集群的cluster-node-timeout时间

        set-timeout    host:port milliseconds

设置整个集群的cluster-node-timeout时间

11、--cluster import          导入输入至集群

import         host:port
                 --cluster-from <arg>
                 --cluster-from-user <arg>
                 --cluster-from-pass <arg>
                 --cluster-from-askpass
                 --cluster-copy               #migrate时指定类型为copy
                 --cluster-replace           #migrate时指定类型为replace

将外部redis的数据导入集群 

redis-cli --cluster import 127.0.01:6379 --cluster-from 127.0.0.2:6379 --cluster-replace --cluster-copy

说明:外部Redis实例(127.0.0.2:6379)导入到集群中的任意一节点,倒入之后,原来集群的key变为空,导入到新集群的key会自动分片到各个mater节点的slot

--cluster-replace 如果集群(127.0.01:6379)中存在外部redis实例(127.0.0.2:6379)的key,则会覆盖掉(10.35.2.68:6379)的value

--cluster-copy 默认情况下,import 命令在向集群导入数据的同时,还会删除单机服务器中源数据。如果用户想要保留单机服务器中的数据,那么可以在执行命令的同时给定 –cluster-copy 选项 

该命令将正在运行的实例的所有键(从源实例中删除键)移动到指定的预先存在的 Redis 集群。

实际项目上,数据导入应该用redis-shake工具的比较多。

12、--cluster backup         备份集群rdb文件

backup         host:port backup_directory    

备份主节点上的数据值RDB文件

[root@localhost bin]# mkdir backup
[root@localhost bin]# ./redis-cli --cluster backup 192.168.84.1:7001  ./backup
>>> Performing Cluster Check (using node 192.168.84.1:7001)
M: 30c69d9e408015082c1a2145875cd1ec0c7a41ea 192.168.84.1:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 8ae862c1815ff89e49d6c8801ea0cb074e82855c 192.168.84.1:7006
   slots: (0 slots) slave
   replicates 222df2165b7b9dba291cfa572d472ad292637873
S: f6df82f6c67fbe83049d6f95a8473221afee2ee6 192.168.84.1:7004
   slots: (0 slots) slave
   replicates 30c69d9e408015082c1a2145875cd1ec0c7a41ea
M: 222df2165b7b9dba291cfa572d472ad292637873 192.168.84.1:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 4cdc4c8127dc5ea8d15a239fa6c2f3c09b0d5f3a 192.168.84.1:7005
   slots: (0 slots) slave
   replicates 10a4381d9869e293cad95bf75482f506de9f43fe
M: 10a4381d9869e293cad95bf75482f506de9f43fe 192.168.84.1: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.
>>> Node 192.168.84.1:7001 -> Saving RDB...
SYNC sent to master, writing 232 bytes to './backup/redis-node-192.168.84.1-7001-30c69d9e408015082c1a2145875cd1ec0c7a41ea.rdb'
Transfer finished with success.
>>> Node 192.168.84.1:7003 -> Saving RDB...
SYNC sent to master, writing 226 bytes to './backup/redis-node-192.168.84.1-7003-222df2165b7b9dba291cfa572d472ad292637873.rdb'
Transfer finished with success.
>>> Node 192.168.84.1:7002 -> Saving RDB...
SYNC sent to master, writing 206 bytes to './backup/redis-node-192.168.84.1-7002-10a4381d9869e293cad95bf75482f506de9f43fe.rdb'
Transfer finished with success.
Saving cluster configuration to: ./backup/nodes.json
[OK] Backup created into: ./backup

nodes.json保存的是集群结构:

[root@localhost bin]# cat nodes.json 
[
  {
    "name": "30c69d9e408015082c1a2145875cd1ec0c7a41ea",
    "host": "192.168.84.1",
    "port": 7001,
    "replicate": null,
    "slots": [[0,5460]],
    "slots_count": 5461,
    "flags": "master",
    "current_epoch": 1
  },
  {
    "name": "8ae862c1815ff89e49d6c8801ea0cb074e82855c",
    "host": "192.168.84.1",
    "port": 7006,
    "replicate": "222df2165b7b9dba291cfa572d472ad292637873",
    "slots": [],
    "slots_count": 0,
    "flags": "slave",
    "current_epoch": 3
  },
  {
    "name": "f6df82f6c67fbe83049d6f95a8473221afee2ee6",
    "host": "192.168.84.1",
    "port": 7004,
    "replicate": "30c69d9e408015082c1a2145875cd1ec0c7a41ea",
    "slots": [],
    "slots_count": 0,
    "flags": "slave",
    "current_epoch": 1
  },
  {
    "name": "222df2165b7b9dba291cfa572d472ad292637873",
    "host": "192.168.84.1",
    "port": 7003,
    "replicate": null,
    "slots": [[10923,16383]],
    "slots_count": 5461,
    "flags": "master",
    "current_epoch": 3
  },
  {
    "name": "4cdc4c8127dc5ea8d15a239fa6c2f3c09b0d5f3a",
    "host": "192.168.84.1",
    "port": 7005,
    "replicate": "10a4381d9869e293cad95bf75482f506de9f43fe",
    "slots": [],
    "slots_count": 0,
    "flags": "slave",
    "current_epoch": 2
  },
  {
    "name": "10a4381d9869e293cad95bf75482f506de9f43fe",
    "host": "192.168.84.1",
    "port": 7002,
    "replicate": null,
    "slots": [[5461,10922]],
    "slots_count": 5462,
    "flags": "master",
    "current_epoch": 2
  }
]

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1003086.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

实现读写分离(MySQL主从数据库)

读写别离&#xff08;Read/Write Splitting&#xff09;。 1.原理&#xff1a; 让主数据库&#xff08;master&#xff09;处理事务性增、改、删操作&#xff08;INSERT、UPDATE、DELETE&#xff09;&#xff0c;而从数据库&#xff08;slave&#xff09;处理SELECT查询操作。 …

论文复制ChatGPT按钮被发表,撤回后再曝多个类似案例;Midjourney 生成大师级的人像

&#x1f989; AI新闻 &#x1f680; 论文复制ChatGPT按钮被发表&#xff0c;撤回后再曝多个类似案例 摘要&#xff1a;一篇物理论文复制了ChatGPT按钮内容&#xff0c;经过两个月同行评审并在杂志上发表。这一现象被知名打假人发现后&#xff0c;发表商决定撤回该论文。此外…

doccano 文本标注工具使用

最近在做面向大模型的文本标注工作&#xff0c;涉及到多人协同的问题&#xff0c;因此用到了doccano工具。 这个工具可以使用docker进行安装&#xff0c;安装之后的启动也都比较简单。 关于基础使用&#xff08;例如管理员登录、新建任务、上传数据集等&#xff09;&#xff…

推荐 4 个本周 火火火火 的开源项目

本期推荐开源项目目录&#xff1a; 1. 一个集成了主流 AI 大模型的 APP 2. 模拟人类行为的小镇 3. 离线 OCR 软件 4. 流程图编辑框架 01 一个集成了主流 AI 大模型的 APP 该开源项目集成了主流的大语言模型、绘图模型&#xff0c;基于Flutter开发&#xff0c;支持问答聊天、文生…

10个值得收藏的3D任务角色下载网站

每个人都喜欢免费的东西。 无论是免费的 3D 角色还是游戏资产&#xff0c;我们都喜欢它们。 以下是可以为你的游戏获取免费 3D 角色的前 10 个网站的列表。 你可以将它们用于多种用途&#xff0c;例如 3D 打印或动画剪辑。 如果需要将下载的3D模型转换为其他格式&#xff0c;可…

科技资讯|苹果虚拟纸可在Vision Pro中为广告、书籍等提供MR内容和动画

近日&#xff0c;美国专利商标局正式授予苹果一项与虚拟纸张相关的专利。这是与虚拟纸张这项发明相关的第二项专利&#xff0c;鉴于苹果 Vision Pro 将于明年上市&#xff0c;那么我们离苹果实现虚拟纸张的发明又近了一步。 虚拟纸张将能够包含 2D、3D 和动画等 MR内容&#…

移远通信一站式Matter智能照明/电工解决方案,让家居生活互联互通更便捷

从原始时期的钻木取火&#xff0c;到19世纪爱迪生发明电灯&#xff0c;人类对于“光亮”的追求从未停止。而伴随着社会经济的发展&#xff0c;人们对于照明等家居生活的追求开始从“物质层面”逐步向“精神层面”转变&#xff0c;智能家居成为了当下社会智能技术走向大众的重要…

crAPI靶场学习记录

靶场搭建 [靶场下载地址](我fork了一份) docker安装&#xff0c;笔者是用的wsldocker. [lab0:**初始账户 **] 注册一个账户&#xff0c;邮箱为[APIqq.com]&#xff0c;密码为Admin123 登陆后访问对应IP的8025端口&#xff0c;接收邮件获取车辆信息。 [lab1:**访问其它用户车…

电脑ip地址在哪里看?很简单,3个方法!

在网络连接的世界中&#xff0c;iP地址是一个至关重要的标识&#xff0c;它帮助设备在互联网上进行通信。无论是在家庭网络还是企业环境中&#xff0c;了解如何查看电脑的iP地址都是必不可少的技能。本文将详细介绍电脑ip地址在哪里看的3个方法&#xff0c;帮助您轻松获取这一重…

c++深拷贝、浅拷贝

浅拷贝&#xff1a;简单的赋值拷贝包含指针拷贝 #include <iostream> #include <string.h> using namespace std; //深拷贝、浅拷贝 //浅拷贝&#xff1a;简单的赋值拷贝包含指针拷贝 //深拷贝&#xff1a;在堆区重新申请的空间&#xff0c;进行拷贝操作class Per…

《Java极简设计模式》第06章:适配器模式(Adapter)

作者&#xff1a;冰河 星球&#xff1a;http://m6z.cn/6aeFbs 博客&#xff1a;https://binghe.gitcode.host 文章汇总&#xff1a;https://binghe.gitcode.host/md/all/all.html 源码地址&#xff1a;https://github.com/binghe001/java-simple-design-patterns/tree/master/j…

flex 布局:一行固定个数,超出换行(流式布局)

一、flex基础 设置在父容器上的属性&#xff1a;display&#xff1a;flex&#xff0c;align-items(交叉轴对齐方式)&#xff0c;justify-content(主轴对齐方式)&#xff0c;flex-wrap(换行)。 设置在子容器上的属性&#xff0c;通过 flex: 1&#xff0c;简写了 flex-grow、fl…

2023最新计算机信息管理毕设选题分享

文章目录 0 前言1 java web 管理系统 毕设选题2 java web 平台/业务系统 毕设选题3 游戏设计、动画设计类 毕设选题 (适合数媒的同学)4 算法开发5 数据挖掘 毕设选题6 大数据处理、云计算、区块链 毕设选题7 网络安全 毕设选题8 通信类/网络工程 毕设选题9 嵌入式 毕设选题10 开…

高性能数据JS网格 Bryntum Grid 5.5.2 Crack

高性能数据网格 Bryntum Grid 是一个高性能的网络表格组件。它是用纯 JavaScript 构建的&#xff0c;并且可以轻松地与所有主要 JS 框架集成。 功能丰富 Bryntum Grid 具有您期望从专业网格组件获得的所有功能&#xff0c;包括&#xff1a; 很好的表现;很好的绩效 没有人喜欢缓…

关于卫星电话的冷知识

卫星电话功率 卫星电话的发射功率取决于以下几个因素&#xff1a; 卫星的轨道高度&#xff1a;卫星的轨道高度越高&#xff0c;卫星电话与卫星之间的距离越远&#xff0c;信号衰减越大&#xff0c;因此需要更大的发射功率。一般来说&#xff0c;同步轨道卫星&#xff08;GEO&…

[每周一更]-(第61期):Rust入门策略(持续更新)

一门语言的学习&#xff0c;就要从最基本的语法开始认识&#xff0c;再分析不同语言的区别&#xff0c;再加上实战&#xff0c;才能更快的学会&#xff0c;领悟到作者的设计思想&#xff1b; 介绍 Rust编程练习 开发工具VSCode及插件 社区驱动的 rust-analyzerEven Better T…

再生龙clonezilla系统镜像下载

官网 Clonezilla - 簡介 下载

uniapp video全屏 cover-view 失效

查看官方文档 video | uni-app官网 确定自己开发的是app端 还是 微信小程序&#xff0c;我开发的是app端&#xff0c;所以在nvue文件写&#xff0c;同时cover-view组件要在video组件内容。 我写了一个简单的视频组件&#xff0c;可以看我另一篇博客。 uniapp video&#xff08…

Windows排查恶意程序及一些工具

目录 基础排查 使用系统配置实用程序 检查启动项 检查注册表 工具 网络监控 系统监控 安全检测 文件活动监控 其他 基础排查 使用系统配置实用程序 按下Win R键组合打开运行对话框&#xff0c;然后输入"msconfig"并按Enter键。在"服务"选项卡…

Cpolar+Emlog搭建指南—在Ubuntu上轻松创建个人博客,无需专业技能

前言 博客作为使用最广的个人在互联网的发声网站&#xff0c;在很长一段时间里&#xff0c;都被大家视作在互联网上发声的主要通道之一。虽然之后出现的即时聊天工具和朋友圈崛起&#xff0c;抢夺了不少博客的空间&#xff0c;但其仍占有不小的市场份额。也正因博客很重要&…