注意:

将 Redis 集群迁移到 Oracle Cloud Infrastructure Cache

简介

本教程提供了使用 pyredis-dump 工具迁移分片 Redis 集群高速缓存的分步过程。pyredis-dump 是一个基于 Python 的工具,旨在简化 Redis 数据的转储和恢复。该过程适用于以下任一迁移方案:

虽然本教程主要侧重于在没有主从架构的情况下将本地 Redis 集群迁移到 OCI,但同样的方法也适用于使用 Redis 主从设置的环境。

什么是 Redis?

什么是 Redis 集群?

Redis Cluster 提供高可用性和横向扩展。它将数据划分为分片,分布在多个节点上。每个分片都有一个主节点和副本以实现容错。这允许 Redis Cluster 处理较大的数据集和较高的写入吞吐量。

下图显示一个 3 节点群集,其中包含 3 个节点作为主节点(M1、M2 和 M3),每个节点的从属节点为(S1、S2 和 S3)。从属服务器配置是可选的,可以提供更高的可用性。

图像

Redis 集群不使用一致的散列,而是使用不同形式的分片,其中每个密钥在概念上都是我们称之为散列槽的一部分。Redis 集群中有 16384 个散列插槽。Redis 集群中的每个节点都负责哈希插槽的子集,例如,您可能有一个包含 3 个节点的集群,其中:

什么是 OCI Cache?

图像

目标

先决条件

任务 1:在内部部署设置中设置源 Redis 集群

  1. 配置内部部署主机。使用 RHEL8 创建三个内部部署虚拟机 (Virtual Machine,VM) 或物理主机。

    • Redis_Node1: 10.0.0.151
    • Redis_Node2: 10.0.0.164
    • Redis_Node3: 10.0.0.205
  2. 在所有节点上打开端口。

    每个 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
    
  3. 在所有节点上安装 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
    
  4. 在所有节点上准备 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
    
  5. 在所有节点上启动 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 &
    
  6. 创建 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
  7. 使用 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>
    
  8. 在集群中执行数据操纵语言 (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 集群

  1. 登录到 OCI 控制台,导航到数据库OCI 高速缓存,然后单击集群

    图像

  2. 选择相应的区间,然后单击创建集群

  3. 选择群集名称在区间中创建OCI 高速缓存引擎版本

    图像

  4. 选择 Cluster mode(群集模式)Shard count(分片计数)Node per Shard(每个分片节点)Memory per node(每个节点的内存)

    图像

  5. 选择 VCN子网

    图像

  6. 查看并单击创建集群

    图像

    OCI 集群预配完成后,它应如下图所示。我们可以看到三个集群节点和专用端点

    图像

任务 3:创建与 OCI 高速缓存集群的连接

OCI Cache 为 SaaS,不会直接访问 Redis 主机。要连接到 OCI Cache,需要在公共子网上创建跳转实例或 OCI 堡垒主机,以安全访问同一 VCN 和子网中的集群。

图像

在 OCI 中预配堡垒/跳跃

  1. 转到 OCI 控制台,导航到身份与安全,然后单击堡垒

  2. 选择区间、VCN、子网、 CIDR 块允许列表,然后单击创建堡垒

    图像

  3. 运行以下命令在跳转主机上安装 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
    
  4. 从跳转主机连接到任一群集节点并验证节点。

    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

任务 4:使用 Pyredis-dump 实用程序将数据从内部部署迁移到 OCI 集群

  1. 在跳转/堡垒主机上安装 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
    
  2. 检查从跳转/堡垒主机到内部部署 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>
    
  3. 从源集群导出转储。从跳转主机连接到每个 Redis 主节点并创建远程转储。将在给定位置的跳转主机上创建转储。

    1. 从主 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
      
    2. 从主 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
      
    3. 从主 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
      
  4. 在导入数据之前,请在 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
    
  5. 将转储导入 OCI Cache 中的目标集群。连接到每个 OCI 集群,并根据源和目标的分片密钥将转储导入到相应的集群。具有 shard key 0-5460 的主 Node1 的导出转储应分别导入到目标群集的主 node1。

    我们需要在以下步骤中导入转储,否则将失败。

    1. 将转储从 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
      
    2. 将转储从 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
      
    3. 将转储从 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]#
      
  6. 验证 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>
    

确认

更多学习资源

浏览 docs.oracle.com/learn 上的其他实验室,或者访问 Oracle Learning YouTube 渠道上的更多免费学习内容。此外,请访问 education.oracle.com/learning-explorer 成为 Oracle Learning Explorer。

有关产品文档,请访问 Oracle 帮助中心