/ HOWTO

Cluster de Redis 4 en Docker.


Montando un cluster Master-Slave de Redis con Docker y ejemplos de uso.

Hay varias imágenes de Redis para bajar del “hub” de Docker, nos quedamos con esta

# docker pull bitnami/redis:latest
latest: Pulling from bitnami/redis
21a7e111de8c: Pull complete
65e50e28a4a4: Pull complete
b94a89aafdb1: Pull complete
7487ef2491c8: Pull complete
fbabf81a333a: Pull complete
f532281efb89: Pull complete
898a96d873bf: Pull complete
669bbe1b9a26: Pull complete
Digest: sha256:bb3bfa6031bd1c1532653216f6d5149821b712818008f3864445c80459d6633f
Status: Downloaded newer image for bitnami/redis:latest

La imágen que bajamos mide 113 mbs.

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
bitnami/redis       latest              3235bef8ad2d        4 weeks ago         113MB

Arrancamos ahora un contenedor con Redis en modo “master”, indicamos la clave de acceso para darle cierta seguridad al cluster.

# docker run --name redis-master \
  -e REDIS_REPLICATION_MODE=master \
  -e REDIS_PASSWORD=masterpassword123 \
  bitnami/redis:latest

 Welcome to the Bitnami redis container
Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-redis
Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-redis/issues
Send us your feedback at [email protected]

nami    INFO  Initializing redis
redis   INFO  ==> Validating inputs...
redis   INFO  ==> Configuring replication settings...
redis   INFO  ==> Setting up Redis master...
redis   INFO
redis   INFO  ########################################################################
redis   INFO   Installation parameters for redis:
redis   INFO     Password: **********
redis   INFO     Replication Mode: master
redis   INFO   (Passwords are not shown for security reasons)
redis   INFO  ########################################################################
redis   INFO
nami    INFO  redis successfully initialized
INFO  ==> Starting redis...
31:C 27 Nov 17:23:52.324 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
31:C 27 Nov 17:23:52.324 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=31, just started
31:C 27 Nov 17:23:52.324 # Configuration loaded
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 4.0.2 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 31
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

Ahora arrancaremos el nodo “slave” o “esclavo” asociado al nodo “master” o “maestro”.

# docker run --name redis-slave \
  --link redis-master:master \
  -e REDIS_REPLICATION_MODE=slave \
  -e REDIS_MASTER_HOST=172.17.0.2 \
  -e REDIS_MASTER_PORT_NUMBER=6379 \
  -e REDIS_MASTER_PASSWORD=masterpassword123 \
  -e REDIS_PASSWORD=masterpassword123 \
  bitnami/redis:latest

28:S 27 Nov 17:24:01.402 * Ready to accept connections
28:S 27 Nov 17:24:01.402 * Connecting to MASTER 172.17.0.2:6379
28:S 27 Nov 17:24:01.404 * MASTER <-> SLAVE sync started
28:S 27 Nov 17:24:01.404 * Non blocking connect for SYNC fired the event.
28:S 27 Nov 17:24:01.405 * Master replied to PING, replication can continue...
28:S 27 Nov 17:24:01.406 * Partial resynchronization not possible (no cached master)
28:S 27 Nov 17:24:01.407 * Full resync from master: a55e4292580fa84c04640fce730c5a9ee8800888:0
28:S 27 Nov 17:24:01.454 * MASTER <-> SLAVE sync: receiving 175 bytes from master
28:S 27 Nov 17:24:01.454 * MASTER <-> SLAVE sync: Flushing old data
28:S 27 Nov 17:24:01.454 * MASTER <-> SLAVE sync: Loading DB in memory
28:S 27 Nov 17:24:01.454 * MASTER <-> SLAVE sync: Finished with success
28:S 27 Nov 17:24:01.455 * Background append only file rewriting started by pid 38
28:S 27 Nov 17:24:01.478 * AOF rewrite child asks to stop sending diffs.
38:C 27 Nov 17:24:01.478 * Parent agreed to stop sending diffs. Finalizing AOF...
38:C 27 Nov 17:24:01.478 * Concatenating 0.00 MB of AOF diff received from parent.
38:C 27 Nov 17:24:01.478 * SYNC append only file rewrite performed
38:C 27 Nov 17:24:01.479 * AOF rewrite: 0 MB of memory used by copy-on-write
28:S 27 Nov 17:24:01.504 * Background AOF rewrite terminated with success
28:S 27 Nov 17:24:01.504 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
28:S 27 Nov 17:24:01.504 * Background AOF rewrite finished successfully

Nos conectamos ahora con el cliente al nodo Master para probar la persistencia.

# redis-cli -h 172.17.0.2 -a masterpassword123
172.17.0.2:6379> set aa 1
OK
172.17.0.2:6379> set bb 2
OK
172.17.0.2:6379> get aa
"1"
172.17.0.2:6379> get bb
"2"
172.17.0.2:6379> ping
PONG
172.17.0.2:6379> quit

Pedimos estadísticas de uso del Nodo.

# redis-cli -h 172.17.0.2 -a masterpassword123 --stat
------- data ------ --------------------- load -------------------- - child -
keys       mem      clients blocked requests            connections
2          828.77K  2       0       22 (+0)             6
2          828.77K  2       0       23 (+1)             6
2          828.77K  2       0       24 (+1)             6
2          828.77K  2       0       25 (+1)             6
2          828.77K  2       0       26 (+1)             6
2          828.77K  2       0       27 (+1)             6
2          828.77K  2       0       28 (+1)             6
2          828.77K  2       0       29 (+1)             6
2          828.77K  2       0       30 (+1)             6
2          828.77K  2       0       31 (+1)             6

Con este comando, podemos ver todas las unidades de información almacenadas.

# redis-cli -h 172.17.0.2 -a masterpassword123 --scan
bb
aa

Ahora podemos buscar algunas mediante un texto (* es algo), en este caso, objetos que contengan en su nombre la letra “a”.

# redis-cli -h 172.17.0.2 -a masterpassword123 --scan --pattern '*a*'
aa

Podemos activar un monitor, donde veremos en pantalla todos los comandos que llegan al Nodo.

# redis-cli -h 172.17.0.2 -a masterpassword123 monitor
OK
1511798824.960750 [0 172.17.0.1:41126] "set" "zz" "999"
1511798829.839087 [0 172.17.0.1:41126] "get" "zz"

Verificación de latencia, mediante esta herramienta.

# redis-cli -h 172.17.0.2 -a masterpassword123 --latency-dist
---------------------------------------------
. - * #          .01 .125 .25 .5 milliseconds
1,2,3,...,9      from 1 to 9     milliseconds
A,B,C,D,E        10,20,30,40,50  milliseconds
F,G,H,I,J        .1,.2,.3,.4,.5       seconds
K,L,M,N,O,P,Q,?  1,2,4,8,16,30,60,>60 seconds
From 0 to 100%:
---------------------------------------------
.-*#123456789ABCDEFGHIJKLMNOPQ?
.-*#123456789ABCDEFGHIJKLMNOPQ?
.-*#123456789ABCDEFGHIJKLMNOPQ?
.-*#123456789ABCDEFGHIJKLMNOPQ?
.-*#123456789ABCDEFGHIJKLMNOPQ?

Ahora hacemos un testeo Least Recently Used (LRU).

# redis-cli -h 172.17.0.2 -a masterpassword123 --lru-test 1000
208750 Gets/sec | Hits: 208171 (99.72%) | Misses: 579 (0.28%)
228000 Gets/sec | Hits: 227966 (99.99%) | Misses: 34 (0.01%)
243250 Gets/sec | Hits: 243234 (99.99%) | Misses: 16 (0.01%)
216000 Gets/sec | Hits: 215990 (100.00%) | Misses: 10 (0.00%)
234750 Gets/sec | Hits: 234740 (100.00%) | Misses: 10 (0.00%)
233750 Gets/sec | Hits: 233741 (100.00%) | Misses: 9 (0.00%)
219250 Gets/sec | Hits: 219248 (100.00%) | Misses: 2 (0.00%)

Se puede pedir el estado del cluster desde la línea de comando. Nos dirá si es un Master o un Slave.

# redis-cli -h 172.17.0.3 -a masterpassword123 info replication

Es posible conectarnos a un nodo y cambiar el modo (master o slave) En este caso nos conectamos al nodo 2 y le decimos que pasará a ser “slave de” el nodo Master.

# redis-cli -h 172.17.0.3 -a masterpassword123
172.17.0.3:6379> slaveof 172.17.0.2 6379
OK
172.17.0.3:6379> info replication
# Replication
role:slave
master_host:172.17.0.2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:0
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:4e0d9092e2a021cb735473264ee2a2f468528f84
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:0

Verificaremos que lo que pasa en el primer nodo, se refleja en el slave, pasaremos a distintas sesiones, una conectada a “MASTER” y la otra a “SLAVE”.

MASTER
172.17.0.2:6379> get aa
"88"
SLAVE
172.17.0.3:6379> get aa
"88"
MASTER
172.17.0.2:6379> set aa 99
OK
SLAVE
172.17.0.3:6379> get aa
"99"

Y también verificamos que el nodo “slave” esta en modo lectura.

SLAVE
172.17.0.3:6379> set aa 77
(error) READONLY You can't write against a read only slave.

Para ver todos los objetos definidos en el cluster.

172.17.0.3:6379> KEYS *
1) "aa"
2) "bb"

172.17.0.3:6379> KEYS a?
1) "aa"

172.17.0.3:6379> keys *a*
1) "aa"
172.17.0.3:6379> keys *b*
1) "bb"

Como realizar copias de datos de Redis

# cp /var/lib/redis/dump.rdb /home/dac/redis_backup

Subscríbete y recibirás los últimos artículos semanalmente en tu email.