我想将 Amazon Elastic Block Storage (Amazon EBS) 卷用于我的 Amazon Elastic Container Service (Amazon ECS) 任务。
解决方案
**注意:**如果您在运行 AWS 命令行界面 (AWS CLI) 命令时收到错误,请参阅 AWS CLI 错误故障排除。此外,请确保您使用的是最新版本的 AWS CLI。
先决条件:
要将新的 EBS 卷附加到您的任务,请在任务定义的 mountPoints 下指定以下配置:
- 对于 sourceVolume,输入要挂载的卷的名称。
- 对于 containerPath,输入在容器实例中挂载卷的路径。
- 对于 readOnly,根据容器对卷是否具有只读访问权限,输入 true 或 false。
然后,将 configuredAtLaunch 设置为 true。
任务定义示例:
{
"family": "mytaskdef",
"containerDefinitions": [
{
....
#Some fields removed from example
....
"mountPoints": [
{
"sourceVolume": "myEBSVolume",
"containerPath": "/mount/ebs",
"readOnly": true
}
]
}
],
....
#Some fields removed from example
....
"volumes": [
{
"name": "myEBSVolume",
"configuredAtLaunch": true
}
]
}
将任务定义模板保存为 JSON 文件,然后运行 register-task-definition 来注册任务定义:
aws ecs register-task-definition \
--cli-input-json file://json_file_name.json \
--region Region_name
**注意:**请将 json_file_name.json 替换为您的 JSON 文件,将 Region_name 替换为您的 AWS 区域。
然后,在运行独立任务时,在部署时配置 Amazon EBS 卷。或者,在创建或更新服务时进行配置。
要使用现有 EBS 卷中的数据,请创建该卷的快照。然后,在任务定义中的 VolumeConfigurations 下,为 SnapshotID 添加快照 ID。有关示例配置,请参阅在创建服务时配置卷。
相关信息
对将 Amazon EBS 卷附加到 Amazon ECS 任务进行故障排除
将 Amazon EBS 卷与 Amazon ECS 结合使用