如何在 ECS 任务中将 Amazon EFS 挂载为数据卷?

如何在 ECS 任务中将 Amazon EFS 挂载为数据卷?

启动配置时的用户数据:

#!/bin/bash
echo ECS_CLUSTER=prodcluster >> /etc/ecs/ecs.config

sudo yum install -y nfs-utils 
sudo mkdir /home/ec2-user/web_file_uploads 
sudo chmod 777 /home/ec2-user/web_file_uploads 

sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 fs-abcdef.efs.ap-southeast-2.amazonaws.com:/ /home/ec2-user/web_file_uploads 

任务定义:

{
  "networkMode": "bridge",
  "containerDefinitions": [
    {
      "portMappings": [
        ...
      ],
      "essential": true,
      "mountPoints": [
        {
          "containerPath": "/src/temp_uploads/",
          "sourceVolume": "web_file_uploads",
          "readOnly": null
        }
      ],
      "name": "webapp",
      "readonlyRootFilesystem": null,
      "image": "911911911.dkr.ecr.ap-southeast-2.amazonaws.com/webapp:release6",
      "memoryReservation": 150
    }
  ],
  "volumes": [
    {
      "host": {
        "sourcePath": "/home/ec2-user/web_file_uploads"
      },
      "name": "web_file_uploads"
    }
  ],
  "family": "webapp-task"
}

笔记:我已将所需的端口添加到安全组。使用此任务定义启动的所有其他容器实例中都需要有 src/temp_uploads 文件夹。

问题:如果我使用手动挂载它sudo mount ... folder1,将一些文件放在那里并将其挂载到sudo mount ... folder2文件夹 2,其中包含表明它已正确挂载的更改。

问题是通过任务定义映射的卷不映射efs。相反,它实际上将内容放入/使用内部/home/ec2-user/web_file_uploads。如何使此卷实际映射到efs

答案1

修改 lunch 配置为安装 EFS 后立即重启 docker 服务。那么只有 ECS 会使用已挂载的 EFS 作为卷。否则它将使用原始目录(挂载将被忽略)。

#!/bin/bash
echo ECS_CLUSTER=prodcluster >> /etc/ecs/ecs.config
sudo yum install -y nfs-utils

sudo stop ecs
sudo mkdir /home/ec2-user/web_file_uploads
sudo chmod 777 /home/ec2-user/web_file_uploads
sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2 fs-abcdef.efs.ap-southeast-2.amazonaws.com:/ /home/ec2-user/web_file_uploads
sudo service docker restart
sudo start ecs

注意:由于 ECS Agent 在 docker 内部运行,因此重新启动 docker 服务后 ECS 服务将停止。之后您需要启动 ECS 服务。

相关内容