【Redis】起動と停止、master/slaveの確認方法
つい先日、担当しているプロジェクトのサーバ障害がおこり、Redisのレプリ設定がごちゃごちゃになり、master,slaveの状態の確認とRedisサーバー自体の上げ下げを行ったので
その作業に使用したコマンド周りをメモ。
現在のプロジェクトはredisサーバー複数台をSentinelで管理しており、masterがダウンしたらslaveがmasterに昇格するようになっているので
現在masterになっているRedisサーバーを停止させ、本来masterであるべきRedisサーバーがslaveからmasterに戻ったのを確認したら、止めたサーバーを再度起動する方法で対応した。
(Redis単体にはフェイルオーバーの機能はないので、Redisだけでレプリを張ってる場合は別の手順が必要になります)
Redisサーバーの起動
1 |
$redis-server |
バックグラウンドでのRedisの起動
1 |
$ sudo /etc/init.d/redis start |
Redisの起動の確認(一例)
1 2 |
$ redis-cli ping PONG |
PONGが帰ってくれば起動してる
Redisクライアントの起動
1 |
$redis-cli |
Redisサーバーの停止
1 |
$redis-cli shutdown |
もしくは、Ctrl + Cでも停止できるが、その場合はメモリに残っているデータは消える。
master/slave設定の確認
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 |
$redis-cli info -masterの場合 # Replication role:master connected_slaves:1 slave0:ip=xx.xx.xx.xx,port=xxxx,state=online,offset=871612,lag=0 master_repl_offset:871763 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:2 repl_backlog_histlen:871762 -slaveの場合 # Replication role:slave master_host:xx.xx.xx.xx master_port:xxxx master_link_status:up master_last_io_seconds_ago:2 master_sync_in_progress:0 slave_repl_offset:870994 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のレプリについて
公式ドキュメント http://redis.io/topics/replication
- マスターは複数のスレーブをもてる
- スレーブは別のスレーブの親になれる
- マスター/スレーブのレプリケート処理はノンブロッキング
- スレーブは writable (2.6 からは slave-read-only 設定が追加され、デフォルトは read-only)
Redis 2.4 ではフェイルオーバーの仕組みはない。
スレーブをマスターに昇格するには、マスターを停止し、手動でスレーブ設定を無効化する必要がある。
スレーブ設定方法
1 2 |
redis 127.0.0.1:9999> slaveof localhost 6379 OK |
ヘルスチェック
スレーブは redis.conf の repl-ping-slave-period の間隔(デフォルトは10秒)でマスターに PING コマンドを送信している。
1 2 3 4 5 6 7 |
redis 127.0.0.1:9999> monitor OK 1329549788.726369 "monitor" 1329549798.046136 "PING" 1329549808.066132 "PING" 1329549818.086138 "PING" 1329549828.096214 "PING" |
master がダウンしている場合に info を確認すると
master_link_status が down
master_last_io_seconds_ago は -1 となる。
またダウンの場合のみダウン後の経過秒数がmaster_link_down_since_seconds で表示される。
関連記事
http://astone.jeez.jp/%e3%80%90redis%e3%80%91%e3%83%ac%e3%83%97%e3%83%aa%e3%82%b1%e3%83%bc%e3%82%b7%e3%83%a7%e3%83%b3%e3%81%ae%e8%a8%ad%e5%ae%9a%e3%81%a8masterslave%e3%81%ae%e7%a2%ba%e8%aa%8d%e6%96%b9%e6%b3%95/http://astone.jeez.jp/wp-content/uploads/2015/10/8203794182_8004f8f636_b-1024x683.jpghttp://astone.jeez.jp/wp-content/uploads/2015/10/8203794182_8004f8f636_b-150x150.jpgServerKVS,Redis,server
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 7781 bytes) in /home/milksoap/www/_itblog/wp-content/plugins/crayon-syntax-highlighter/crayon_wp.class.php on line 854