このセクションでは、冗長化した Redis のフェイルオーバー環境を構築する方法について紹介します。ここでは、3. Redis の冗長化 の紹介した master ノード × 1台( Redis_1 )と slave ノード × 2台( Redis_2、Redis_3 )の構成を前提にしています。

Redis のフェイルオーバーで使用する Redis-Sentinel があることを確認します。Redis をインストールする時に一緒にインストールされます。

[root@localhost ~]# which redis-sentinel
/usr/bin/redis-sentinel
[root@localhost ~]# 

Redis-Sentinel 設定ファイルの準備

Redis_1 の Redis-Sentinel 用の設定ファイル( /etc/redis/26381.conf )を準備します。

[root@localhost ~]# cp -rp /etc/redis-sentinel.conf /etc/redis/26381.conf
[root@localhost ~]# 

Redis_1 の Redis-Sentinel 設定ファイル( /etc/redis/26381.conf )を編集し、以下設定します。

[root@localhost ~]# vi /etc/redis/26381.conf 

sentinel monitor パラメーターでは、master ノードの Redis の IPアドレスとポート番号、そして master ノードに昇格できる slave ノード数を指定します。2とした場合には、3ノードのうち 2 ノードが昇格できる候補となります。
sentinel down-after-millisecons パラメーターでは、master ノードがダウンと検知する時間を指定します。ここでは、5000 ミリ秒( 5秒 )間 master ノードが無応答であった場合にフェイルオーバーするようにしています。
sentinel auth-pass パラメーターでは、Redis でパスワード設定している場合には、Redis で設定したパスワードを指定します。

bind 192.168.24.199
port 26381
daemonize yes
pidfile /var/run/redis-sentinel_26381.pid
logfile /var/log/redis/26381.log
dir /var/run/redis
sentinel monitor mymaster 192.168.24.199 6381 2
sentinel down-after-milliseconds mymaster 5000
sentinel auth-pass mymaster foobared

master ノードの Redis-Sentinel 設定ファイル( /etc/redis/26381.conf )を複製し、slave ノード( Redis_2、Redis_3 )の Redis-Sentinel 設定ファイルをそれぞれ準備します。

[root@localhost ~]# cp -rp /etc/redis/26381.conf /etc/redis/26382.conf
[root@localhost ~]# cp -rp /etc/redis/26381.conf /etc/redis/26383.conf
[root@localhost ~]# 

Redis_2 の Redis-Sentinel 設定ファイル( /etc/redis/26382.conf )を編集し、以下設定します。

[root@localhost ~]# vi /etc/redis/26382.conf 

port、pidfile、logfile のパラメーターを Redis_2 用に書き換えます。

port 26382
pidfile /var/run/redis-sentinel_26382.pid
logfile /var/log/redis/26382.log

Redis_3 の Redis-Sentinel 設定ファイル( /etc/redis/26383.conf )を編集し、以下設定します。

[root@localhost ~]# vi /etc/redis/26383.conf 

port、pidfile、logfile のパラメーターを Redis_2 用に書き換えます。

port 26383
pidfile /var/run/redis-sentinel_26383.pid
logfile /var/log/redis/26383.log

Redis-Sentinel 設定ファイルのアクセス権限を変更します。

[root@localhost ~]# chmod 644 /etc/redis/26381.conf 
[root@localhost ~]# chmod 644 /etc/redis/26382.conf 
[root@localhost ~]# chmod 644 /etc/redis/26383.conf 
[root@localhost ~]# 

Redis-Sentinel 設定ファイルのアクセス権と所有権を redis 起動ユーザーに変更します。

[root@localhost ~]# chown redis:redis /etc/redis/26381.conf 
[root@localhost ~]# chown redis:redis /etc/redis/26382.conf 
[root@localhost ~]# chown redis:redis /etc/redis/26383.conf 
[root@localhost ~]# 

Redis-Sentinel 起動スクリプトの準備

Redis_1 の Redis-Sentinel の起動スクリプト設定ファイル( /lib/systemd/system/redis-sentinel_26381.service )を準備します。

[root@localhost ~]# cp -rp /lib/systemd/system/redis-sentinel.service /lib/systemd/system/redis-sentinel_26381.service 
[root@localhost ~]# 

Redis-Sentinel の起動スクリプト/lib/systemd/system/redis-sentinel_26381.service )を編集し、以下設定します。

[root@localhost ~]# vi /lib/systemd/system/redis-sentinel_26381.service 

Redis_1 の Redis-Sentinel 用に ExecStart と ExecStop パラメーターを書き換えます。

ExecStart=/usr/bin/redis-sentinel /etc/redis/26381.conf --supervised systemd
ExecStop=/usr/libexec/redis-26381-shutdown redis-sentinel

master ノードの Redis-Sentinel 起動スクリプト( /lib/systemd/system/redis-sentinel_26381.service )を複製し、slave ノード( Redis_2、Redis_3 )の Redis-Sentinel 起動スクリプトをそれぞれ準備します。

[root@localhost ~]# cp -rp /usr/lib/systemd/system/redis-sentinel_26381.service /usr/lib/systemd/system/redis-sentinel_26382.service 
[root@localhost ~]# cp -rp /usr/lib/systemd/system/redis-sentinel_26381.service /usr/lib/systemd/system/redis-sentinel_26383.service 
[root@localhost ~]# 

Redis_2 の Redis-Sentinel 起動スクリプト( /lib/systemd/system/redis-sentinel_26382.service )を編集し、以下設定します。

[root@localhost ~]# vi /usr/lib/systemd/system/redis-sentinel_26382.service

Redis_2 の Redis-Sentinel 用に ExecStart と ExecStop パラメーターを書き換えます。

ExecStart=/usr/bin/redis-sentinel /etc/redis/26382.conf --supervised systemd
ExecStop=/usr/libexec/redis-26382-shutdown redis-sentinel

Redis_3 の Redis-Sentinel 起動スクリプト( /lib/systemd/system/redis-sentinel_26383.service )を編集し、以下設定します。

[root@localhost ~]# vi /usr/lib/systemd/system/redis-sentinel_26383.service

Redis_3 の Redis-Sentinel 用に ExecStart と ExecStop パラメーターを書き換えます。

ExecStart=/usr/bin/redis-sentinel /etc/redis/26383.conf --supervised systemd
ExecStop=/usr/libexec/redis-26383-shutdown redis-sentinel

Redis-Sentinel 停止スクリプトの準備

Redis_1 の Redis-Sentinel の停止スクリプト設定ファイル( /usr/libexec/redis-26381-shutdown )を準備します。

[root@localhost ~]# cp -rp /usr/libexec/redis-shutdown /usr/libexec/redis-26381-shutdown
[root@localhost ~]# 

Redis-Sentinel の停止スクリプト( /usr/libexec/redis-26381-shutdown )を編集し、以下設定します。

[root@localhost ~]# vi /usr/libexec/redis-26381-shutdown

Redis_1 の Redis-Sentinel 用に SERVICE_NAME、CONFIG_FILE、HOST、PORT のパラメーターを書き換えます。

if [ -z "$SERVICE_NAME" ]; then
   SERVICE_NAME=26381
fi

CONFIG_FILE="/etc/redis/26381.conf"

HOST=${HOST:-192.168.24.199}
if [ "$SERVICE_NAME" = 26381 ]; then
    PORT=${PORT:-26381}
else

master ノードの Redis-Sentinel 停止スクリプト( /usr/libexec/redis-26381-shutdown )を複製し、slave ノード( Redis_2、Redis_3 )の Redis-Sentinel 停止スクリプトをそれぞれ準備します。

[root@localhost ~]# cp -rp /usr/libexec/redis-26381-shutdown /usr/libexec/redis-26382-shutdown
[root@localhost ~]# cp -rp /usr/libexec/redis-shutdown /usr/libexec/redis-26383-shutdown
[root@localhost ~]# 

Redis_2 の Redis-Sentinel 停止スクリプト( /usr/libexec/redis-26382-shutdown )を編集し、以下設定します。

[root@localhost ~]# vi /usr/libexec/redis-26382-shutdown 

Redis_2 の Redis-Sentinel 用に SERVICE_NAME、CONFIG_FILE、HOST、PORT のパラメーターを書き換えます。

if [ -z "$SERVICE_NAME" ]; then
   SERVICE_NAME=26382
fi

CONFIG_FILE="/etc/redis/26382.conf"

HOST=${HOST:-192.168.24.199}
if [ "$SERVICE_NAME" = 26382 ]; then
    PORT=${PORT:-26382}
else

Redis_3 の Redis-Sentinel 停止スクリプト( /usr/libexec/redis-26383-shutdown )を編集し、以下設定します。

[root@localhost ~]# vi /usr/libexec/redis-26383-shutdown 

Redis_3 の Redis-Sentinel 用に SERVICE_NAME、CONFIG_FILE、HOST、PORT のパラメーターを書き換えます。

if [ -z "$SERVICE_NAME" ]; then
   SERVICE_NAME=26383
fi

CONFIG_FILE="/etc/redis/26383.conf"

HOST=${HOST:-192.168.24.199}
if [ "$SERVICE_NAME" = 26383 ]; then
    PORT=${PORT:-26383}
else

Redis-Sentinel の起動と自動起動設定

Redis_1 の Redis-Sentinel サービスを起動します。

■ 起動
[root@localhost ~]# systemctl start redis-sentinel_26381.service
[root@localhost ~]# 

■ 起動状態の確認 
[root@localhost ~]# systemctl status redis-sentinel_26381.service
● redis-sentinel_26381.service - Redis Sentinel
   Loaded: loaded (/usr/lib/systemd/system/redis-sentinel_26381.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2019-01-27 14:46:31 JST; 5s ago
 Main PID: 4404 (redis-sentinel)
   CGroup: /system.slice/redis-sentinel_26381.service
           └─4404 /usr/bin/redis-sentinel 192.168.24.199:26381 [sentinel]

 1月 27 14:46:31 localhost.localdomain systemd[1]: Starting Redis Sentinel...
 1月 27 14:46:31 localhost.localdomain systemd[1]: Started Redis Sentinel.
[root@localhost ~]# 

Redis_2 の Redis-Sentinel サービスを起動します。

■ 起動
[root@localhost ~]# systemctl start redis-sentinel_26382.service
[root@localhost ~]# 

■ 起動状態の確認 
[root@localhost ~]# systemctl status redis-sentinel_26382.service
● redis-sentinel_26382.service - Redis Sentinel
   Loaded: loaded (/usr/lib/systemd/system/redis-sentinel_26382.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2019-01-27 14:51:38 JST; 1s ago
 Main PID: 4460 (redis-sentinel)
   CGroup: /system.slice/redis-sentinel_26382.service
           └─4460 /usr/bin/redis-sentinel 192.168.24.199:26382 [sentinel]

 1月 27 14:51:38 localhost.localdomain systemd[1]: Starting Redis Sentinel...
 1月 27 14:51:38 localhost.localdomain systemd[1]: Started Redis Sentinel.
[root@localhost ~]# 

Redis_3 の Redis-Sentinel サービスを起動します。

■ 起動
[root@localhost ~]# systemctl start redis-sentinel_26383.service
[root@localhost ~]# 

■ 起動状態の確認
[root@localhost ~]# systemctl status redis-sentinel_26383.service
● redis-sentinel_26383.service - Redis Sentinel
   Loaded: loaded (/usr/lib/systemd/system/redis-sentinel_26383.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2019-01-27 14:50:27 JST; 5s ago
 Main PID: 4445 (redis-sentinel)
   CGroup: /system.slice/redis-sentinel_26383.service
           └─4445 /usr/bin/redis-sentinel 192.168.24.199:26383 [sentinel]

 1月 27 14:50:27 localhost.localdomain systemd[1]: Starting Redis Sentinel...
 1月 27 14:50:27 localhost.localdomain systemd[1]: Started Redis Sentinel.
[root@localhost ~]# 

Redis_1〜3 の Redis-Sentinel サービスの自動起動を有効化します。

■ 自動起動の有効化
[root@localhost ~]# systemctl enable redis-sentinel_26381 redis-sentinel_26382 redis-sentinel_26383
Created symlink from /etc/systemd/system/multi-user.target.wants/redis-sentinel_26381.service to /usr/lib/systemd/system/redis-sentinel_26381.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/redis-sentinel_26382.service to /usr/lib/systemd/system/redis-sentinel_26382.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/redis-sentinel_26383.service to /usr/lib/systemd/system/redis-sentinel_26383.service.
[root@localhost ~]# 

■ 自動起動の状態確認 
[root@localhost ~]# systemctl is-enabled redis-sentinel_26381 redis-sentinel_26382 redis-sentinel_26383
enabled
enabled
enabled
[root@localhost ~]# 

Redis のフェイルオーバー動作確認

Redis_1 〜 3 ノードの役割を確認します。ここでは、Redis_1 が master、Redis_2 と 3 が slave であることが確認できます。

■ Redis_1 の役割
[root@localhost ~]# redis-cli -h 192.168.24.199 -p 6381 -a foobared info | grep role
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
role:master
[root@localhost ~]# 

■ Redis_2 の役割
[root@localhost ~]# redis-cli -h 192.168.24.199 -p 6382 -a foobared info | grep role
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
role:slave
[root@localhost ~]# 

■ Redis_3 の役割
[root@localhost ~]# redis-cli -h 192.168.24.199 -p 6383 -a foobared info | grep role
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
role:slave
[root@localhost ~]# 

master である Redis_1 をシャットダウンします。

[root@localhost ~]# redis-cli -h 192.168.24.199 -p 6381 -a foobared shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
[root@localhost ~]# 

5秒程度待った後、slave である Redis_2 と 3 の役割を確認します。ここでは、Redis_2 が master ノードになっていることが確認 できます。

[root@localhost ~]# redis-cli -h 192.168.24.199 -p 6382 -a foobared info | grep role
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
role:master
[root@localhost ~]# 
[root@localhost ~]# redis-cli -h 192.168.24.199 -p 6383 -a foobared info | grep role
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
role:slave
[root@localhost ~]# 

Redis_2 のログを確認します。

[root@localhost ~]# tail -F /var/log/redis/6382.log
3015:S 27 Jan 2019 19:25:27.199 * MASTER <-> REPLICA sync started
3015:S 27 Jan 2019 19:25:27.199 # Error condition on socket for SYNC: Connection refused
3015:S 27 Jan 2019 19:25:28.202 * Connecting to MASTER 192.168.24.199:6381
3015:S 27 Jan 2019 19:25:28.202 * MASTER <-> REPLICA sync started
3015:S 27 Jan 2019 19:25:28.202 # Error condition on socket for SYNC: Connection refused
3015:S 27 Jan 2019 19:25:29.204 * Connecting to MASTER 192.168.24.199:6381
3015:S 27 Jan 2019 19:25:29.204 * MASTER <-> REPLICA sync started
3015:S 27 Jan 2019 19:25:29.204 # Error condition on socket for SYNC: Connection refused
3015:S 27 Jan 2019 19:25:30.207 * Connecting to MASTER 192.168.24.199:6381
3015:S 27 Jan 2019 19:25:30.207 * MASTER <-> REPLICA sync started
3015:S 27 Jan 2019 19:25:30.207 # Error condition on socket for SYNC: Connection refused
3015:S 27 Jan 2019 19:25:31.208 * Connecting to MASTER 192.168.24.199:6381
3015:S 27 Jan 2019 19:25:31.208 * MASTER <-> REPLICA sync started
3015:S 27 Jan 2019 19:25:31.208 # Error condition on socket for SYNC: Connection refused
3015:M 27 Jan 2019 19:25:32.209 # Setting secondary replication ID to b0bada393ca0e8aa9b23343d64666a3e3fe4582d, valid up to offset: 8197. New replication ID is 7d198017516a9ff4e1b3cfa943419b18670bafe9
3015:M 27 Jan 2019 19:25:32.209 * Discarding previously cached master state.
3015:M 27 Jan 2019 19:25:32.209 * MASTER MODE enabled (user request from 'id=26 addr=192.168.24.199:51566 fd=8 name=sentinel-a9ff03c6-cmd age=51 idle=0 flags=x db=0 sub=0 psub=0 multi=3 qbuf=140 qbuf-free=32628 obl=36 oll=0 omem=0 events=r cmd=exec')
3015:M 27 Jan 2019 19:25:32.209 # CONFIG REWRITE executed with success.

シャットダウンした Redis_1 を起動します。

■ 起動
[root@localhost ~]# systemctl start redis_1

■ 起動状態の確認
[root@localhost ~]# systemctl status redis_1
● redis_1.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis_1.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2019-01-27 19:28:44 JST; 22s ago
  Process: 5479 ExecStop=/usr/libexec/redis-shutdown-1 (code=exited, status=0/SUCCESS)
 Main PID: 5494 (redis-server)
   CGroup: /system.slice/redis_1.service
           └─5494 /usr/bin/redis-server 192.168.24.199:6381

 1月 27 19:28:44 localhost.localdomain systemd[1]: Stopped Redis persistent ...
 1月 27 19:28:44 localhost.localdomain systemd[1]: Starting Redis persistent...
 1月 27 19:28:44 localhost.localdomain systemd[1]: Started Redis persistent ...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# 

Redis_1 〜 3 ノードの役割を確認します。ここでは、Redis_2 が master、Redis_1 と 3 が slave であることが確認できます。

■ Redis_1 の役割
[root@localhost ~]# redis-cli -h 192.168.24.199 -p 6381 -a foobared info | grep role Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. role:slave [root@localhost ~]#
■ Redis_2 の役割 [root@localhost ~]# redis-cli -h 192.168.24.199 -p 6382 -a foobared info | grep role Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. role:master [root@localhost ~]#
■ Redis_3 の役割 [root@localhost ~]# redis-cli -h 192.168.24.199 -p 6383 -a foobared info | grep role Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. role:slave [root@localhost ~]#