Back to Amazon Web Services

See Also Amazon Machine ImagesAmazon Elastic Compute CloudAWS Command Line Interface

Amazon Elastic Block Store

Amazon Elastic Block Store(Amazon EBS),提供用于Amazon EC2实例的持久性数据块级存储卷。每个Amazon EBS卷在其可用区内自动复制,以保护用户免于组件故障的威胁,同时提供高可用性和持久性。Amazon EBS卷为用户提供运行工作负载所需的一致性和低延迟性能。Amazon EBS可在几分钟内调整用量,同时以低价仅为调配的资源支付费用。

1. 基础知识

1.1. 定价

http://aws.amazon.com/ebs/pricing/

1.1.1. Singapore

Amazon EBS General Purpose SSD (gp2) volumes

$0.12 per GB-month of provisioned storage

1.2. 入门指南

Amazon EBS 卷需在指定的可用区域内创建,容量大小需在 1 GB 到 1 TB 之间。当一个卷创建完成后,此卷可以与位于同一可用区域内的任何 Amazon EC2 实例相连接。与实例相连接后,卷将显示为已安装设备,类似于任何硬盘或其他块存储设备。此时,就如与本地驱动装置相互作用一样,实例可以与卷进行相互作用,并可以通过一个文件系统对其进行格式化,或直接为其安装应用程序。

Amazon EBS 卷也可以对 Amazon EC2 实例进行引导分区,您可将引导分区的大小提高到最大 1 TB,并可保存引导分区数据超出实例生命周期之久,并可单击一次捆绑您的 AMI。您还可以关闭和重新启动由 Amazon EBS 卷启动的实例,同时以在快速启动时间内保存状态。

2. API 参考

CLI EBS相关

2.1. Getting Started

2.1.1. Launch EC2 first

(pythonenv)➜  aws_cli  aws ec2 run-instances --image-id ami-41a7ea71 --count 1 --instance-type t2.micro --key-name liyan --security-groups default --placement AvailabilityZone=us-west-2c
{
    "OwnerId": "995517354230", 
    "ReservationId": "r-eedd42e1", 
    "Groups": [], 
    "Instances": [
        {
            "Monitoring": {
                "State": "disabled"
            }, 
            "PublicDnsName": null, 
            "RootDeviceType": "ebs", 
            "State": {
                "Code": 0, 
                "Name": "pending"
            }, 
            "EbsOptimized": false, 
            "LaunchTime": "2014-12-15T02:32:12.000Z", 
            "PrivateIpAddress": "172.31.5.111", 
            "ProductCodes": [], 
            "VpcId": "vpc-ee3fe28b", 
            "StateTransitionReason": null, 
            "InstanceId": "i-ef2bd6e1", 
            "ImageId": "ami-41a7ea71", 
            "PrivateDnsName": "ip-172-31-5-111.us-west-2.compute.internal", 
            "KeyName": "liyan", 
            "SecurityGroups": [
                {
                    "GroupName": "default", 
                    "GroupId": "sg-d32056b6"
                }
            ], 
            "ClientToken": null, 
            "SubnetId": "subnet-1369804a", 
            "InstanceType": "t2.micro", 
            "NetworkInterfaces": [
                {
                    "Status": "in-use", 
                    "MacAddress": "0a:77:1d:22:35:c9", 
                    "SourceDestCheck": true, 
                    "VpcId": "vpc-ee3fe28b", 
                    "Description": null, 
                    "NetworkInterfaceId": "eni-571c330e", 
                    "PrivateIpAddresses": [
                        {
                            "PrivateDnsName": "ip-172-31-5-111.us-west-2.compute.internal", 
                            "Primary": true, 
                            "PrivateIpAddress": "172.31.5.111"
                        }
                    ], 
                    "PrivateDnsName": "ip-172-31-5-111.us-west-2.compute.internal", 
                    "Attachment": {
                        "Status": "attaching", 
                        "DeviceIndex": 0, 
                        "DeleteOnTermination": true, 
                        "AttachmentId": "eni-attach-ea9f00e2", 
                        "AttachTime": "2014-12-15T02:32:12.000Z"
                    }, 
                    "Groups": [
                        {
                            "GroupName": "default", 
                            "GroupId": "sg-d32056b6"
                        }
                    ], 
                    "SubnetId": "subnet-1369804a", 
                    "OwnerId": "995517354230", 
                    "PrivateIpAddress": "172.31.5.111"
                }
            ], 
            "SourceDestCheck": true, 
            "Placement": {
                "Tenancy": "default", 
                "GroupName": null, 
                "AvailabilityZone": "us-west-2c"
            }, 
            "Hypervisor": "xen", 
            "BlockDeviceMappings": [], 
            "Architecture": "x86_64", 
            "StateReason": {
                "Message": "pending", 
                "Code": "pending"
            }, 
            "RootDeviceName": "/dev/xvda", 
            "VirtualizationType": "hvm", 
            "AmiLaunchIndex": 0
        }
    ]
}

2.1.2. Attach Volume to EC2

(pythonenv)➜  aws_cli  aws ec2 create-volume --size 1 --volume-type standard --availability-zone us-west-2c
{
    "AvailabilityZone": "us-west-2c", 
    "Encrypted": false, 
    "VolumeType": "standard", 
    "VolumeId": "vol-82ec4d8d", 
    "State": "creating", 
    "SnapshotId": null, 
    "CreateTime": "2014-12-12T10:09:12.242Z", 
    "Size": 1
}
(pythonenv)➜  aws_cli  fab ec2.tag:vol-82ec4d8d,liyan
[localhost] local: aws ec2 create-tags --resources vol-82ec4d8d --tags Key=Onwer,Value=liyan Key=Stack,Value=test
(pythonenv)➜  aws_cli  aws ec2 describe-volumes 

(pythonenv)➜  aws_cli  aws ec2 attach-volume --volume-id vol-82ec4d8d --instance-id i-ef2bd6e1 --device /dev/sdf
{
    "AttachTime": "2014-12-12T10:10:40.024Z", 
    "InstanceId": "i-176c9b19", 
    "VolumeId": "vol-82ec4d8d", 
    "State": "attaching", 
    "Device": "/dev/sdf"
}

2.1.3. EC2: mount the Volume

ssh -i /opt/app/ssh/hst-dev.aws/liyan.pem ec2-user@54.148.152.130
[ec2-user@ip-172-31-11-61 ~]$ lsblk
NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   2G  0 disk 
└─xvda1 202:1    0   2G  0 part /
xvdf    202:80   0   1G  0 disk 
[ec2-user@ip-172-31-11-61 ~]$ sudo mkdir /mnt/my-data
[ec2-user@ip-172-31-11-61 ~]$ sudo mount /dev/xvdf /mnt/my-data
mount: /dev/xvdf is write-protected, mounting read-only
mount: wrong fs type, bad option, bad superblock on /dev/xvdf,
       missing codepage or helper program, or other error

       In some cases useful info is found in syslog - try
       dmesg | tail or so.

[ec2-user@ip-172-31-11-61 ~]$ sudo mkfs -t ext4 /dev/xvdf
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
...
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: don

[ec2-user@ip-172-31-11-61 ~]$ sudo mount /dev/xvdf /mnt/my-data
[ec2-user@ip-172-31-11-61 ~]$ 

# fix issue "scp: command not found"
➜  /tmp  scp -i /opt/app/ssh/hst-dev.aws/liyan.pem image.tar.bz2 ec2-user@54.149.1.250:
bash: scp: command not found
lost connection
yum install openssh-clients
[ec2-user@ip-172-31-11-61 ~]$ sudo yum install openssh-clients
...
Installed:
  openssh-clients.x86_64 0:6.2p2-8.42.amzn1                                     

Complete!

2.1.4. EC2: dd in the Volume

# [ec2-user@ip-172-31-5-111 ~]$ sudo dd if=/mnt/my-data2/ec2.img of=/dev/xvdg bs=10M
[ec2-user@ip-172-31-5-111 ~]$ tar xf image.tar.bz2 -O |sudo dd bs=10M of=/dev/xvdi
0+66440 records in
0+66440 records out
5368709120 bytes (5.4 GB) copied, 292.122 s, 18.4 MB/s

3. Reference


CategoryAmazonWebServices

MainWiki: AWS_EBS (last edited 2014-01-13 00:07:02 by twotwo)