レプリカセットのローリングメンテナンス (再起動、OS パッチ、マイナーアップグレード)
MongoDBレプリカセットコマンド
// 1) Before starting: all members healthy, low lag, and an oplog window longer than the maintenance
rs.printSecondaryReplicationInfo()
// 2) Clean shutdown on the SECONDARY under maintenance (then OS/binary work, restart with the same config):
db.getSiblingDB("admin").shutdownServer({ timeoutSecs: 60 })
// 3) Wait until the member is SECONDARY and lag has closed, then move to the next secondary:
rs.status().members.map(m => ({ host: m.name, state: m.stateStr, health: m.health }))
// 4) When all secondaries are done, on the PRIMARY: hand over to a secondary that catches up within 30 s
rs.stepDown(120, 30)
// 5) The old primary is now a SECONDARY: apply steps 2 and 3 to it概要
セカンダリを先に、プライマリを最後にして、メンバーを 1 つずつメンテナンスに入れる順序を示す。これにより、セットをサービスから外さずに再起動、OS パッチ、バイナリ更新を行える。
使用する場面
計画的な OS / カーネルの更新、マイナーバージョンのアップグレード、mongod パラメータの変更、ディスク / ハードウェアのメンテナンス時に。
注意
一度に複数のメンバーを停止しない。3 メンバーのセットで 2 つを失うと過半数を失う。各メンバーの後で SECONDARY 状態と遅延の解消を確認する。stepDown は短時間の書き込み中断を引き起こすため、ドライバーが retryable writes で再接続することを確認する。アービターがある場合は、同じ手順の中で扱う。