Server Swappingパターンとautofs
以前の記事でEC2インスタンスに障害が発生した場合の復旧スクリプトを書きました。
スクリプトでは障害時にAMIを作成しているので、EBSがアタッチされているインスタンスの場合はそれも含めてスナップショットから新しいもので復旧されます。
しかし、CDPのServer Swappingパターンのようにインスタンスに外付けのEBSがある場合、インスンタンスのみを新しく作成してEBSを新しいものに付け替える場合があります。
インスタンスのみ新しく起動し、EBSを付け替える場合もスクリプト化して自動化が出来そうです。
しかし、その場合の手順が以下のようになりEBSをアタッチした後にOS側からマウントする必要があります。
せっかく自動化したのでSSHで接続してmountコマンド実行なんてしたくありません。
やり方はいくつかあると思いますが、今回はautofsを使ってみたいと思います。
autofsはユーザーがアクセスした場合に自動的にファイルシステムをマウント出来るので、USBメモリやCDなどで設定する事が一般的かと思いますが、これをEBSに使います。
テストした環境はAmazon Linuxです。
1. マウントポイントの作成とインストール
$ sudo mkfs -t ext4 /dev/sdf $ sudo mkdir -p /data/ebs01 $ sudo yum install autofs
2. auto.masterとauto.dataの設定
$ sudo cp -R /etc/auto.master /etc/auto.master-orig $ sudo vi /etc/auto.master $ diff -u /etc/auto.master-orig /etc/auto.master --- /etc/auto.master-orig 2012-10-08 07:02:14.363356456 +0000 +++ /etc/auto.master 2012-10-08 11:02:40.980072067 +0000 @@ -4,13 +4,13 @@ # key [ -mount-options-separated-by-comma ] location # For details of the format look at autofs(5). # -/misc /etc/auto.misc +#/misc /etc/auto.misc # # NOTE: mounts done from a hosts map will be mounted with the # "nosuid" and "nodev" options unless the "suid" and "dev" # options are explicitly given. # -/net -hosts +#/net -hosts # # Include central master map if it can be found using # nsswitch sources. @@ -20,4 +20,5 @@ # same will not be seen as the first read key seen takes # precedence. # +/data /etc/auto.data --timeout=0 +auto.master $ sudo vi /etc/auto.data $ cat /etc/auto.data ebs01 -fstype=ext4 :/dev/xvdf
3. nsswitch.confに不要な設定があるので修正
$ sudo cp -R /etc/nsswitch.conf /etc/nsswitch.conf-orig $ sudo vi /etc/nsswitch.conf $ diff -u /etc/nsswitch.conf-orig /etc/nsswitch.conf --- /etc/nsswitch.conf-orig 2012-10-08 09:25:33.425332794 +0000 +++ /etc/nsswitch.conf 2012-10-08 09:25:42.051371368 +0000 @@ -58,6 +58,6 @@ publickey: nisplus -automount: files nisplus +automount: files aliases: files nisplus
4. 起動と自動起動の設定
$ sudo service autofs start Loading autofs4: [ OK ] Starting automount: [ OK ] $ sudo chkconfig autofs on
これでEBSのマウントポイント以下にアクセスされたタイミングで自動的にマウントされます。
auto.masterの設定で--timeoutを0(アンマウントしない)にしたのはアンマウントする必要がなさそうだからです。
もちろん、その後の運用でインスタンスを再起動することもあるかと思いますので、fstabなどもしっかりと設定しておいた方が良いと思います。