文档概述
在我们日常工作中,常常会遇到AWS EC2实例存储卷空间不够用,需要扩容存储卷、在线添加存储卷或由于工作负载变化,需要调整存储卷类型的情况。上述操作会对业务有影响吗?答案是不会的,AWS支持在线存储卷扩容,在线添加存储卷,在线修改存储卷类型且不需要重启EC2实例。
以下文章描述如何完成上述操作,参考AWS官方文档:https://docs.amazonaws.cn/AWSEC2/latest/UserGuide/ebs-modify-volume.html
操作背景
环境中有一台AWS Linux EC2实例,根存储卷大小为默认8GB 通用型SSD存储,IOPS为100。登录EC2实例,使用 df -h 命令查看存储使用情况,可看到默认8GB存储,已使用空间为16%。
一、在线添加EBS存储卷
打开AWS控制台,创建一个2GB大小的通用型SSD存储卷,将新创建的EBS存储卷附加至测试的Linux实例。可在控制台中看到增加了一块EBS存储卷。
进入系统,格式化并挂载EBS存储卷,输入 lsblk 查看存储卷设备。
创建文件系统,并挂载EBS存储卷,输入 file -s /dev/xvda1,可看到xvda1根据的文件系统是XFS,而xvdf为data时,则表示设备上没有文件系统。
使用 mkfs -t 在空存储卷上创建文件系统。如果要装载已具有数据的存储卷(例如,通过快照还原的存储卷),请勿使用此命令,否则会格式化存储卷并删除现有数据。
创建挂载点目录并挂载:
mkdir /data
mount /dev/xvdf /data
重启验证挂载点是否存在,执行 reboot 操作,重启之后发现新挂载点不存在。
要做到重启后自动挂载附加存储卷,首先创建 /etc/fstab 文件的备份,以便在编辑时误损坏或删除此文件时使用:
cp /etc/fstab /etc/fstab.orig
使用 blkid 命令查找设备的UUID,打开 sudo vim /etc/fstab 文件,添加对应的挂载信息,然后执行 mount -a 挂载文件系统。
二、在线调整根存储卷大小
对于弹性存储卷EBS,可以在不分离Amazon EBS存储卷的情况下动态修改存储卷的大小、性能和存储卷类型。在修改包含有用数据的存储卷之前,最佳实践是创建存储卷的快照。
首先修改在线根存储卷大小,从8GB调整为10GB。
在增加EBS存储卷的大小后,必须使用特定于文件系统的命令来将文件系统扩展到较大大小。一旦存储卷进入 optimizing 状态,即可调整文件系统的大小。
参考链接:https://docs.amazonaws.cn/AWSEC2/latest/UserGuide/recognize-expanded-volume-linux.html
使用 file -s /dev/xvd* 查看分区文件系统,显示为XFS文件系统。
要在根存储卷上扩展分区,使用以下 growpart 命令:
growpart /dev/xvda 1
扩展XFS文件系统:
xfs_growfs -d /
三、在线调整存储卷类型
从通用型SSD 2GB修改为4GB预配置IO1。通过 lsblk 查看,扩展存储卷大小为4GB。
增大扩展存储卷文件系统,执行以下命令查看文件系统格式(格式为XFS):
file -s /dev/xvdf
使用 xfs_growfs -d /data 增大扩展存储卷文件系统,df -h 查看,从2GB增加到4GB。
本文结论
在AWS EBS上创建一台Linux EC2实例,可以实现以下在线操作:
- AWS EBS可在线增加EBS存储卷。
- 可在线修改EBS存储卷大小、类型、扩容存储卷,数据不会丢失,不需要重启生效。
- 当修改了存储卷大小后,需要6小时才能修改其他选项。
- 建议做操作前,拍摄快照进行备份。
陈汉卿
云业务事业部 | 高级系统架构师
神州泰岳软件股份有限公司 · AWS战略合作伙伴
返回技术博客
Overview
In daily operations, we often encounter situations where an AWS EC2 instance's storage volume runs out of space, requiring volume expansion, online volume addition, or volume type changes due to workload changes. Will these operations affect the business? The answer is no — AWS supports online volume expansion, online volume addition, and online volume type modification without restarting the EC2 instance.
The following article describes how to complete these operations. Reference: AWS EBS Modify Volume Documentation
Background
The environment has an AWS Linux EC2 instance with a default root volume of 8GB General Purpose SSD with 100 IOPS. After logging in and running df -h, the storage usage shows the default 8GB volume is 16% used.
1. Add an EBS Volume Online
Open the AWS Console, create a 2GB General Purpose SSD volume, and attach the newly created EBS volume to the test Linux instance. You can see the new EBS volume in the console.
Log into the system and run lsblk to view storage devices.
Run file -s /dev/xvda1 to check the file system. If xvda1 shows XFS and xvdf shows "data", it means the device has no file system yet.
Use mkfs -t to create a file system on the empty volume. Do NOT use this command on a volume that already contains data (e.g., a volume restored from a snapshot), as it will format the volume and delete existing data.
Create a mount point and mount the volume:
mkdir /data
mount /dev/xvdf /data
After a reboot, the mount point will be gone. To auto-mount on reboot, first back up /etc/fstab:
cp /etc/fstab /etc/fstab.orig
Use blkid to find the device UUID, then edit /etc/fstab to add the mount entry, and run mount -a to mount the file system.
2. Resize the Root Volume Online
For Elastic Volumes, you can dynamically modify the size, performance, and type of an Amazon EBS volume without detaching it. Before modifying a volume that contains useful data, the best practice is to create a snapshot.
Modify the root volume size from 8GB to 10GB in the console.
After increasing the EBS volume size, you must use file-system-specific commands to extend the file system to the larger size. Once the volume enters the "optimizing" state, you can resize the file system.
Reference: Recognize Expanded Volume on Linux
Check the partition file system with file -s /dev/xvd* — it shows XFS.
Extend the partition on the root volume:
growpart /dev/xvda 1
Extend the XFS file system:
xfs_growfs -d /
3. Change Volume Type Online
Change from General Purpose SSD 2GB to Provisioned IOPS IO1 4GB. After the change, lsblk shows the volume is now 4GB.
Check the file system format (XFS):
file -s /dev/xvdf
Extend the file system using xfs_growfs:
xfs_growfs -d /data
Run df -h to confirm the volume has grown from 2GB to 4GB.
Conclusion
On an AWS Linux EC2 instance with EBS storage, you can perform the following online operations:
- Add EBS volumes online without downtime.
- Modify EBS volume size, type, and expand capacity online — data is preserved and no restart is required.
- After modifying a volume's size, you must wait 6 hours before modifying other options.
- It is recommended to take a snapshot as a backup before performing any operations.
Chen Hanqing
Cloud Business Division | Senior Solutions Architect
Shenzhou Taiyue Software Co., Ltd. · AWS Strategic Partner
Back to Tech Blog