在EC2中使用EBS作为永久存储

Amazon’s Web Services 的EC2是个好东西,价格也不贵,因为是按照实际使用时间收费,所以我就考虑用这样一个东西来作为一个备份服务器。

但是如果一旦你重启EC2,可能就会导致数据丢失,如果要永久保存一些数据,那么就需要额外添加一个EBS,我们这里可以完全将其理解成磁盘。

一旦磁盘创建,就可以随便挂载到你的任何一个EC2上,所以ebs更相当于一个移动硬盘。

1. Create a volume创建一个volume

  • 登录到aws管理,进入EC2管理页面,然后按照该菜单创建一个卷标 “Elastic Block Store > Volumes”
  • 点击 “Create Volume”.
  • 设置volume的大小,记住,这个和s3不一样,amazon会按照你创建的大小来进行收费,即使你没有使用到那么多存储空间,所以我这里建议你创建小一点,比如5G或者10G,今后不够用了,再创建一个大一点的即可。
  • 添加 tags,这个无所谓了.

2. 右键点击volume,然后将其attach到你的EC2上去

3. Format the volume.格式化处理

登录到你的ec2中

1
2
mkfs.ext3 /dev/sdf # /dev/sdf你在attach的时候,可以设定该磁盘的路径
Mount points are available at /dev/sdf through /dev/sdp.

4. Mount the volume挂载驱动器

1
2
sudo mkdir /mnt/data  
sudo mount -t ext3 /dev/sdf /mnt/data

If you are potentially going to be dealing with many versions of data overtime, you might want to version your mount points. This will allow you to attach multiple EBS volumes at different sensible directories:

1
2
sudo mkdir /mnt/data-v0.2  
sudo mount -t ext3 /dev/sdf /mnt/data-v0.2

Alternatively, you might consider handle versioning when creating snapshots of your volume.

5. Set the EBS volume to mount automatically (optional) 自动挂载,这样每次重启的时候就不需要手动挂载驱动器了

1
2
sudo emacs /etc/fstab  
/dev/sdh /mnt/data ext3 defaults 0 0