附註:

將 Redis 叢集移轉至 Oracle Cloud Infrastructure Cache

簡介

本教學課程提供使用 pyredis-dump 工具移轉分區 Redis 叢集快取的逐步程序。pyredis-dump 是一種 Python 式工具,可簡化 Redis 資料的傾印和回復。此程序適用於下列其中一個移轉案例:

雖然此教學課程主要著重於在沒有主從架構的情況下,將內部部署 Redis 叢集移轉至 OCI,但也可將相同的方法套用至使用 Redis 主從設定的環境。

Redis 是什麼?

什麼是 Redis 叢集?

Redis Cluster 提供高可用性和水平調整。它會將資料分割成分區,並分散到多個節點。每個分區都有一個容錯的主要節點和複本。這可讓 Redis 叢集處理較大的資料集和較高的寫入傳輸量。

下列影像顯示一個 3 節點叢集,其中包含 3 個節點作為主要節點 (M1、M2 和 M3),每個節點的從屬節點為 (S1、S2 和 S3)。從屬站組態是選擇性的,可提供更高的可用性。

圖像

Redis Cluster 不使用一致的雜湊,而使用不同形式的分區,其中每個金鑰的概念上都是我們所呼叫雜湊槽的一部份。Redis Cluster 中有 16384 個雜湊插槽。Redis 叢集中的每個節點都負責雜湊槽位的子集,例如,您可以有一個含有 3 個節點的叢集,其中:

什麼是 OCI Cache?

圖像

目標

必要條件

作業 1:在內部部署設定中設定來源 Redis 叢集

  1. 設定內部部署主機。使用 RHEL8 建立三個內部部署虛擬機器 (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 軟體。執行下列命令以下載適用於 RHEL8 的最新 Redis Community Edition 7.4。使用 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 組態檔。

    建議設定三個節點 Redis 叢集,每個主要節點至少要有一個複本 (從屬站),而 Sentinel 則提供高可用性。在此範例中,我們建立了三個沒有複本的節點叢集。若要設定 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.
    

    我們可以看到下列節點的分區索引鍵

    • Node-1 (10.0.0.151) 0-5460
    • Node-2 (10.0.0.164) 5461-10922
    • Node-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. 在叢集中執行「資料處理語言 (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/dump.rdb 之下的 Redis 叢集的資料持續性,您可以使用 Redis 資料庫 (RDB) 快照或「僅附加檔案 (AOF)」。RDB 會以指定的間隔建立記憶體內資料庫的快照,而 AOF 會記錄所有寫入作業,允許在發生失敗時重建資料庫。

作業 2:在 OCI 快取中設定目標 Redis 叢集

  1. 登入 OCI 主控台,瀏覽至資料庫OCI 快取,然後按一下叢集

    圖像

  2. 選取個別的區間,然後按一下建立叢集

  3. 選取叢集名稱在區間中建立以及 OCI 快取引擎版本

    圖像

  4. 選取叢集模式分區數目每一分區的節點數以及每一節點的記憶體數

    圖像

  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
    

    我們可以看到分區索引鍵

    • Cluster-1 (1-1) 0-5461
    • Cluster-2 (2-1) 5462-10922
    • Cluster-3 (3-1) 5462-10922

作業 4:使用 Pyredis-dump 公用程式將資料從內部部署移轉至 OCI 叢集

  1. 在 jump/bastion 主機上安裝 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 快取中清除目標叢集,並且確定其中一個叢集節點中沒有任何資料。

    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 Help Center