認証
EC2のWEBにログインするための情報
ID:ec2@kapi.jp(取得したメアド)
PASS:パスワード
EC2のManagerコンソールにログインするための情報
Your Access Key ID:
K****A*******P******P
Your Secret Access Key:
K********A*********P*****************I
サーバ共通で使うX.509キー
まずはEC2管理用のキーを作成しないとならない。このキーは最重要なので大切に保管しとく。
http://aws.amazon.com/
を開き右上メニューの
Your Account > Access Identifiers
から作成。
X.509 Certificate の項目の
Create Newを進めれば公開鍵、秘密鍵が出来上がる。
もし、紛失してしまった場合は、同じ手順で再発行は出来る。
※しかし、既存のサーバで実行したimageとかにはアクセス出来なくなる。(?)(多分・・・それはうそかも(-_-;))
めったに変更しないこと。
公開鍵:cert-**********************.pem
秘密鍵:pk-************************.pem
管理client側の用意
linuxでamazon-ec2-commandを使う。
ec2-api-toolsをダウンロードしてくる。
解凍して適当なとこに設置。
mv /home/kapi/ec2-api-tools /opt/apps/
x509キーを置く。
>ls /home/kapi/
cert-**********************.pem
pk-************************.pem
>cp /home/kapi/*.pem /opt/apps/cert/
pathを通す。
/etc/profile等に書いとく。
export JAVA_HOME=/opt/apps/java
export EC2_HOME=/opt/apps/ec2-api-tools
export PATH=$PATH:$JAVA_HOME/bin:$EC2_HOME/bin/
export EC2_PRIVATE_KEY=/opt/apps/ec2-api-tools/cert/pk-************************.pem
export EC2_CERT=/opt/apps/ec2-api-tools/cert/cert-**********************.pem
Instanceの作成
Instance追加初期状態ではtimezoneが日本時間になってないので以下を実行
> cp /usr/share/zoneinfo/Japan /etc/localtime
cp: overwrite `/etc/localtime'? y
>
Instanceが消えても復元できるようにs3にイメージ化しておく。
まずはscpでInstanceにx509キーを転送
>scp -i /home/kapi/ec2_work/west/ec2_instance_key.pem /opt/apps/ec2-api-tools/cert/*.pem root@ec2-***-***-***-***.euwest-1.compute.amazonaws.com:/mnt
cert-**********************.pem 100% 916 0.9KB/s 00:00
pk-************************.pem 100% 926 0.9KB/s 00:00
イメージ化する
EC2のInstance上で
>cd /mnt
>ec2-bundle-vol -d /mnt --privatekey pk-************************.pem --cert cert-**********************.pem --user ****-****-****
を実行
Please specify a value for arch [i386]:【enter】
Copying / into the image file /mnt/image...
Excluding:
/sys
/proc
/sys/fs/fuse/connections
/dev/pts
/proc/sys/fs/binfmt_misc
/dev
/media
/mnt
/proc
/sys
/mnt/image
/mnt/img-mnt
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.001838 s, 570 MB/s
mke2fs 1.40.4 (31-Dec-2007)
...
...
...
終わると
-rw-r--r-- 1 root root 10737418240 Jul 12 06:34 image
-rw-r--r-- 1 root root 7205 Jul 12 06:41 image.manifest.xml
-rw-r--r-- 1 root root 10485760 Jul 12 06:40 image.part.00
-rw-r--r-- 1 root root 10485760 Jul 12 06:40 image.part.01
-rw-r--r-- 1 root root 10485760 Jul 12 06:40 image.part.02
-rw-r--r-- 1 root root 10485760 Jul 12 06:40 image.part.03
-rw-r--r-- 1 root root 10485760 Jul 12 06:40 image.part.04
-rw-r--r-- 1 root root 10485760 Jul 12 06:40 image.part.05
のようなファイルが出来上がる。
S3に転送
ec2-upload-bundleでS3に転送する。
> ec2-upload-bundle --bucket /my/s3/bucket/ --manifest image.manifest.xml --access-key K****A*******P******P --secret-key K********A*********P*****************I
You are bundling in one region, but uploading to another. If the kernel
or ramdisk associated with this AMI are not in the target region, AMI
registration will fail.
You can use the ec2-migrate-manifest tool to update your manifest file
with a kernel and ramdisk that exist in the target region.
Are you sure you want to continue? [y/N]y
Uploading bundled image parts to the S3 bucket kapi ...
Uploaded image.part.00
Uploaded image.part.01
Uploaded image.part.02
Uploaded image.part.03
...
...
Uploaded manifest.
Bundle upload completed.
>
イメージを登録
Instanceではなく、管理端末から
> ec2-register my/s3/bucket/image.manifest.xml
IMAGE ami-******
と出たら成功。
自分の登録したイメージはコマンドから
>ec2-describe-images -o self
とやるか、管理ツールで見れる。(Account Numberとかで検索すると直ぐ出る)
トラブルシューティング
■Client.InvalidSecurity: Request has expired
Client.InvalidSecurity: Request has expired
ローカルマシンの時間(時刻)がおかしくなっている
http://d.hatena.ne.jp/rx7/20090323/p1を見よ
■check your manifest path is correct and in the correct region.
>ec2-register --region eu-west-1 my/s3/bucket/image.manifest.xml
Client.InvalidManifest: HTTP 301 (Moved Permanently) response for URL http://my.s3.amazonaws.com:80/my/s3/bucket/image.manifest.xml: check your manifest path is correct and in the correct region.
x509の認証キーとSSHが食い違ってる?
Instance追加後にx509のキーを作り直した?
やってみた感じだと、x509のキーを作り直すと、既存のキーで作ったInstanceのイメージ化に失敗した。
■ec2-register実行時のオプション
ec2-register --debug --region eu-west-1 eu-codexami/image.manifest.xml
等で詳細を見れる。
■特定ポートにアクセスできない。
AWSManagementConsoleからSecurity Groupsで開放する。
■Client.InvalidVolume.ZoneMismatch: The volume 'vol-****' is not in the same availability zone as instance 'i-****'
稼動ロケーションとEC2インスタンスにAttachするEBSボリュームが一致していない。
※説明に出てくるディレクトリやパスやキーの名前等は例です。
参考URL
http://kapi.jp/kapi_blog/247
2009年07月24日
関連カテゴリ Linux Unix