ECS コンテナにNFSサーバをマウント - toge510/aws GitHub Wiki

NFS server

commands

mkdir /share
chmod 755 /share
yum install nfs-utils -y
vi /etc/exports
/share 10.0.0.0/16(rw)
systemctl start nfs

参考:

ECS on EC2を準備

ECS cluster

  • nginx-test

EC2準備

  • ami: al2023-ami-ecs-hvm-2023.0.20240328-kernel-6.1-x86_64
  • instance type: t2.small
  • private ip: 10.0.0.234
  • user-data
    #!/bin/bash
    echo ECS_CLUSTER=nginx-test >> /etc/ecs/ecs.config
    

ECS clusterのコンテナインスタンスに登録される。

image

参考:

mount -t nfs 10.0.0.25:/share /test でテスト

ECS

Dockerfile, docker image push

FROM nginx:1.25.4
RUN mkdir -p /home/work/mnt/

Task Definition

{
    "family": "nginx-task-definition",
    "containerDefinitions": [
        {
            "name": "nginx",
            "image": "624838222411.dkr.ecr.ap-northeast-1.amazonaws.com/test",
            "cpu": 0,
            "portMappings": [
                {
                    "name": "nginx-80-tcp",
                    "containerPort": 80,
                    "hostPort": 80,
                    "protocol": "tcp",
                    "appProtocol": "http"
                }
            ],
            "essential": true,
            "environment": [],
            "mountPoints": [
                {
                    "sourceVolume": "NFS",
                    "containerPath": "/home/work/mnt/"
                }
            ],
            "volumesFrom": [],
            "logConfiguration": {
                "logDriver": "awslogs",
                "options": {
                    "awslogs-create-group": "true",
                    "awslogs-group": "/ecs/nginx-task-definition",
                    "awslogs-region": "ap-northeast-1",
                    "awslogs-stream-prefix": "ecs"
                }
            },
            "systemControls": []
        }
    ],
    "executionRoleArn": "arn:aws:iam::624838222411:role/ecsTaskExecutionRole",
    "networkMode": "awsvpc",
    "volumes": [
        {
            "name": "NFS",
            "dockerVolumeConfiguration": {
                "scope": "task",
                "driver": "local",
                "driverOpts": {
                    "device": "10.0.0.25:/share",
                    "o": "addr=10.0.0.25,nfsvers=4",
                    "type": "nfs"
                }
            }
        }
    ],
    "requiresCompatibilities": [
        "EC2"
    ],
    "cpu": "256",
    "memory": "512",
    "runtimePlatform": {
        "cpuArchitecture": "X86_64",
        "operatingSystemFamily": "LINUX"
    }
}

https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/docker-volume-examples.html

nfsvers=4がないと、以下エラーが発生した。

Screenshot 2024-04-07 at 0 24 10

ECS service

マウント状況

mount

10.0.0.25:/share on /var/lib/docker/volumes/ecs-nginx-task-definition-9-NFS-82e6fedcb8fccaa3f401/_data type nfs4 (rw,relatime,vers=4.0,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.0.0.234,local_lock=none,addr=10.0.0.25)