在 ECS 中附加卷 EFS

问题描述 投票:0回答:5

尝试将 EFS 文件系统与 ECS 一起挂载时,出现以下错误:

ResourceInitializationError:无法调用 EFS utils 命令来设置 EFS 卷:stderr:mount.nfs4:连接由对等方重置:EFS utils 命令执行失败;代码:32

我的堆栈:

--- 
  AWSTemplateFormatVersion: "2010-09-09"
  Description: "Template Test"
  Outputs: 
    FileSystemID: 
      Description: "File system ID"
      Value: 
        Ref: FileSystem
  Parameters: 
    VolumeName: 
      Default: myEFSvolume
      Description: "The name to be used for the EFS volume"
      MinLength: "1"
      Type: String
  Resources: 
    ECSCluster: 
      Properties: 
        ClusterName: jenkins-cluster
      Type: "AWS::ECS::Cluster"
    EFSMountTarget1: 
      Properties: 
        FileSystemId: 
          Ref: FileSystem
        SecurityGroups: 
          - "sg-0082cea75ba714505"
        SubnetId: "subnet-0f0b0d3aaada62b6c"
      Type: "AWS::EFS::MountTarget"
    FileSystem: 
      Properties: 
        Encrypted: true
        FileSystemTags: 
          - Key: Name
            Value: 
              Ref: VolumeName
        PerformanceMode: generalPurpose
      Type: "AWS::EFS::FileSystem"
    JenkinsService: 
      Type: "AWS::ECS::Service"
      Properties: 
        Cluster: 
          Ref: ECSCluster
        DesiredCount: 2
        LaunchType: FARGATE
        NetworkConfiguration: 
          AwsvpcConfiguration:
            AssignPublicIp: ENABLED
            SecurityGroups: 
              - "sg-0082cea75ba714505"
            Subnets: 
              - "subnet-0f0b0d3aaada62b6c"
        PlatformVersion: "1.4.0"
        ServiceName: JenkinsService
        
        TaskDefinition: 
          Ref: JenkinsTaskDef
    JenkinsTaskDef: 
      Type: "AWS::ECS::TaskDefinition"
      Properties:
        Cpu: 2048
        Memory: 4096
        Family: efs-example-task-fargate
        NetworkMode: awsvpc
        TaskRoleArn: "arn:xxxxx/ecs"
        ExecutionRoleArn: "arn:xxxxxx:role/ecs"
        RequiresCompatibilities:
          - FARGATE 
        ContainerDefinitions: 
          - Cpu: 1024
            Memory: 2048
            PortMappings:
              - HostPort: 8080
                ContainerPort: 8080
              - HostPort: 50000
                ContainerPort: 50000
            image: "xxxxxxx.dkr.ecr.us-east-1.amazonaws.com/sample:latest"
            mountPoints: 
              - containerPath: /var/jenkins_home
                readOnly: false
                sourceVolume: myEfsVolume
            name: jenkins
        volumes:
          - name: myEfsVolume  
            efsVolumeConfiguration: 
              fileSystemId: 
                Ref: FileSystem
              rootDirectory: /var/jenkins_home
              transitEncryption: ENABLED 
    

我正在根据文档执行:

https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_EFSVolumeConfiguration.html

amazon-web-services docker jenkins amazon-ecs amazon-efs
5个回答
18
投票

您需要在网络接口和任务定义的安全组上打开端口 2049 入站。即使您将其设置为为您创建安全组,它也不会自动设置。


13
投票

已经有一段时间了,但我遇到了同样的问题,并且不知道如何继续。创建 EFS 卷时,您可以为每个子网选择一个 VPC 和一个安全组。

您需要去编辑此安全组,添加

type
NFS 的入站规则,以允许访问(tcp 端口 2049)您要允许访问的 ECS 集群服务的安全组标识符。为此,只需在
source
字段中选择自定义,然后在文本框中键入服务的安全组标识符。

欲了解更多信息这篇文章很好地描述了整个过程。


8
投票

在 AWS FARGATE 中安装 EFS 需要执行以下操作:

  1. 从应用层将 2049 端口添加到 EFS sg 组(本例中为容器 sg)
  2. 使用挂载和写入 EFS 的策略更新 ECS FARGATE 任务执行角色
  3. EFS 子网的 NACL 在端口 2049 上有出站

2
投票

如果您在将任务定义关联到卷时启用了 IAM 授权,则还需要更新其任务执行角色。您需要将访问 EFS 所需的策略附加到其中。


0
投票

如果是 CDK,以下 PolicyStatement 将处理上述建议的操作并修复错误:

fileSystem.addToResourcePolicy(
      new iam.PolicyStatement({
        actions: ['elasticfilesystem:ClientMount'],
        principals: [new iam.AnyPrincipal()],
        conditions: {
          Bool: {
            'elasticfilesystem:AccessedViaMountTarget': 'true'
          }
        }
      })
    )
© www.soinside.com 2019 - 2024. All rights reserved.