
バックナンバー
| 役割 | Redisスクリプト | ポート |
| マスター | redis_6381 | 6381 |
| スレーブ | redis_6382 | 6382 |
| スレーブ | redis_6383 | 6383 |
上記構成で作ります。
redis_6381をマスターにして、ダウン時にスレーブと切り替わる動きをSentinelで行いたいと思います。
これまで作ってきたredis_6379サービスを無効化します。
# /etc/init.d/redis_6379 stop Stopping ... Redis stopped
# chkconfig redis_6379 --list
# chkconfig redis_6379 --list redis_6379 0:off 1:off 2:off 3:off 4:off 5:off 6:off
予定通り、redis_6381をマスター。redis_6382, redis_6383をスレーブにします。
# redis-cli -p 6382 slaveof 127.0.0.1 6381 OK
# redis-cli -p 6383 slaveof 127.0.0.1 6381 OK
確認します。
# redis-cli -p 6381 info replication # Replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6382,state=online,offset=74721,lag=0 slave1:ip=127.0.0.1,port=6383,state=online,offset=74721,lag=0 master_repl_offset:74721 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:4856 repl_backlog_histlen:69866
# redis-cli -p 6382 info replication # Replication role:slave master_host:127.0.0.1 master_port:6381 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:74987 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:40480
# redis-cli -p 6383 info replication # Replication role:slave master_host:127.0.0.1 master_port:6381 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:75400 slave_priority:100 slave_read_only:1 connected_slaves:0 master_repl_offset:0 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0
Redisプロセス用ディレクトリ作成
# mkdir /var/run/redis
ソースからSentinelのファイルを複製して使います
# cp /usr/local/src/redis-*/sentinel.conf /etc/redis/sentinel_1.conf
# vi /etc/redis/26381.conf daemonize yes port 26381 pidfile sentinel_26381.pid dir /var/run/redis logfile /var/log/redis/26381.log sentinel monitor mymaster 127.0.0.1 6381 2 sentinel down-after-milliseconds mymaster 5000 sentinel auth-pass mymaster syncPass
# vi /etc/redis/26382.conf daemonize yes port 26382 pidfile sentinel_26382.pid dir /var/run/redis logfile /var/log/redis/26382.log sentinel monitor mymaster 127.0.0.1 6381 2 sentinel down-after-milliseconds mymaster 5000 sentinel auth-pass mymaster syncPass
# vi /etc/redis/26383.conf daemonize yes port 26383 pidfile sentinel_26383.pid dir /var/run/redis logfile /var/log/redis/26383.log sentinel monitor mymaster 127.0.0.1 6381 2 sentinel down-after-milliseconds mymaster 5000 sentinel auth-pass mymaster syncPass
# groupadd redis
# useradd -s /sbin/nologin -M -g redis redis
# chmod 644 /etc/redis/2638*.conf
# chown redis:redis /etc/redis/2638*.conf
redis_6381サービスの起動ファイルを流用します。
# cp -p /etc/init.d/redis_6381 /etc/init.d/redis-sentinel_1
Sentinel用に変更します。
# vi /etc/init.d/redis-sentinel_1
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# chkconfig: - 58 74
# description: redis_6379 is the redis daemon.
#REDISPORT=6379
REDISPORT=6381
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
(略)
↓変更
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# chkconfig: 345 75 15
REDISPORT=26381
EXEC=/usr/local/bin/redis-sentinel
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis/sentinel_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
(略)
好みでrestartをつけています。
# cat /etc/init.d/redis-sentinel_1
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# chkconfig: 345 75 15
REDISPORT=26381
CLIEXEC=/usr/local/bin/redis-cli
EXEC=/usr/local/bin/redis-sentinel
PIDFILE=/var/run/redis/sentinel_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
start(){
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
}
stop(){
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
}
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart)
stop
start
;;
*)
echo "Please use start or stop as first argument"
;;
esac
# cp -p /etc/init.d/redis-sentinel_1 /etc/init.d/redis-sentinel_2
# cp -p /etc/init.d/redis-sentinel_1 /etc/init.d/redis-sentinel_3
# vi /etc/init.d/redis-sentinel_2
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# chkconfig: 345 75 15
REDISPORT=26382 ←変更
EXEC=/usr/local/bin/redis-sentinel
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis/sentinel_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
(略)
# vi /etc/init.d/redis-sentinel_3
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
# chkconfig: 345 75 15
REDISPORT=26383
EXEC=/usr/local/bin/redis-sentinel
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis/sentinel_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
(略)
Sentinelを起動させます。
# /etc/init.d/redis-sentinel_1 start Starting Redis server...
# /etc/init.d/redis-sentinel_2 start Starting Redis server...
# /etc/init.d/redis-sentinel_3 start Starting Redis server...
自動起動設定をonにします。
# chkconfig redis-sentinel_1 on # chkconfig redis-sentinel_2 on # chkconfig redis-sentinel_3 on
# chkconfig --list | grep redis redis-sentinel_1 0:off 1:off 2:on 3:on 4:on 5:on 6:off redis-sentinel_2 0:off 1:off 2:on 3:on 4:on 5:on 6:off redis-sentinel_3 0:off 1:off 2:on 3:on 4:on 5:on 6:off redis_6379 0:off 1:off 2:off 3:off 4:off 5:off 6:off redis_6381 0:off 1:off 2:on 3:on 4:on 5:on 6:off redis_6382 0:off 1:off 2:on 3:on 4:on 5:on 6:off redis_6383 0:off 1:off 2:on 3:on 4:on 5:on 6:off
役割の確認をします。
# redis-cli -p 6381 -a syncPass info | grep role role:master
# redis-cli -p 6382 -a syncPass info | grep role role:slave
# redis-cli -p 6383 -a syncPass info | grep role role:slave
マスターをダウンさせます。
# /etc/init.d/redis_6381 stop Stopping ... Redis stopped
フェイルオーバの確認
状態を見ていきます。
# redis-cli -p 6381 -a syncPass info | grep role Could not connect to Redis at 127.0.0.1:6381: Connection refused
ダウンしています。
# redis-cli -p 6382 -a syncPass info | grep role role:slave
# redis-cli -p 6383 -a syncPass info | grep role role:master
redis_6383サービスがマスターに切り替わり、フェイルオーバしていることがわかりますね!
お疲れ様です。
セス負荷対策などNginxへの移行案件が多いこの頃。IBM SoftLayerやAWSなどクラウド案件も多くなってきました。
