バックナンバー
役割 | Redisスクリプト | ポート |
マスター | redis_6381 | 6381 |
スレーブ | redis_6382 | 6382 |
スレーブ | redis_6383 | 6383 |
上記構成で作ります。
redis_6381をマスターにして、ダウン時にスレーブと切り替わる動きをSentinelで行いたいと思います。
これまで作ってきたredis_6379サービスを無効化します。
1 2 3 |
# /etc/init.d/redis_6379 stop Stopping ... Redis stopped |
1 |
# chkconfig redis_6379 --list |
1 2 |
# chkconfig redis_6379 --list redis_6379 0:off 1:off 2:off 3:off 4:off 5:off 6:off |
1 2 |
# redis-cli -p 6382 slaveof 127.0.0.1 6381 OK |
1 2 |
# redis-cli -p 6383 slaveof 127.0.0.1 6381 OK |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# 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 |
1 |
# mkdir /var/run/redis |
1 |
# cp /usr/local/src/redis-*/sentinel.conf /etc/redis/sentinel_1.conf |
1 2 3 4 5 6 7 8 9 10 |
# 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 |
1 2 3 4 5 6 7 8 9 10 |
# 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 |
1 2 3 4 5 6 7 8 9 10 |
# 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 |
1 |
# groupadd redis |
1 |
# useradd -s /sbin/nologin -M -g redis redis |
1 |
# chmod 644 /etc/redis/2638*.conf |
1 |
# chown redis:redis /etc/redis/2638*.conf |
1 |
# cp -p /etc/init.d/redis_6381 /etc/init.d/redis-sentinel_1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# 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" (略) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# 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 |
1 |
# cp -p /etc/init.d/redis-sentinel_1 /etc/init.d/redis-sentinel_2 |
1 |
# cp -p /etc/init.d/redis-sentinel_1 /etc/init.d/redis-sentinel_3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# 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" (略) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# 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" (略) |
1 2 |
# /etc/init.d/redis-sentinel_1 start Starting Redis server... |
1 2 |
# /etc/init.d/redis-sentinel_2 start Starting Redis server... |
1 2 |
# /etc/init.d/redis-sentinel_3 start Starting Redis server... |
1 2 3 |
# chkconfig redis-sentinel_1 on # chkconfig redis-sentinel_2 on # chkconfig redis-sentinel_3 on |
1 2 3 4 5 6 7 8 9 |
# 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 |
1 2 |
# redis-cli -p 6381 -a syncPass info | grep role role:master |
1 2 |
# redis-cli -p 6382 -a syncPass info | grep role role:slave |
1 2 |
# redis-cli -p 6383 -a syncPass info | grep role role:slave |
1 2 3 |
# /etc/init.d/redis_6381 stop Stopping ... Redis stopped |
フェイルオーバの確認
状態を見ていきます。
1 2 |
# redis-cli -p 6381 -a syncPass info | grep role Could not connect to Redis at 127.0.0.1:6381: Connection refused |
ダウンしています。
1 2 |
# redis-cli -p 6382 -a syncPass info | grep role role:slave |
1 2 |
# redis-cli -p 6383 -a syncPass info | grep role role:master |
redis_6383サービスがマスターに切り替わり、フェイルオーバしていることがわかりますね!
お疲れ様です。
セス負荷対策などNginxへの移行案件が多いこの頃。IBM SoftLayerやAWSなどクラウド案件も多くなってきました。