Nota

Migra il cluster Redis a Oracle Cloud Infrastructure Cache

Introduzione

Questa esercitazione fornisce un processo dettagliato per la migrazione della cache del cluster Redis con partizionamento orizzontale mediante lo strumento pyredis-dump. pyredis-dump è uno strumento basato su Python progettato per semplificare il dump e il ripristino dei dati Redis. La procedura si applica a uno dei seguenti scenari di migrazione:

Sebbene questa esercitazione si concentri principalmente sulla migrazione di un cluster Redis in locale a OCI senza un'architettura master-slave, lo stesso approccio può essere applicato anche agli ambienti che utilizzano un'impostazione master-slave Redis.

Cos'è Redis?

Descrizione del cluster Redis

Il cluster Redis offre alta disponibilità e scalabilità orizzontale. Divide i dati in partizioni, distribuite su più nodi. Ogni partizione dispone di un nodo primario e di repliche per la tolleranza agli errori. Ciò consente al cluster Redis di gestire set di dati più grandi e un throughput di scrittura più elevato.

L'immagine seguente mostra un cluster a 3 nodi contenente 3 nodi come primario (M1, M2 e M3) con ogni nodo con slave come (S1, S2 e S3). La configurazione slave è facoltativa per fornire una disponibilità più elevata.

immagine

Redis Cluster non utilizza l'hashing coerente, ma una forma diversa di sharding in cui ogni chiave è concettualmente parte di quello che chiamiamo uno slot hash. Sono presenti 16384 slot hash nel cluster Redis. Ogni nodo in un cluster Redis è responsabile di un subset degli slot hash, per cui, ad esempio, potresti avere un cluster con 3 nodi, dove:

Che cos'è OCI Cache?

immagine

Obiettivi

Prerequisiti

Task 1: impostazione del cluster Redis di origine in un'impostazione in locale

  1. Configurare gli host in locale. Crea tre virtual machine (VM) o host fisico in locale utilizzando RHEL8.

    • Redis_Node1: 10.0.0.151
    • Redis_Node2: 10.0.0.164
    • Redis_Node3: 10.0.0.205
  2. Aprire la porta su tutti i nodi.

    Ogni nodo del cluster Redis richiede da 1 a 3 connessioni TCP aperte in base alla configurazione. La porta TCP Redis normale utilizzata per servire i client, ad esempio 6379, più la porta ottenuta aggiungendo 10000 alla porta dati, quindi 16379 per il cluster e 26379 per Sentinel. Assicurarsi di aprire tutte le porte nel firewall, altrimenti i nodi del cluster Redis non saranno in grado di comunicare. Eseguire i comandi seguenti.

    $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. Installare il software Redis su tutti i nodi. Eseguire il comando seguente per scaricare l'ultima Redis Community Edition 7.4 per RHEL8. Installare il software Redis su tutti e tre i nodi utilizzando l'utente root.

    $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. Preparare il file di configurazione Redis su tutti i nodi.

    Si consiglia di impostare un cluster Redis a tre nodi con almeno una replica (slave) per master e Sentinel per l'alta disponibilità. In questo esempio è stato creato un cluster a tre nodi senza repliche. Per configurare un cluster Redis, è necessario preparare il file di configurazione del cluster denominato redis.conf su tutti e tre i nodi nella cartella /redis.

    $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. Avviare il server Redis su tutti i nodi. Eseguire il comando seguente per avviare il server Redis su tutti e tre i nodi.

    [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
    

    Avvia il server Redis utilizzando un file di configurazione specifico e lo esegue in background.

    $redis-server /redis/redis.conf &
    
  6. Crea cluster Redis. Per creare un cluster Redis da tutti e tre i server Redis avviati in precedenza, eseguire il comando seguente da 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.
    

    Per i nodi è possibile visualizzare la chiave partizione:

    • Nodo 1 (10.0.0.151) 0-5460
    • Nodo 2 (10.0.0.164) 5461-10922
    • Nodo 3 (10.0.0.205) 10923-16383
  7. Connettersi al cluster Redis utilizzando la utility client Redis e convalidare i nodi del cluster.

    L'interfaccia della riga di comando Redis (nota anche come redis-cli) è un programma di terminale che invia comandi e legge le risposte dal server Redis. Redis-cli viene installato automaticamente come parte dell'installazione Redis.

    • Eseguire il comando $redis-cli -a pass123 -p 6379 -c per connettersi all'host locale.

    • Eseguire il comando $redis-cli -a pass123 -p 6379 -h 10.0.0.151 -c per connettersi da remoto.

    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. Eseguire l'operazione DML (Data Manipulation Language) nel cluster.

    In un ambiente Redis Cluster, i dati (operazione di lettura/scrittura) vengono reindirizzati automaticamente al nodo appropriato in base allo slot hash derivato dalla chiave della partizione.

    $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
    

    Salvare per garantire la persistenza dei dati in un cluster Redis in /redis/dump.rdb, è possibile utilizzare gli snapshot del database Redis (RDB) o i file AOF (Append-Only Files). L'RDB crea snapshot del database in memoria a intervalli specificati, mentre l'AOF registra tutte le operazioni di scrittura, consentendo la ricostruzione del database in caso di errore.

Task 2: impostare il cluster Redis di destinazione nella cache OCI

  1. Eseguire il login a OCI Console, passare a Database, OCI Cache e fare clic su Cluster.

    immagine

  2. Selezionare il compartimento rispettivo e fare clic su Crea cluster.

  3. Selezionare Nome, Crea nel compartimento e Versione del motore cache OCI del cluster.

    immagine

  4. Selezionare Modalità cluster, Conteggio partizioni, Nodo per partizione e Memoria per nodo.

    immagine

  5. Selezionare VCN e Subnet.

    immagine

  6. Rivedere e fare clic su Crea cluster.

    immagine

    Una volta completato il provisioning del cluster OCI, dovrebbe essere simile all'immagine seguente. È possibile visualizzare tre nodi cluster e l'endpoint privato.

    immagine

Task 3: Crea connessione al cluster cache OCI

La cache OCI è SaaS, non sarà disponibile un accesso diretto all'host Redis. Per connettersi alla cache OCI, dobbiamo creare un'istanza jump o un host bastion OCI nella subnet pubblica per accedere in modo sicuro ai cluster all'interno della stessa VCN e della stessa subnet.

immagine

Provisioning di bastion/jump in OCI

  1. Andare alla console OCI, andare a Identità e sicurezza e fare clic su Bastions.

  2. Selezionare il compartimento, la VCN, la subnet, la lista di inclusione blocchi CIDR e fare clic su Crea bastion.

    immagine

  3. Eseguire il comando seguente per installare Redis sull'host jump.

    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. Connettersi a uno dei nodi cluster dall'host di accesso rapido e convalidare i nodi.

    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
    

    È possibile visualizzare la chiave partizione:

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

Task 4: Migrazione dei dati da on-premise al cluster OCI utilizzando la utility Pyredis-dump

  1. Installare la utility pyredis-dump sull'host jump/bastion.

    $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. Controllare la connessione al cluster Redis in locale dall'host jump/bastion.

    [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. Esporta il dump dal cluster di origine. Connettersi a ogni nodo principale Redis dall'host jump e creare un dump remoto. Il dump verrà creato sull'host di salto nella posizione specificata.

    1. Eseguire il dump dal master 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. Eseguire il dump dal master 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. Eseguire il dump dal master 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. Prima di importare i dati, eseguire il cleanup del cluster di destinazione nella cache OCI e assicurarsi che non vi siano dati in nessuno dei nodi del cluster.

    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. Importa dump nel cluster di destinazione nella cache OCI. Connettersi a ogni cluster OCI e importare il dump nel rispettivo cluster in base alla chiave di partizione dell'origine e della destinazione. Il dump di esportazione del master Node1 con shard key 0-5460 deve essere importato rispettivamente nel master node1 del cluster di destinazione.

    È necessario importare il dump nei passi riportati di seguito. In caso contrario, l'operazione non riuscirà.

    1. Importa il dump da Node1 in 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. Importa il dump da Node2 in 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. Importa il dump da Node3 in 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. Convalida i dati in OCI. Dopo che i dati sono stati importati correttamente su ogni nodo cluster, connettersi a qualsiasi nodo cluster OCI e controllare i dati.

    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>
    

Conferme

Altre risorse di apprendimento

Esplora altri laboratori su docs.oracle.com/learn o accedi a più contenuti gratuiti sulla formazione su Oracle Learning YouTube channel. Inoltre, visita education.oracle.com/learning-explorer per diventare un Oracle Learning Explorer.

Per la documentazione del prodotto, visita l'Oracle Help Center.