>_Cluster CentOS 7.2 – Configurando o Redis 3.0 Master/Slave – 1.4

cluster-redis

Neste quarto tutorial veremos a configuração do Redis como Master/Slave e do Sentinel Failover.

Passo 1 – Ajustando o arquivo de configuração 6379.conf no nó ba-vm-www-01.

[root@ba-vm-www1 ~]# cd /etc/redis/
[root@ba-vm-www1 redis]# cp 6379.conf 6379.conf-old
[root@ba-vm-www1 redis]# cp sentinel.conf sentinel.conf-old

6379.conf

[root@ba-vm-www1 redis]# vim 6379.conf
# maxmemory 
PARA
maxmemory 104857600

# maxmemory-policy noeviction
PARA
maxmemory-policy allkeys-lru

# requirepass foobared
PARA
requirepass senha-aqui

# masterauth 
PARA
masterauth senha-aqui

Passo 2 – Ajustando o arquivo de configuração 6379.conf nó ba-vm-www-02.

[root@ba-vm-www2 ~]# cd /etc/redis/
[root@ba-vm-www1 redis]# cp 6379.conf 6379.conf-old
[root@ba-vm-www1 redis]# cp sentinel.conf sentinel.conf-old

6379.conf

# maxmemory 
PARA
maxmemory 104857600

# maxmemory-policy noeviction
PARA
maxmemory-policy allkeys-lru

# requirepass foobared
PARA
requirepass senha-aqui

# masterauth 
PARA
masterauth senha-aqui

# slaveof  
PARA
slaveof 192.168.0.114 6379

Passo 3 – Iniciando o Redis em ambos os nós (ba-vm-www-01 e ba-vm-www-02).

[root@ba-vm-www1 redis]# systemctl restart redis_6379
[root@ba-vm-www2 redis]# systemctl restart redis_6379

Passo 4 – Acessando o Redis através do comando redis-cli no nó ba-vm-www-01.
AUTH – Autenticação
ping – Verifica o status do Redis.
set foo bar – Criando o registro.
get foo – Listando o registro.

[root@ba-vm-www1 redis]# redis-cli 
127.0.0.1:6379> AUTH senha-aqui
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379>

Passo 5 – Acessando o Redis através do comando redis-cli no nó ba-vm-www-02.

[root@ba-vm-www2 redis]# redis-cli 
127.0.0.1:6379> AUTH senha-aqui
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379>

Passo 6 – Configurando o arquivo Sentinel no nó ba-vm-www-01.

[root@ba-vm-www1 redis]# vim sentinel.conf
sentinel monitor mymaster 127.0.0.1 6379 2
PARA
sentinel monitor mymaster 192.168.0.114 6379 2

# sentinel auth-pass  
PARA
sentinel auth-pass mymaster senha-aqui

Passo 7 – Copiando o arquivo sentinel.conf para o nó ba-vm-www-02.

# scp sentinel.conf root@192.168.0.115:/etc/redis/

Passo 8 – Reiniciando o Sentinel em ambos os nós ba-vm-www-01 e ba-vm-www-02.

[root@ba-vm-www1 ~]# systemctl restart redis-sentinel
[root@ba-vm-www1 ~]# systemctl restart redis-sentinel

Passo 9 – Verificando o log do Sentinel em ambos os nós.
ba-vm-www-01.

[root@ba-vm-www1 redis]# tail -f /var/log/redis-sentinel.log 
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

11483:X 22 Feb 14:44:06.670 # Sentinel runid is a641ecf5e1ea95cf2270ef7c7dc09180045019b3
11483:X 22 Feb 14:44:06.670 # +monitor master mymaster 192.168.0.114 6379 quorum 2
11483:X 22 Feb 14:44:06.671 * +slave slave 192.168.0.115:6379 192.168.0.115 6379 @ mymaster 192.168.0.114 6379
11483:X 22 Feb 14:44:15.471 * +sentinel sentinel 192.168.0.115:26379 192.168.0.115 26379 @ mymaster 192.168.0.114 6379

ba-vm-www-02.

[root@ba-vm-www2 redis]# tail -f /var/log/redis-sentinel.log 
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

6824:X 22 Feb 14:44:13.427 # Sentinel runid is cef75c86e93a26b2c0b1bbcbdf8b3bdd868f3516
6824:X 22 Feb 14:44:13.427 # +monitor master mymaster 192.168.0.114 6379 quorum 2
6824:X 22 Feb 14:44:13.429 * +slave slave 192.168.0.115:6379 192.168.0.115 6379 @ mymaster 192.168.0.114 6379
6824:X 22 Feb 14:44:14.758 * +sentinel sentinel 192.168.0.114:26379 192.168.0.114 26379 @ mymaster 192.168.0.114 6379

Passo 10 – Aplicando um Stop no Redis do nó ba-vm-www-01.

[root@ba-vm-www1 ~]# systemctl stop redis_6379

Log do ba-vm-www-01.

11483:X 22 Feb 14:46:19.195 # +sdown master mymaster 192.168.0.114 6379
11483:X 22 Feb 14:46:19.309 # +new-epoch 1
11483:X 22 Feb 14:46:19.315 # +vote-for-leader cef75c86e93a26b2c0b1bbcbdf8b3bdd868f3516 1
11483:X 22 Feb 14:46:20.271 # +odown master mymaster 192.168.0.114 6379 #quorum 2/2
11483:X 22 Feb 14:46:20.272 # Next failover delay: I will not start a failover before Mon Feb 22 14:52:19 2016
11483:X 22 Feb 14:46:20.425 # +config-update-from sentinel 192.168.0.115:26379 192.168.0.115 26379 @ mymaster 192.168.0.114 6379
11483:X 22 Feb 14:46:20.425 # +switch-master mymaster 192.168.0.114 6379 192.168.0.115 6379
11483:X 22 Feb 14:46:20.425 * +slave slave 192.168.0.114:6379 192.168.0.114 6379 @ mymaster 192.168.0.115 6379

Log do ba-vm-www-02.

6824:X 22 Feb 14:46:19.229 # +sdown master mymaster 192.168.0.114 6379
6824:X 22 Feb 14:46:19.296 # +odown master mymaster 192.168.0.114 6379 #quorum 2/2
6824:X 22 Feb 14:46:19.296 # +new-epoch 1
6824:X 22 Feb 14:46:19.296 # +try-failover master mymaster 192.168.0.114 6379
6824:X 22 Feb 14:46:19.301 # +vote-for-leader cef75c86e93a26b2c0b1bbcbdf8b3bdd868f3516 1
6824:X 22 Feb 14:46:19.316 # 192.168.0.114:26379 voted for cef75c86e93a26b2c0b1bbcbdf8b3bdd868f3516 1
6824:X 22 Feb 14:46:19.359 # +elected-leader master mymaster 192.168.0.114 6379
6824:X 22 Feb 14:46:19.359 # +failover-state-select-slave master mymaster 192.168.0.114 6379
6824:X 22 Feb 14:46:19.459 # +selected-slave slave 192.168.0.115:6379 192.168.0.115 6379 @ mymaster 192.168.0.114 6379
6824:X 22 Feb 14:46:19.459 * +failover-state-send-slaveof-noone slave 192.168.0.115:6379 192.168.0.115 6379 @ mymaster 192.168.0.114 6379
6824:X 22 Feb 14:46:19.543 * +failover-state-wait-promotion slave 192.168.0.115:6379 192.168.0.115 6379 @ mymaster 192.168.0.114 6379
6824:X 22 Feb 14:46:20.375 # +promoted-slave slave 192.168.0.115:6379 192.168.0.115 6379 @ mymaster 192.168.0.114 6379
6824:X 22 Feb 14:46:20.376 # +failover-state-reconf-slaves master mymaster 192.168.0.114 6379
6824:X 22 Feb 14:46:20.420 # +failover-end master mymaster 192.168.0.114 6379
6824:X 22 Feb 14:46:20.420 # +switch-master mymaster 192.168.0.114 6379 192.168.0.115 6379
6824:X 22 Feb 14:46:20.420 * +slave slave 192.168.0.114:6379 192.168.0.114 6379 @ mymaster 192.168.0.115 6379

Passo 11 – Acessando o Redis do nó ba-vm-www-02.
Perceba que ele estará como Master. Estando como Master é possível alterar/criar um registro no banco.

[root@ba-vm-www2 ~]# redis-cli 
127.0.0.1:6379> AUTH senha-aqui
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> set foo bar2
OK
127.0.0.1:6379> get foo
"bar2"
127.0.0.1:6379> 

Passo 12 – Iniciando novamente o Redis no nó ba-vm-www-01.

[root@ba-vm-www1 ~]# systemctl start redis_6379

Passo 13 – Acessando o Redis no nó ba-vm-www-01.
Perceba que agora o nó ba-vm-www-01 estará como Slave, sem permissão para escrita.

[root@ba-vm-www1 ~]# redis-cli 
127.0.0.1:6379> AUTH senha-aqui
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> get foo
"bar2"
127.0.0.1:6379> set foo bar3
(error) READONLY You can't write against a read only slave.
127.0.0.1:6379> 

Passo 14 – Aplicando o Stop no Redis do nó ba-vm-www-02.

[root@ba-vm-www2 ~]# systemctl stop redis_6379

Log do ba-vm-www-01.

11483:X 22 Feb 14:49:18.178 # +sdown master mymaster 192.168.0.115 6379
11483:X 22 Feb 14:49:18.231 # +odown master mymaster 192.168.0.115 6379 #quorum 2/2
11483:X 22 Feb 14:49:18.231 # +new-epoch 2
11483:X 22 Feb 14:49:18.231 # +try-failover master mymaster 192.168.0.115 6379
11483:X 22 Feb 14:49:18.237 # +vote-for-leader a641ecf5e1ea95cf2270ef7c7dc09180045019b3 2
11483:X 22 Feb 14:49:18.248 # 192.168.0.115:26379 voted for a641ecf5e1ea95cf2270ef7c7dc09180045019b3 2
11483:X 22 Feb 14:49:18.299 # +elected-leader master mymaster 192.168.0.115 6379
11483:X 22 Feb 14:49:18.299 # +failover-state-select-slave master mymaster 192.168.0.115 6379
11483:X 22 Feb 14:49:18.399 # +selected-slave slave 192.168.0.114:6379 192.168.0.114 6379 @ mymaster 192.168.0.115 6379
11483:X 22 Feb 14:49:18.399 * +failover-state-send-slaveof-noone slave 192.168.0.114:6379 192.168.0.114 6379 @ mymaster 192.168.0.115 6379
11483:X 22 Feb 14:49:18.471 * +failover-state-wait-promotion slave 192.168.0.114:6379 192.168.0.114 6379 @ mymaster 192.168.0.115 6379
11483:X 22 Feb 14:49:19.303 # +promoted-slave slave 192.168.0.114:6379 192.168.0.114 6379 @ mymaster 192.168.0.115 6379
11483:X 22 Feb 14:49:19.303 # +failover-state-reconf-slaves master mymaster 192.168.0.115 6379
11483:X 22 Feb 14:49:19.372 # +failover-end master mymaster 192.168.0.115 6379
11483:X 22 Feb 14:49:19.372 # +switch-master mymaster 192.168.0.115 6379 192.168.0.114 6379
11483:X 22 Feb 14:49:19.373 * +slave slave 192.168.0.115:6379 192.168.0.115 6379 @ mymaster 192.168.0.114 6379

Log do ba-vm-www-02.

6824:X 22 Feb 14:49:18.164 # +sdown master mymaster 192.168.0.115 6379
6824:X 22 Feb 14:49:18.243 # +new-epoch 2
6824:X 22 Feb 14:49:18.248 # +vote-for-leader a641ecf5e1ea95cf2270ef7c7dc09180045019b3 2
6824:X 22 Feb 14:49:19.236 # +odown master mymaster 192.168.0.115 6379 #quorum 2/2
6824:X 22 Feb 14:49:19.236 # Next failover delay: I will not start a failover before Mon Feb 22 14:55:18 2016
6824:X 22 Feb 14:49:19.379 # +config-update-from sentinel 192.168.0.114:26379 192.168.0.114 26379 @ mymaster 192.168.0.115 6379
6824:X 22 Feb 14:49:19.379 # +switch-master mymaster 192.168.0.115 6379 192.168.0.114 6379
6824:X 22 Feb 14:49:19.379 * +slave slave 192.168.0.115:6379 192.168.0.115 6379 @ mymaster 192.168.0.114 6379

Passo 15 – Perceba que no Log, foi informado que o nó ba-vm-www-01 (192.168.0.114) retornou a ser o Master novamente.

127.0.0.1:6379> set foo bar3
(error) NOAUTH Authentication required.
127.0.0.1:6379> AUTH senha-aqui
OK
127.0.0.1:6379> set foo bar3
OK
127.0.0.1:6379> get foo
"bar3"
127.0.0.1:6379> 

Passo 16 – Aplicando o Start no Redis ddo nó ba-vm-www-02.

[root@ba-vm-www2 ~]# systemctl start redis_6379
[root@ba-vm-www2 ~]# redis-cli 
127.0.0.1:6379> AUTH senha-aqui
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> get foo
"bar3"
127.0.0.1:6379> set foo bar4
(error) READONLY You can't write against a read only slave.
127.0.0.1:6379> 

Fim da configuração do Redis e do Sentinel. O próximo passo da continuação do tutorial sobre Cluster com Docker, será a instalação e configuração do próprio Docker.

Fonte:
http://redis.io/topics/sentinel

>_Cluster CentOS 7.2 – Configurando o Redis 3.0 Master/Slave – 1.4
Tagged on:     

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

%d blogueiros gostam disto: