ノート:

RedisクラスタのOracle Cloud Infrastructure Cacheへの移行

イントロダクション

このチュートリアルでは、pyredis-dumpツールを使用してシャードされたRedis Clusterキャッシュを移行するためのステップバイステップのプロセスを提供します。 pyredis-dumpは、Redisデータのダンプとリストアを簡素化するために設計されたPythonベースのツールです。この手順は、次のいずれかの移行シナリオに適用されます。

このチュートリアルでは、主にマスター・スレーブ・アーキテクチャを使用せずにオンプレミスのRedisクラスタをOCIに移行することに重点を置いていますが、Redisマスター・スレーブ設定を使用する環境にも同じアプローチを適用できます。

Redisとは?

Redis Clusterとは?

Redis Clusterは、高可用性と水平スケーリングを提供します。データをシャードに分割し、複数のノードに分散します。各シャードには、フォルト・トレランスのためのプライマリ・ノードとレプリカがあります。これにより、Redis Clusterは、より大きなデータセットおよびより高い書込みスループットを処理できます。

次の図は、3つのノードがプライマリ(M1、M2およびM3)として含まれ、各ノードがスレーブ(S1、S2およびS3)である3ノード・クラスタを示しています。スレーブ構成は、より高可用性を実現するためにオプションです。

イメージ

Redis Clusterは、一貫したハッシュを使用しませんが、すべてのキーが概念的にハッシュ・スロットと呼ばれるものの一部である別の形式のシャーディングを使用します。Redisクラスタには16384個のハッシュ・スロットがあります。Redisクラスタ内のすべてのノードがハッシュ・スロットのサブセットを担当するため、たとえば、3つのノードを持つクラスタがあるとします。ここでは:

OCIキャッシュとは

イメージ

目的

前提条件

タスク1: オンプレミス設定でのソースRedisクラスタの設定

  1. オンプレミス・ホストを構成します。RHEL8を使用して、3つのオンプレミス仮想マシン(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ユーザーを使用して、3つすべてのノードに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構成ファイルを準備します。

    高可用性のために、マスターごとに少なくとも1つのレプリカ(スレーブ)とSentinelを使用して3つのノードRedisクラスタを設定することをお薦めします。この例では、レプリカなしで3つのノード・クラスタを作成しました。Redisクラスタを構成するには、/redisフォルダの下の3つのノードすべてで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サーバーを起動します。次のコマンドを実行して、3つのノードすべてで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クラスタの作成前述の3つの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 Database (RDB)スナップショットまたは追加専用ファイル(AOF)を使用できます。RDBは、指定された間隔でインメモリー・データベースのスナップショットを作成し、AOFはすべての書込み操作を記録して、障害発生時にデータベースを再構築できるようにします。

タスク2: OCIキャッシュでのターゲットRedisクラスタの設定

  1. OCIコンソールにログインし、「データベース」「OCIキャッシュ」に移動して「クラスタ」をクリックします。

    イメージ

  2. それぞれのコンパートメントを選択し、「クラスタの作成」をクリックします。

  3. クラスタの「名前」「コンパートメントに作成」および「OCIキャッシュ・エンジンのバージョン」を選択します。

    イメージ

  4. 「クラスタ・モード」「シャード数」「シャード当たりのノード」および「ノード当たりのメモリー」を選択します。

    イメージ

  5. 「VCN」および「サブネット」を選択します。

    イメージ

  6. 確認して、「クラスタの作成」をクリックします。

    イメージ

    OCIクラスタのプロビジョニングが完了すると、次のイメージのようになります。3つのクラスタ・ノードとプライベート・エンドポイントが表示されます。

    イメージ

タスク3: OCIキャッシュ・クラスタへの接続の作成

OCIキャッシュはSaaSで、Redisホストへの直接アクセスはありません。OCIキャッシュに接続するには、同じVCNおよびサブネット内のクラスタにセキュアにアクセスするために、ジャンプ・インスタンスまたはOCI要塞ホストをパブリック・サブネット上に作成する必要があります。

イメージ

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キャッシュでターゲット・クラスタをクリーン・アップし、どちらのクラスタ・ノードにもデータがないことを確認します。

    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キャッシュのターゲット・クラスタにダンプをインポートします。各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を参照してください。