close

為什麼要弄這篇文章 其實主要就是因為我們的AMI有移轉的需求
要從新加坡移轉到美東去 以準備未來上線使用
其實接觸Amazon到現在 我們都知道一件很重要的事
就是region間並沒有任何共享資源可以用
所以當我們在新加坡建的ami 在美東是沒辦法看到的

因此當我想要準備把環境從新加坡搬到美東去 我就得找出對應的方式

1.花錢買第三方管理工具
2.重建環境
3.用dd建一個EBS的備份再轉過去

想了我就累
後來大概花了一個小時在找解法
總算讓我找到個工具
migrate-ebs-image.pl (只支援ebs base的unix系統)
http://search.cpan.org/~lds/VM-EC2/bin/migrate-ebs-image.pl

這個工具運作的方式大概是這樣
在來源region與目標region各開一台EC2 instance
來源region 會把ami檔的EBS 掛起來 再rsync到目標區
完成之後就快照再註冊成ami

資源:
需要能運作perl的主機
我是自己原本就有台CENTOS的vm可以來處理

安裝:
1.需先安裝perl的環境
yum install perl*
2.perl -MCPAN -e 'install VM::EC2'  #安裝VM::EC2
中間可能會遇到問題
(1)ExtUtils::MakeMaker version 6.31 required--this is only version 6.30 at Makefile.PL line 7.
root@localhost build]# cpan ExtUtils::MakeMaker #升級xtUtils::MakeMaker
(2) 找不到/root/.cpan/build/VM-EC2-1.19
root@localhost build]#mkdir /root/.cpan/build/VM-EC2-1.19   #手動建VM-EC2-1.19
(3)OSTYPE.pm 找不到
安裝epel來源
rpm -ivh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm #以5為例
#http://dl.fedoraproject.org/pub/epel/6/ 6的來源
vi /etc/yum.repos.d/epel.repo
[epel] [epel-debuginfo] [epel-source] 的enabled=0 都改為1
yum install perl-Perl-OSType
3.安裝過程會提示要輸入: EC2_ACCESS_KEY, EC2_SECRET_KEY
我後來是直接enter略過了(因為一直出錯)
然後他就會開始安裝 
Installing /usr/share/man/man3/VM::EC2::BlockDevice.3pm
Installing /usr/share/man/man3/VM::EC2::Instance::StatusItem.3pm
Installing /usr/share/man/man3/VM::EC2::Instance::State.3pm
Installing /usr/share/man/man3/VM::EC2::Instance::IamProfile.3pm
Installing /usr/share/man/man3/VM::EC2::VPC::VpnGateway.3pm
Installing /usr/share/man/man3/VM::EC2::ELB::Policies::LBCookieStickinessPolicy.3pm
Installing /usr/bin/sync_to_snapshot.pl
Installing /usr/bin/migrate-ebs-image.pl
/usr/bin/make install  -- OK
這樣就安裝成功
4.vi /usr/bin/migrate-ebs-image.pl
接著在131跟132增加下面兩行手動輸入EC2_ACCESS_KEY, EC2_SECRET_KEY
131 $Access_key ="XXXXXXXXXXXXXXXX";
132 $Secret_key ="XXXXXXXXXXXXXXXX";
5.移轉ami指令
migrate-ebs-image.pl --from "來源region" --to "目標region" "AMIid"
例如我從新加坡搬到美東
migrate-ebs-image.pl --from ap-southeast-1 --to us-east-1 ami-88gsygyg 
6.接下來就會開始自動進行跟進度說明
[info] Scanning for existing staging servers and volumes in http://ec2.ap-southeast-1.amazonaws.com.
[info] Scanning for existing staging servers and volumes in http://ec2.us-east-1.amazonaws.com.
[info]    Gathering information about image ami-88fbbbda.
[info]    Copying EBS volumes attached to this image (this may take a long time).
[info]   Provisioning volume from snapshot snap-6effe0e4b.
[info]   Obtaining a staging server.
[info]    Creating keypair staging-key-374eeea6.
[info]    Creating staging security group ssh-ad5edr3.
[info]    Searching for a staging image...
[info]    ...found ami-608acd32: ubuntu/images/ubuntu-maverick-10.10-i386-server-20120410.
[info]    Waiting for ssh daemon on i-b6647de2.
[info]   Using snapshot snap-6f020e4b...
[info]   Mounting staging volume at /dev/sdf1 on /mnt/Staging/volume-6b3baee2.
[info]   Provisioning a new 10 GB ext4 volume.
[info]   Obtaining a staging server.
[info]    Creating keypair staging-key-28926fef29.
[info]    Creating staging security group ssh-7b3efef9af50.
[info]    Searching for a staging image...
[info]    ...found ami-7b8f5712: ubuntu/images/ubuntu-maverick-10.10-i386-server-20120410.
[info]    Waiting for ssh daemon on i-75039709.
[info]   Provisioning a new 10 GB volume...
[info]   Making ext4 filesystem on staging volume...
[info]   Mounting staging volume at /dev/sdf1 on /mnt/Staging/volume-d522931d.
[info] creating ssh key for server to server data transfer.
[info]   Beginning rsync /mnt/Staging/volume-6b3baee2/ ec2-23-22-163-223.compute-1.amazonaws.com:/mnt/Staging/volume-d522931d/...
[info]   ...rsync done.
[info]    unmounting vol-e24d0efe99f...
[info]    snapshotting vol-e24efed099f
[info]    unmounting vol-2c117efed08...
[info]    deleting vol-2c1fef17d08...
[info]    deleting vol-e24efwd099f...
[info]    Waiting for all snapshots to complete. This may take a long time.
[info]    Registering snapshot in destination with the equivalent of:
[info]    ec2-register -n 'XDDDDDD2.0' -d 'XDDDDDD2.0' -a x86_64 --kernel aki-88aa75e1 --ramdisk '' --root-device-name /dev/sda1 -b /dev/sda1=snap-5f1e9729:10:true:standard
[info]    Terminating servers i-b6647de2.
[info]    Terminating servers i-75039709.
New snapshot is now located in us-east-1 under ami-062a926f.
7.移轉完成

我想這個作法是目前比較輕鬆可以進行移轉的方式
就把我實作的方式跟大家分享了
有相同的需求都可以參考看看

參考文章
https://forums.aws.amazon.com/thread.jspa?threadID=104733
http://search.cpan.org/~lds/VM-EC2/bin/migrate-ebs-image.pl
http://blog.ibd.com/scalable-deployment/copy-an-ebs-ami-image-to-another-amazon-ec2-region/
https://forums.aws.amazon.com/thread.jspa?messageID=249804
http://alestic.com/2010/10/ec2-ami-copy





arrow
arrow
    全站熱搜

    紐士比 發表在 痞客邦 留言(0) 人氣()