注意:
- 本教程需要访问 Oracle Cloud。要注册免费账户,请参阅开始使用 Oracle Cloud Infrastructure 免费套餐。
- 它对 Oracle Cloud Infrastructure 身份证明、租户和区间使用示例值。完成实验室后,请使用特定于云环境的那些值替换这些值。
将 Redis 集群迁移到 Oracle Cloud Infrastructure Cache
简介
本教程提供了使用 pyredis-dump
工具迁移分片 Redis 集群高速缓存的分步过程。pyredis-dump
是一个基于 Python 的工具,旨在简化 Redis 数据的转储和恢复。该过程适用于以下任一迁移方案:
- Oracle Cloud Infrastructure (OCI) 的任何其他云服务。
- 从本地部署到 OCI。
- 从 OCI 到 OCI。
虽然本教程主要侧重于在没有主从架构的情况下将本地 Redis 集群迁移到 OCI,但同样的方法也适用于使用 Redis 主从设置的环境。
什么是 Redis?
-
Redis 是一个开源的内存中数据结构存储,可以用作 NoSQL 数据库、高速缓存、消息代理和流处理引擎,以其速度和低延迟数据访问而闻名。
-
Redis 支持各种数据结构,如字符串、散列、列表、集、排序集、流等。
-
Redis 提供可选的数据持久性,允许将数据保存到磁盘,确保数据持久性,即使服务器重新启动也是如此。
-
Redis 提供横向可扩展性 (Redis Cluster) 和高可用性(Redis Sentinel - 主从属体系结构)的功能。
-
我们可以使用 Redis Sentinel 等高可用性组合来配置 Redis 集群。
什么是 Redis 集群?
Redis Cluster 提供高可用性和横向扩展。它将数据划分为分片,分布在多个节点上。每个分片都有一个主节点和副本以实现容错。这允许 Redis Cluster 处理较大的数据集和较高的写入吞吐量。
下图显示一个 3 节点群集,其中包含 3 个节点作为主节点(M1、M2 和 M3),每个节点的从属节点为(S1、S2 和 S3)。从属服务器配置是可选的,可以提供更高的可用性。
Redis 集群不使用一致的散列,而是使用不同形式的分片,其中每个密钥在概念上都是我们称之为散列槽的一部分。Redis 集群中有 16384 个散列插槽。Redis 集群中的每个节点都负责哈希插槽的子集,例如,您可能有一个包含 3 个节点的集群,其中:
- 节点 A 包含从 0 到 5500 的散列槽。
- 节点 B 包含从 5501 到 11000 的散列槽。
- 节点 C 包含从 11001 到 16383 的散列槽。
什么是 OCI Cache?
-
OCI Cache 集群 for Redis 是完全托管的 Redis 服务,可提供高性能、低延迟的缓存功能来加速应用。
-
它通过将经常访问的数据存储在内存中来支持实时数据访问,从而减少后端系统的负载并提高整体响应能力。
-
它支持两种群集模式:分片群集和非分片群集。
-
非分片群集配置有一个主节点和一个或多个副本节点,每个节点上都有重复的数据。
-
分片集群具有三个或更多分片,数据在集群中的分片之间拆分,以便每个分片都包含部分数据。每个分片就像一个群集,有一个主节点,最多四个副本节点。
-
分片集群在集群级别没有主端点或副本端点,就像非分片集群那样。相反,每个分片都有一个专用端点,您可以在连接到集群时使用。
目标
-
在内部部署 Redis 版本
7.4.2
中创建三个节点分片集群。 -
在 OCI Cache 版本
7.0.0
中创建三个节点分片集群。 -
将高速缓存数据从内部部署迁移到 OCI。
先决条件
-
访问 OCI 租户。
-
订阅 OCI 区域。
-
在租户中预配区间。
-
创建 VCN 和子网。
任务 1:在内部部署设置中设置源 Redis 集群
-
配置内部部署主机。使用 RHEL8 创建三个内部部署虚拟机 (Virtual Machine,VM) 或物理主机。
- Redis_Node1:
10.0.0.151
- Redis_Node2:
10.0.0.164
- Redis_Node3:
10.0.0.205
- Redis_Node1:
-
在所有节点上打开端口。
每个 Redis 集群节点都需要基于配置打开 1 到 3 个 TCP 连接。用于服务客户机的常规 Redis TCP 端口(例如
6379
),加上通过向数据端口添加 10000 获得的端口,因此群集为 16379,Sentinel 为 26379。确保打开防火墙中的所有端口,否则 Redis 集群节点将无法通信。运行以下命令。$firewall-cmd --permanent --add-port=6379/tcp ( For master/slave replication and clint connection) $firewall-cmd --permanent --add-port=16379/tcp ( For Redis Cluster ) $firewall-cmd --reload
-
在所有节点上安装 Redis 软件。运行以下命令以下载最新的 Redis Community Edition 7.4 for RHEL8。使用 root 用户在所有三个节点上安装 Redis 软件。
$mkdir /redis $wget http://download.redis.io/redis-stable.tar.gz $tar xvzf redis-stable.tar.gz $cd redis-stable $make redis-cli $make BUILD_TLS=yes $cp src/redis-cli /usr/local/bin/ $cp redis-server /usr/local/bin/ $redis-cli -v
-
在所有节点上准备 Redis 配置文件。
建议为每个主服务器和 Sentinel 设置一个具有至少一个副本(从属服务器)的三个节点 Redis 集群,以实现高可用性。在此示例中,我们创建了三个没有副本的节点群集。要配置 Redis 集群,必须在
/redis
文件夹下的所有三个节点上准备名为redis.conf
的集群配置文件。$cd /redis $vi redis.conf port 6379 timeout 0 protected-mode no tcp-keepalive 300 pidfile "/redis/redis-server.pid" logfile "/redis/redis-server.log" requirepass "pass123" dbfilename "dump.rdb" appendonly no masterauth "pass123" cluster-enabled yes cluster-config-file /redis/nodes.conf cluster-node-timeout 5000
-
在所有节点上启动 Redis 服务器。运行以下命令在所有三个节点上启动 Redis 服务器。
[root@instance-20250224-0709 redis]# redis-server 814355:C 16 Apr 2025 15:30:35.171 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 814355:C 16 Apr 2025 15:30:35.171 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 814355:C 16 Apr 2025 15:30:35.171 * Redis version=7.4.2, bits=64, commit=00000000, modified=0, pid=814355, just started 814355:C 16 Apr 2025 15:30:35.171 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 814355:M 16 Apr 2025 15:30:35.171 * Increased maximum number of open files to 10032 (it was originally set to 1024). 814355:M 16 Apr 2025 15:30:35.172 * monotonic clock: POSIX clock_gettime _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis Community Edition .-`` .-```. ```\/ _.,_ ''-._ 7.4.2 (00000000/0) 64 bit ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 814355 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | https://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 814355:M 16 Apr 2025 15:30:35.172 * Server initialized 814355:M 16 Apr 2025 15:30:35.173 * Loading RDB produced by version 7.4.2 814355:M 16 Apr 2025 15:30:35.173 * RDB age 9 seconds 814355:M 16 Apr 2025 15:30:35.173 * RDB memory usage when created 2.18 Mb 814355:M 16 Apr 2025 15:30:35.173 * Done loading RDB, keys loaded: 1, keys expired: 0. 814355:M 16 Apr 2025 15:30:35.173 * DB loaded from disk: 0.000 seconds 814355:M 16 Apr 2025 15:30:35.173 * Ready to accept connections tcp
使用特定的配置文件启动 Redis 服务器,并在后台运行它。
$redis-server /redis/redis.conf &
-
创建 Redis 集群。要从上面启动的所有三个 Redis 服务器创建 Redis 集群,请从 Node1 运行以下命令。
# redis-cli --cluster create 10.0.0.151:6379 10.0.0.164:6379 10.0.0.205:6379 -a pass123 output: >>> Performing hash slots allocation on 3 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 M: a89cae61ea55xxxxxxxxxxx8ea31c70b0dc290 10.0.0.151:6379 slots:[0-5460] (5461 slots) master M: b914e2031ceaxxxxxxxxxxxa6d7d5d67fa748 10.0.0.164:6379 slots:[5461-10922] (5462 slots) master M: 9e71454df3f0cbexxxxxxxxxxx6ddc34177f7465 10.0.0.205:6379 slots:[10923-16383] (5461 slots) master [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
我们可以看到节点的分片密钥:
- 节点 1 (
10.0.0.151
)0-5460
- 节点 2 (
10.0.0.164
)5461-10922
- 节点 3 (
10.0.0.205
)10923-16383
- 节点 1 (
-
使用 Redis 客户端实用程序连接到 Redis 集群并验证集群节点。
Redis 命令行界面(也称为 redis-cli)是一个终端程序,用于向 Redis 服务器发送命令并读取回复。Redis-cli 作为 Redis 安装的一部分自动安装。
-
运行
$redis-cli -a pass123 -p 6379 -c
命令以连接到本地主机。 -
运行
$redis-cli -a pass123 -p 6379 -h 10.0.0.151 -c
命令以从远程连接。
127.0.0.1:6379> cluster nodes b914e2031cea8bxxxxxxxxxxxe9a6d7d5d67fa748 10.0.0.164:6379@16379 master - 0 1740409776706 2 connected 5461-10922 a89cae61ea553420c7xxxxxxxxxxx8ea31c70b0dc290 10.0.0.151:6379@16379 myself,master - 0 1740409775000 1 connected 0-5460 9e71454df3f0cbvxxxxxxxxxxx46ddc34177f7465 10.0.0.205:6379@16379 master - 0 1740409776000 3 connected 10923-16383 127.0.0.1:6379>
-
-
在集群中执行数据操纵语言 (Data Manipulation Language,DML) 操作。
在 Redis 集群环境中,数据(读/写操作)会根据从分片密钥派生的散列槽自动重定向到相应的节点。
$redis-cli -a pass123 -p 6379 -c 127.0.0.1:6379> ping PONG 127.0.0.1:6379> set name Dharmesh -> Redirected to slot [5798] located at 10.0.0.164:6379 OK 10.0.0.164:6379> set age 40 -> Redirected to slot [741] located at 10.0.0.151:6379 OK 10.0.0.151:6379> set gender M -> Redirected to slot [15355] located at 10.0.0.205:6379 OK 127.0.0.1:6379> KEYS * 1) "name" 2) "age" 3) "gender" >SAVE 10.0.0.205:6379> exit
保存以确保 Redis 集群在
/redis/dump.rdb
下的数据持久性,您可以使用 Redis 数据库 (RDB) 快照或仅附加文件 (Append-Only Files,AOF)。RDB 按指定间隔创建内存中数据库的快照,而 AOF 记录所有写入操作,允许在发生故障时重建数据库。
任务 2:在 OCI 高速缓存中设置目标 Redis 集群
-
登录到 OCI 控制台,导航到数据库、 OCI 高速缓存,然后单击集群。
-
选择相应的区间,然后单击创建集群。
-
选择群集名称、在区间中创建和 OCI 高速缓存引擎版本。
-
选择 Cluster mode(群集模式)、 Shard count(分片计数)、 Node per Shard(每个分片节点)和 Memory per node(每个节点的内存)。
-
选择 VCN 和子网。
-
查看并单击创建集群。
OCI 集群预配完成后,它应如下图所示。我们可以看到三个集群节点和专用端点。
任务 3:创建与 OCI 高速缓存集群的连接
OCI Cache 为 SaaS,不会直接访问 Redis 主机。要连接到 OCI Cache,需要在公共子网上创建跳转实例或 OCI 堡垒主机,以安全访问同一 VCN 和子网中的集群。
在 OCI 中预配堡垒/跳跃
-
转到 OCI 控制台,导航到身份与安全,然后单击堡垒。
-
选择区间、VCN、子网、 CIDR 块允许列表,然后单击创建堡垒。
-
运行以下命令在跳转主机上安装 Redis。
sudo su - mkdir /redis wget http://download.redis.io/redis-stable.tar.gz tar xvzf redis-stable.tar.gz cd redis-stable make redis-cli make BUILD_TLS=yes cp src/redis-cli /usr/local/bin/ cp redis-server /usr/local/bin/ $redis-cli -v
-
从跳转主机连接到任一群集节点并验证节点。
Connect Cluster Node1 $redis-cli -h aaantxkdlyarjuuzfc5qgic3xdbxxxxxxxxxxxo6aj65pokndqtnwa-1-1.redis.us-ashburn-1.oci.oraclecloud.com --tls -c Connect Cluster Node2 $redis-cli -h aaantxkdlyarjuuzfc5qgic3xxxxxxxxxxxxa5vo6aj65pokndqtnwa-2-1.redis.us-ashburn-1.oci.oraclecloud.com --tls -c Connect Cluster Node3 $redis-cli -h aaantxkdlyarjuuzfc5qgic3xxxxxxxxxxxfeaa5vo6aj65pokndqtnwa-3-1.redis.us-ashburn-1.oci.oraclecloud.com --tls -c Validate OCI cluster Nodes: > cluster nodes b89bf86956a0ce19e6b4xxxxxxxxxxxcd41d0b78dd 10.0.15.21:6379@16379,aaantxkdlyarjuuzfc5qgicxxxxxxxxxxxvo6aj65pokndqtnwa-2-1.redis.us-ashburn-1.oci.oraclecloud.com master - 0 1740462307292 2 connected 5462-10922 ba2253bcdb56f2e5227xxxxxxxxxxx9672cb527eab 10.0.5.150:6379@16379,aaantxkdlyarjuuzfc5qgic3xdbxxxxxxxxxxxvo6aj65pokndqtnwa-3-1.redis.us-ashburn-1.oci.oraclecloud.com master - 0 1740462305276 0 connected 10923-16383 b59af7d22e86c5600xxxxxxxxxxxe17bf34ca5aab 10.0.61.100:6379@16379,aaantxkdlyarjuuzfc5qgicxxxxxxxxxxxa5vo6aj65pokndqtnwa-1-1.redis.us-ashburn-1.oci.oraclecloud.com myself,master - 0 1740462305000 1 connected 0-5461
我们可以看到分片密钥:
- 集群 1 (1-1)
0-5461
- 集群 2 (2-1)
5462-10922
- 集群 3 (3-1)
5462-10922
- 集群 1 (1-1)
任务 4:使用 Pyredis-dump 实用程序将数据从内部部署迁移到 OCI 集群
-
在跳转/堡垒主机上安装 pyredis-dump 实用程序。
$cd /redis/ $yum install git $git clone https://github.com/tkote/pyredis-dump.git $cd /redis/pyredis-dump $python -m pip install redis $python3 pyredis-dump.py -h
-
检查从跳转/堡垒主机到内部部署 Redis 集群的连接。
[root@jump ~]# redis-cli -a pass123 -p 6379 -h 10.0.0.151 -c Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 10.0.0.151:6379> [root@jump ~]# redis-cli -a pass123 -p 6379 -h 10.0.0.164 -c Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 10.0.0.164:6379> [root@jump ~]# redis-cli -a pass123 -p 6379 -h 10.0.0.205 -c Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 10.0.0.205:6379>
-
从源集群导出转储。从跳转主机连接到每个 Redis 主节点并创建远程转储。将在给定位置的跳转主机上创建转储。
-
从主 Node1 进行转储。
# python3 pyredis-dump.py -H 10.0.0.151 dump -o /redis/dump/rdump1 -w pass123 dumping to '/redis/dump/rdump1' connecting to {'db': 0, 'host': '10.0.0.151', 'port': 6379, 'ssl': None, 'password': 'pass123'} 1 keys, bulk_size: 1000, watch_keys: False dumped 1 records elapsed time: 0.399 seconds
-
从主 Node2 进行转储。
# python3 pyredis-dump.py -H 10.0.0.164 dump -o /redis/dump/rdump2 -w pass123 dumping to '/redis/dump/rdump2' connecting to {'db': 0, 'host': '10.0.0.164', 'port': 6379, 'ssl': None, 'password': 'pass123'} 1 keys, bulk_size: 1000, watch_keys: False dumped 1 records elapsed time: 0.004 seconds
-
从主 Node3 进行转储。
# python3 pyredis-dump.py -H 10.0.0.205 dump -o /redis/dump/rdump3 -w pass123 dumping to '/redis/dump/rdump3' connecting to {'db': 0, 'host': '10.0.0.205', 'port': 6379, 'ssl': None, 'password': 'pass123'} 1 keys, bulk_size: 1000, watch_keys: False dumped 1 records elapsed time: 0.006 seconds
-
-
在导入数据之前,请在 OCI Cache 中清除目标集群,并确保任一集群节点中没有数据。
OCI Cluster Node1 # redis-cli -h aaantxkdlyarjuuzfc5qgic3xdbckjxxxxxxxxxxxaj65pokndqtnwa-1-1.redis.us-ashburn-1.oci.oraclecloud.com --tls > flushall OK OCI Cluster Node2 # redis-cli -h aaantxkdlyarjuuzfc5qgic3xdbckjxxxxxxxxxxxj65pokndqtnwa-2-1.redis.us-ashburn-1.oci.oraclecloud.com --tls > flushall OK OCI Cluster Node3 # redis-cli -h aaantxkdlyarjuuzfc5qgic3xdbckjkxxxxxxxxxxx6aj65pokndqtnwa-3-1.redis.us-ashburn-1.oci.oraclecloud.com --tls > flushall OK
-
将转储导入 OCI Cache 中的目标集群。连接到每个 OCI 集群,并根据源和目标的分片密钥将转储导入到相应的集群。具有
shard key 0-5460
的主 Node1 的导出转储应分别导入到目标群集的主 node1。我们需要在以下步骤中导入转储,否则将失败。
-
将转储从 Node1 导入到 OCI Cluster1-1。
# python3 pyredis-dump.py -S -H aaantxkdlyarjuuzfxxxxxxxxxxxk5bfeaa5vo6aj65pokndqtnwa-1-1.redis.us-ashburn-1.oci.oraclecloud.com restore -i /redis/dump/rdump1 restore from '/redis/dump/rdump1' connecting to {'db': 0, 'host': 'aaantxkdlyarjuuzfcxxxxxxxxxxxkjk5bfeaa5vo6aj65pokndqtnwa-1-1.redis.us-ashburn-1.oci.oraclecloud.com', 'port': 6379, 'ssl': True} restored 1 records elapsed time: 0.023 seconds
-
将转储从 Node2 导入到 OCI Cluster2-1。
# python3 pyredis-dump.py -S -H aaantxkdlyarjuuzfc5xxxxxxxxxxxfeaa5vo6aj65pokndqtnwa-2-1.redis.us-ashburn-1.oci.oraclecloud.com restore -i /redis/dump/rdump2 restore from '/redis/dump/rdump2' connecting to {'db': 0, 'host': 'aaantxkdlyarjuuzfc5xxxxxxxxxxxk5bfeaa5vo6aj65pokndqtnwa-2-1.redis.us-ashburn-1.oci.oraclecloud.com', 'port': 6379, 'ssl': True} restored 1 records elapsed time: 0.024 seconds
-
将转储从 Node3 导入到 OCI Cluster3-1。
# python3 pyredis-dump.py -S -H aaantxkdlyarjuuzfc5qgixxxxxxxxxxxfeaa5vo6aj65pokndqtnwa-3-1.redis.us-ashburn-1.oci.oraclecloud.com restore -i /redis/dump/rdump3 restore from '/redis/dump/rdump3' connecting to {'db': 0, 'host': 'aaantxkdlyarjuuzfc5xxxxxxxxxxx5bfeaa5vo6aj65pokndqtnwa-3-1.redis.us-ashburn-1.oci.oraclecloud.com', 'port': 6379, 'ssl': True} restored 1 records elapsed time: 0.030 seconds [root@instance-20250223-1032 pyredis-dump]#
-
-
验证 OCI 中的数据。在每个集群节点上成功导入数据后,连接到任何 OCI 集群节点并检查数据。
Connect Cluster Node1 $redis-cli -h aaantxkdlyarjuuzfc5qgic3xdbckjk5bfxxxxxxxxxxx65pokndqtnwa-1-1.redis.us-ashburn-1.oci.oraclecloud.com --tls -c 6379> get name -> Redirected to slot [5798] located at aaantxkdlyarjxxxxxxxxxxx3xdbckjk5bfeaa5vo6aj65pokndqtnwa-2-1.redis.us-ashburn-1.oci.oraclecloud.com:6379 "Dharmesh" 6379> get age -> Redirected to slot [741] located at aaantxkdlyarjuuxxxxxxxxxxxbckjk5bfeaa5vo6aj65pokndqtnwa-1-1.redis.us-ashburn-1.oci.oraclecloud.com:6379 "40" 6379> get gender -> Redirected to slot [15355] located at aaantxkdlyarjxxxxxxxxxxxxxdbckjk5bfeaa5vo6aj65pokndqtnwa-3-1.redis.us-ashburn-1.oci.oraclecloud.com:6379 "M" 6379>
相关链接
确认
- 作者 — Dharmesh Patel(Oracle 北美云服务首席云架构师— NACIE)
更多学习资源
浏览 docs.oracle.com/learn 上的其他实验室,或者访问 Oracle Learning YouTube 渠道上的更多免费学习内容。此外,请访问 education.oracle.com/learning-explorer 成为 Oracle Learning Explorer。
有关产品文档,请访问 Oracle 帮助中心。
Migrate Redis Cluster to Oracle Cloud Infrastructure Cache
G33023-02
Copyright ©2025, Oracle and/or its affiliates.