Amazon EC2之初体验(转)
From:http://www.cnblogs.com/nckiki/archive/2007/08/21/864249.html
From:http://www.cnblogs.com/nckiki/archive/2007/08/21/864251.html
Amazon EC2之初体验
由于项目的需要,这几个月都在使用Amazon的EC2。这个东西对于国内的用户来说比较陌生,但是听国外的同事说,这个在美国可是相当流行。这里简单介绍一下,大家有兴趣也可以试试。
EC2全称Amazon Elastic Compute Cloud,是Amazon于2006年推出的新一代hosting服务,目前尚处于beta阶段,但是使用中没有发现什么问题,相当稳定。
EC2的功能介绍:http://www.amazon.com/gp/browse.html?node=201590011
要用EC2,有两个概念必须先了解。
Amazon Machine Image (AMI) - An Amazon Machine Image (AMI) is an encrypted file stored in Amazon S3. It contains all the information necessary to boot instances of your software.
Instance - The running system based on an AMI is referred to as an instance. All instances based on the same AMI begin executing identically. Any information on them is lost when the instances are terminated or if they fail.
对EC2有个大概了解之后,就可以开始在网上申请使用此项服务了。
- 1. 首先必须先在Amazon Web Services上注册一个用户http://www.amazon.com/gp/browse.html?node=3435361
- 2. 注册完毕后,回到Amazon Web Services页面,点击左边Browse Web Services中的Amazon Elastic Compute Cloud (Beta),然后在EC2页面中,点击右边的Sign Up For This Web Service。这时系统会显示一个收费列表(如下)及要求你输入你的支付方式。
Instances
$0.10 per instance-hour consumed (or part of an hour consumed)
Data Transfer
$0.10 per GB - all data transfer in
$0.18 per GB - first 10 TB / month data transfer out
$0.16 per GB - next 40 TB / month data transfer out
$0.13 per GB - data transfer out / month over 50 TB
可以看到EC2完全按你的需要收费。如果需要多个实例,就可以多开几个,不需要了就关了,没有传统hosting的固定合同。
里面还提到Amazon的另一个服务Amazon Simple Storage Service ,简称Amazon S3,主要用于存贮数据。EC2是不负责储存的,每次instance关掉重启除/mnt外所有数据还原成VM镜像的初始状态,/mnt则被清空。储存由S3 (Amazon Simple Storage Service)服务负责。可以看看我的帐单。
Amazon EC2之初体验(二)
运行实例
- 1. 运行‘C:\EC2\bin>ec2-describe-images -o self -o amazon’ 来查看所有公共的镜像
C:\EC2\bin>ec2-describe-images -o self -o amazon
IMAGE ami-20b65349 ec2-public-images/fedora-core4-base.manifest.xml
amazon available public
IMAGE ami-22b6534b ec2-public-images/fedora-core4-mysql.manifest.xml
amazon available public
IMAGE ami-23b6534a ec2-public-images/fedora-core4-apache.manifest.xml
amazon available public
IMAGE ami-25b6534c ec2-public-images/fedora-core4-apache-mysql.manifest.xml
amazon available public
IMAGE ami-26b6534f ec2-public-images/developer-image.manifest.xml amazon
available public
IMAGE ami-2bb65342 ec2-public-images/getting-started.manifest.xml amazon
available public
IMAGE ami-bd9d78d4 ec2-public-images/demo-paid-AMI.manifest.xml amazon
available public A79EC0DB
或者到这个页面选择一个适合的AMI:
http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=101
- 2. 运行‘C:\EC2\bin>ec2-add-keypair kiki-keypair’,然后创建一个名为 id_rsa-kiki-keypair的文件,粘贴从-----BEGIN RSA PRIVATE KEY----到-----END RSA PRIVATE KEY-----之间(包括它们本身)所有的内容。特别注意最后一行的换行。
-----BEGIN RSA PRIVATE KEY-----
MIIEoQIBAAKCAQBuLFg5ujHrtm1jnutSuoO8Xe56LlT+HM8v/xkaa39EstM3/aFxTHgElQiJLChp
HungXQ29VTc8rc1bW0lkdi23OH5eqkMHGhvEwqa0HWASUMll4o3o/IX+0f2UcPoKCOVUR+jx71Sg
5AU52EQfanIn3ZQ8lFW7Edp5a3q4DhjGlUKToHVbicL5E+g45zfB95wIyywWZfeW/UUF3LpGZyq/
ebIUlq1qTbHkLbCC2r7RTn8vpQWp47BGVYGtGSBMpTRP5hnbzzuqj3itkiLHjU39S2sJCJ0TrJx5
i8BygR4s3mHKBj8l+ePQxG1kGbF6R4yg6sECmXn17MRQVXODNHZbAgMBAAECggEAY1tsiUsIwDl5
91CXirkYGuVfLyLflXenxfI50mDFms/mumTqloHO7tr0oriHDR5K7wMcY/YY5YkcXNo7mvUVD1pM
ZNUJs7rw9gZRTrf7LylaJ58kOcyajw8TsC4e4LPbFaHwS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hyERbDjUIdHaK3M849JJuf8cSrvSb4g==
-----END RSA PRIVATE KEY-----
- 3. 运行是实例。这个过程需要几分钟的时间
C:\EC2\bin>ec2-run-instances ami-25b6534c -k kiki-keypair
RESERVATION r-3e27c657 621657444030 default
INSTANCE i-c3f31eaa ami-25b6534c pending kiki-keypair 0
- 4. 通过以下命令查看状态
C:\EC2\bin>ec2-describe-instances i-c3f31eaa
RESERVATION r-3e27c657 621657444030 default
INSTANCE i-c3f31eaa ami-25b6534c ec2-72-44-51-222.z-1.compute-1.amazonaws.com domU-12-31-36-00-30-74.z-1.compute-1.internal running kiki-keypair 0
- 5. 给你的实例授以网络访问的权限。
C:\EC2\bin>ec2-authorize default -p 22
PERMISSION default ALLOWS tcp 22 22 FROM CIDR 0.0.0.0/0
C:\EC2\bin>ec2-authorize default -p 80
PERMISSION default ALLOWS tcp 80 80 FROM CIDR 0.0.0.0/0
C:\EC2\bin>ec2-authorize default -p 21
PERMISSION default ALLOWS tcp 80 21 FROM CIDR 0.0.0.0/0
- 6. 用PuTTY连接你的实例
- 7. 通过浏览器查看你的实例.Http://ec2-72-44-51-222.z-1.compute-1.amazonaws.com
- 8. 如果需要,安装VSFTPD
yum –y install vsftpd
passwd root
/sbin/service vsftpd start
创建一个镜像
- 9. 在实例中,下载ec2-ami-tools然后安装
bash-3.2# wget http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools.noarch.rpm.
rpm -Uvh ec2-ami-tools.noarch.rpm
注意:在有些实例中,你需要安装Ruby。
10. 复制private key和509 certificate文件于/mnt下
11. 运行
bash-3.2#ec2-bundle-vol -d /mnt -k /mnt/privatekey.pem -c /mnt/509certificate.pem –u < AWS account ID >
注意:account ID可以从Amazon网上的Account Activity地方得到。它显示在页面的左上方,格式象9999-9999-9999。不过要移除中间的连接符只留下12位数字。
12. 运行ls -l /mnt/image.* 查看生成的文件
13. 运行如下命令上传AMI到Amazon的S3上
bash-3.2#ec2-upload-bundle -b <your-s3-bucket> -m /mnt/image.manifest.xml -a <aws-access-key-id> -s <aws-secret-access-key>
14. 运行如下命令以注册你的AMI
C:\EC2\bin>ec2-register <your-s3-bucket>/image.manifest.xml
15. 运行如下命令产生一个实例基于你的AMI
C:\EC2\bin>ec2-run-instances ami-5bae4b32
Cleaning Up
16. 运行如下命令注销AMI
C:\EC2\bin>ec2-deregister <your-s3-bucket>
17. 运行如下命令从Amazon S3上移除你的AMI
bash-3.2#ec2-delete-bundle -b <your-s3-bucket> -p image -a <aws-access-key-id> -s <aws-secret-access-key>
18. 中止你的实例。一旦你的实例启动后,你就必须为它消耗掉的资源付钱。如果你决定不再使用它了,最好中止掉它
C:\EC2\bin>ec2-terminate-instances i-c3f31eaa
INSTANCE i-10a64379 running shutting-down
或直接关闭你的实例
bash-3.2#shutdown –h now
相关连接
-
l Amazon EC2 Getting Started Guide (API Version 2007-03-01)
http://docs.amazonwebservices.com/AWSEC2/2007-03-01/GettingStartedGuide/
-
l Setting up and Running Amazon EC2 from Windows