EC2インスタンスリージョン間お引越しガイド
EC2インスタンスをリージョンを超えて引っ越しする方法についてまとめてみた。
概念図
引っ越し元から引っ越し先へrootボリュームをコピーします。コピーの際に、引っ越し専用のEC2インスタンスを使います(送受信)。
準備編
AMIの状態を確認する。特にi386 or x86_64, /dev/sda1 の snapshot IDとサイズ。
$ ec2-describe-images ami-114d1d54 IMAGE ami-114d1d54 965789077213/hikkoshi 965789077213 available private i386 machine aki-873667c2 ari-853667c0 ebs paravirtual BLOCKDEVICEMAPPING /dev/sda1 snap-2de9b546 10
引っ越し用のインスタンスを立てる
Linuxであればほぼなんでもよいが、今回はRightscaleのCentOS EBS AMIを利用。us-west-1からap-southeast-1への移行を想定。
まずは専用のsshキーペアを用意します。
$ ssh-keygen -N "" -t rsa -b 4096 -f hikkoshi.pem $ chmod 600 hikkoshi.pem $ ec2-import-keypair --region us-west-1 -f hikkoshi.pem.pub hikkoshi KEYPAIR hikkoshi 95:70:6f:60:cb:e8:1d:26:8b:b6:e1:bb:4e:54:a5:03 $ ec2-import-keypair --region ap-southeast-1 -f hikkoshi.pem.pub hikkoshi KEYPAIR hikkoshi 95:70:6f:60:cb:e8:1d:26:8b:b6:e1:bb:4e:54:a5:03
次にSecurity Groupを作りsshを開けます。
$ ec2-add-group hikkoshi -d "security group for hikkoshi" --region us-west-1 GROUP hikkoshi security group for hikkoshi $ ec2-authorize hikkoshi -P tcp -p 22 --region us-west-1 GROUP hikkoshi PERMISSION hikkoshi ALLOWS tcp 22 22 FROM CIDR 0.0.0.0/0 $ ec2-add-group hikkoshi -d "security group for hikkoshi" --region ap-southeast-1 GROUP hikkoshi security group for hikkoshi $ ec2-authorize hikkoshi -P tcp -p 22 --region ap-southeast-1 GROUP hikkoshi PERMISSION hikkoshi ALLOWS tcp 22 22 FROM CIDR 0.0.0.0/0
そして送受信用のEC2インスタンスを起動しますが、ここで気を付けるのはBLOCKDEVICEの指定です。
送信側には、今回引っ越ししたいAMIのrootデバイスのsnapshotを指定してEBSボリュームを/dev/sdpにattachしています。
対して受信側には、同容量以上の空EBSボリュームを作成します(サイズを広げたい場合には、ここで大き目を指定)。
$ ec2-run-instances -g hikkoshi -k hikkoshi --region us-west-1 -b '/dev/sdp=snap-413f7d2a:10' ami-25df8e60 $ ec2-run-instances -g hikkoshi -k hikkoshi --region ap-southeast-1 -b '/dev/sdp=:10' ami-21c2bd73
インスタンスが起動したら、IPアドレスを確認して、手元(local)→引っ越し元(src)、引っ越し元(src)→引っ越し先(dst)、手元(local)→引っ越し先(dst)のssh接続を確認。
local $ ssh -i hikkoshi.pem root@<SRC_IP> local $ ssh -i hikkoshi.pem root@<DST_IP> local $ scp -i hikkoshi.pem hikkoshi.pem root@<SRC_IP>:. #秘密鍵を送信側にコピーします --- src # ssh -i hikkoshi.pem root@<DST_IP>
そして、各ブロックデバイスごとに中身を転送するのですが、ここで1点だけ注意事項があります。rootボリュームの/root/.ssh/authorized_keysやssh_host_keyを消しておく事です。元リージョンで使っていた同じキーでアクセスできるようにあえて残すのであれば問題ありませんが、セキュリティリスクを考慮してなるべく消しておいたほうがいいでしょう。
src # mount /dev/sdp /mnt src # rm /mnt/root/.ssh/authorized_keys /mnt/etc/ssh/*key{,.pub} src # umount /mnt
さて、それでは転送準備ができましたので、実際にディスクの中身を転送します。fdiskやmkfsをしなくてもいいように、dd|gzip over sshで持っていきます。もしサイズを変えたい場合には、転送先であらかじめfdiskやmkfsを行っておいてマウントした後にrsync over sshが適切でしょう。
src # time dd if=/dev/sdp | gzip -c1 | ssh -i hikkoshi.pem root@<DST_IP> "gzip -dc > /dev/sdp" 10240+0 records in 10240+0 records out 10737418240 bytes (11 GB) copied, 485.199 seconds, 22.1 MB/s real 8m7.490s user 1m29.602s sys 0m11.853s --- dst # mount /dev/sdp /mnt dst # ls /mnt bin boot dev etc home lib lost+found misc mnt opt proc root sbin selinux srv sys tmp usr var
10GBのvolumeの転送にかかった時間は約8分でした(圧縮が効いているので、約800MB程度の転送が行われました)。
次にこのvolumeのsnapshotを作成します。ec2-describe-instancesで引っ越し先インスタンスの情報からどのvolumeが使われているか確認し、snapshotを作ります。
$ ec2-describe-instances i-7e928e2c | grep /dev/sdp BLOCKDEVICE /dev/sdp vol-6fed9c04 2010-12-15T10:06:20.000Z $ ec2-create-snapshot vol-6fed9c04
snapshotを作り終えたら、次はいよいよAMIの登録となります。元々のAMIがAKI/ARIを指定していた場合には注意が必要です。
まずは元のリージョンでAKI、ARIそれぞれの情報を確認します。
$ ec2-describe-images aki-873667c2 --region us-west-1 IMAGE aki-873667c2 ec2-public-images-us-west-1/ec2-vmlinuz-2.6.21.7-2.fc8xen.i386.manifest.xml amazon available public i386 kernel instance-store paravirtual $ ec2-describe-images ari-853667c0 --region us-west-1 IMAGE ari-853667c0 ec2-public-images-us-west-1/ec2-initrd-2.6.21.7-2.fc8xen.i386.manifest.xml amazon available public i386 ramdisk instance-store paravirtual
引っ越し先のリージョンで ec2-.*-2.6.21.7-2.fc8xen.i386.manifest.xml を手掛かりに探します。見つかったら、それぞれのIDをメモしておきます。
$ ec2-describe-images -a --region ap-southeast-1 | grep ec2-.*-2.6.21.7-2.fc8xen.i386.manifest.xml IMAGE ari-37f58a65 ec2-public-images-ap-southeast-1/ec2-initrd-2.6.21.7-2.fc8xen.i386.manifest.xml amazon available public i386 ramdisk instance-store paravirtual IMAGE aki-15f58a47 ec2-public-images-ap-southeast-1/ec2-vmlinuz-2.6.21.7-2.fc8xen.i386.manifest.xml amazon available public i386 kernel instance-store paravirtual
いよいよAMIの登録です。
$ ec2-register --region ap-southeast-1 -a i386 -b "/dev/sdb=ephemeral0" -d "hikkoshi test" -n "hikkoshi test" -s snap-20297c48 --root-device-name /dev/sda1 --kernel aki-15f58a47 --ramdisk ari-37f58a65 IMAGE ami-86552bd4
AMIが登録されました!早速起動してみましょう。
$ ec2-run-instances --region ap-southeast-1 -g hikkoshi -k hikkoshi ami-86552bd4 INSTANCE i-a28e92f0 ami-86552bd4 pending hikkoshi 0 m1.small 2010-12-15T11:10:59+0000 ap-southeast-1b aki-15f58a47 ari-37f58a65 monitoring-disabled ebs paravirtual $ ec2-describe-instances i-a28e92f0 RESERVATION r-0c485a5e 965789077213 hikkoshi INSTANCE i-a28e92f0 ami-86552bd4 ec2-175-41-165-104.ap-southeast-1.compute.amazonaws.com ip-10-130-33-158.ap-southeast-1.compute.internal running hikkoshi0 m1.small 2010-12-15T11:10:59+0000 ap-southeast-1b aki-15f58a47 ari-37f58a65 monitoring-disabled 175.41.165.104 10.130.33.158 ebs paravirtual BLOCKDEVICE /dev/sda1 vol-33c0b158 2010-12-15T11:11:27.000Z $ ssh -i hikkoshi.pem root@ec2-175-41-165-104.ap-southeast-1.compute.amazonaws.com The authenticity of host 'ec2-175-41-165-104.ap-southeast-1.compute.amazonaws.com (175.41.165.104)' can't be established. RSA key fingerprint is 1f:fe:8b:99:63:62:57:92:c8:c8:ca:d8:1a:a7:b6:56. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'ec2-175-41-165-104.ap-southeast-1.compute.amazonaws.com,175.41.165.104' (RSA) to the list of known hosts. Last login: Wed Dec 15 04:47:41 2010 from xxx.xxx.xxx.xxx ___ _ __ __ ____ __ / _ \ (_)___ _ / / / /_ / __/____ ___ _ / /___ / , _// // _ `// _ \/ __/_\ \ / __// _ `// // -_) /_/|_|/_/ \_, //_//_/\__//___/ \__/ \_,_//_/ \__/ /___/ Welcome to a managed virtual machine brought to you by RightScale! ******************************************************************** ******************************************************************** *** Your instance is now operational. *** *** All of the configuration has completed. *** *** Please check /var/log/messages for details. *** ******************************************************************** ********************************************************************
で、こういった手順を自動化するツールを作ろうかなーなんて思っていた矢先、AWSのより使いやすい管理コンソール「Cloudworks」をサービス提供されいている株式会社サーバーワークスさんのブログに、こんな記事が載っていて、詳細をお伺いした所、これはとてもかなわないという事であっさりと開発中止しましたw
AKI/ARIの判定機能あたりはものすごく便利に作ってあるなぁという印象です。リージョン引っ越しをお考えの際には、是非お試しください。